Vulcan/server/publications/single_post.js

54 lines
1.4 KiB
JavaScript
Raw Normal View History

2014-09-04 16:39:35 +09:00
// Publish a single post
Meteor.publish('singlePost', function(id) {
if (Users.can.viewById(this.userId)){
2014-09-04 16:39:35 +09:00
return Posts.find(id);
}
return [];
});
// Publish author of the current post, authors of its comments, and upvoters of the post
2014-09-04 16:39:35 +09:00
Meteor.publish('postUsers', function(postId) {
if (Users.can.viewById(this.userId)){
2014-09-04 16:39:35 +09:00
// publish post author and post commenters
var post = Posts.findOne(postId),
users = [post.userId]; // publish post author's ID
2014-09-04 16:39:35 +09:00
if (post) {
// get IDs from all commenters on the post
2014-09-04 16:39:35 +09:00
var comments = Comments.find({postId: post._id}).fetch();
if (comments.length) {
users = users.concat(_.pluck(comments, "userId"));
}
// publish upvoters
if (post.upvoters && post.upvoters.length) {
users = users.concat(post.upvoters);
}
// publish downvoters
if (post.downvoters && post.downvoters.length) {
users = users.concat(post.downvoters);
}
2014-09-04 16:39:35 +09:00
}
// remove any duplicate IDs
users = _.unique(users);
return Meteor.users.find({_id: {$in: users}}, {fields: Users.pubsub.privacyOptions});
2014-09-04 16:39:35 +09:00
}
return [];
});
// Publish comments for a specific post
Meteor.publish('postComments', function(postId) {
if (Users.can.viewById(this.userId)){
return Comments.find({postId: postId}, {sort: {score: -1, postedAt: -1}});
2014-09-04 16:39:35 +09:00
}
return [];
});