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

103 lines
2.9 KiB
JavaScript
Raw Normal View History

Posts._ensureIndex({"status": 1, "postedAt": 1});
2016-02-17 11:28:00 +09:00
// ------------------------------------- Helpers -------------------------------- //
/**
* Get all users relevant to a list of posts
* (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);
2016-02-17 12:54:18 +09:00
return Meteor.users.find({_id: {$in: userIds}}, {fields: Users.publishedFields.list});
2016-02-17 11:28:00 +09:00
};
/**
* Get all users relevant to a single post
* (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
// get IDs from all commenters on the post
const comments = Comments.find({postId: post._id}).fetch();
if (comments.length) {
users = users.concat(_.pluck(comments, "userId"));
}
// 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);
2016-02-17 12:54:18 +09:00
return Meteor.users.find({_id: {$in: users}}, {fields: Users.publishedFields.list});
2016-02-17 11:28:00 +09:00
};
// ------------------------------------- Publications -------------------------------- //
/**
2016-02-17 12:54:18 +09:00
* 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-02-17 12:54:18 +09:00
Meteor.publish('posts.list', function (terms) {
2016-02-16 15:08:30 +09:00
2016-02-17 17:46:34 +09:00
// this.unblock(); // causes bug where publication returns 0 results
2016-02-16 15:08:30 +09:00
// this.autorun(function () { // sadly, reactive joins break SSR for now :(
const currentUser = Meteor.users.findOne(this.userId);
2016-02-16 15:08:30 +09:00
terms.currentUserId = this.userId; // add currentUserId to terms
({selector, options} = Posts.parameters.get(terms));
2016-03-22 10:22:46 +09:00
// disabled for now because of FlowRouterSSR issue
// Counts.publish(this, 'posts.list', Posts.find(selector, options));
options.fields = Posts.publishedFields.list;
const posts = Posts.find(selector, options);
const users = getPostsListUsers(posts);
return Users.can.view(currentUser) ? [posts, users] : [];
// });
2016-02-17 11:28:00 +09:00
});
2015-04-28 09:44:43 +09:00
2016-02-17 11:28:00 +09:00
/**
2016-02-17 12:54:18 +09:00
* Publish a single post, along with all relevant users
2016-02-17 11:28:00 +09:00
* @param {Object} terms
*/
2016-02-17 12:54:18 +09:00
Meteor.publish('posts.single', function (terms) {
2016-02-27 10:58:57 +09:00
2016-02-17 11:28:00 +09:00
check(terms, {_id: String});
2016-02-17 11:28:00 +09:00
const currentUser = Meteor.users.findOne(this.userId);
2016-02-17 12:54:18 +09:00
const options = {fields: Posts.publishedFields.single};
const posts = Posts.find(terms, options);
const post = posts.fetch()[0];
2016-02-17 11:28:00 +09:00
const users = getSinglePostUsers(post);
2014-09-04 16:39:35 +09:00
return Users.can.viewPost(currentUser, post) ? [posts, users] : [];
2016-02-17 11:28:00 +09:00
});