Vulcan/packages/nova-posts/lib/server/publications.js

121 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-06-23 12:17:39 +09:00
// import Comments from "meteor/nova:comments";
2016-06-23 15:00:58 +09:00
import Users from 'meteor/nova:users';
import { Counts } from 'meteor/tmeasday:publish-counts';
import Posts from '../collection.js';
import { runCallbacks } from 'meteor/nova:core';
Posts._ensureIndex({"status": 1, "postedAt": 1});
2016-02-17 11:28:00 +09:00
// ------------------------------------- Helpers -------------------------------- //
/**
* @summary Get all users relevant to a list of posts
2016-02-17 11:28:00 +09:00
* (authors of the listed posts, and first four commenters of each post)
* @param {Object} posts
*/
const getPostsListUsers = posts => {
// add the userIds of each post authors
let userIds = _.pluck(posts.fetch(), 'userId');
// for each post, also add first four commenter's userIds to userIds array
posts.forEach(function (post) {
userIds = userIds.concat(_.first(post.commenters,4));
});
userIds = _.unique(userIds);
return Users.find({_id: {$in: userIds}}, {fields: Users.publishedFields.list});
2016-02-17 11:28:00 +09:00
};
/**
* @summary Get all users relevant to a single post
2016-02-17 11:28:00 +09:00
* (author of the current post, authors of its comments, and upvoters & downvoters of the post)
* @param {Object} post
*/
const getSinglePostUsers = post => {
let users = [post.userId]; // publish post author's ID
/*
NOTE: to avoid circular dependencies between nova:posts and nova:comments,
2016-06-23 12:17:39 +09:00
use callback hook to get comment authors
*/
users = runCallbacks("posts.single.getUsers", users, post);
2016-02-17 11:28:00 +09:00
// add upvoters
if (post.upvoters && post.upvoters.length) {
users = users.concat(post.upvoters);
}
// add downvoters
if (post.downvoters && post.downvoters.length) {
users = users.concat(post.downvoters);
}
// remove any duplicate IDs
users = _.unique(users);
return Users.find({_id: {$in: users}}, {fields: Users.publishedFields.list});
2016-02-17 11:28:00 +09:00
};
// ------------------------------------- Publications -------------------------------- //
/**
* @summary Publish a list of posts, along with the users corresponding to these posts
2016-02-17 11:28:00 +09:00
* @param {Object} terms
*/
2016-12-23 20:58:10 +01:00
// Meteor.publish('posts.list', function (terms) {
//
// // this.unblock(); // causes bug where publication returns 0 results
//
// this.autorun(function () {
//
// const currentUser = this.userId && Users.findOne(this.userId);
//
// terms.currentUserId = this.userId; // add currentUserId to terms
// const {selector, options} = Posts.getParameters(terms);
//
// Counts.publish(this, terms.listId, Posts.find(selector, options), {noReady: true});
//
// options.fields = Posts.publishedFields.list;
//
// const posts = Posts.find(selector, options);
//
// // note: doesn't work yet :(
// // CursorCounts.set(terms, posts.count(), this.connection.id);
//
// const users = Tracker.nonreactive(function () {
// return getPostsListUsers(posts);
// });
//
// return Users.canDo(currentUser, "posts.view.approved.all") ? [posts, users] : [];
//
// });
//
// });
//
// /**
// * @summary Publish a single post, along with all relevant users
// * @param {Object} terms
// */
// Meteor.publish('posts.single', function (terms) {
//
// check(terms, Match.OneOf({_id: String}, {_id: String, slug: Match.Any}));
//
// const currentUser = this.userId && Users.findOne(this.userId);
// const options = {fields: Posts.publishedFields.single};
// const posts = Posts.find(terms._id, options);
// const post = posts.fetch()[0];
//
// if (post) {
// const users = getSinglePostUsers(post);
// return Users.canView(currentUser, post) ? [posts, users] : [];
// } else {
// console.log(`// posts.single: no post found for _id “${terms._id}”`); // eslint-disable-line
// return [];
// }
//
// });