2015-04-22 08:13:43 +09:00
|
|
|
Posts._ensureIndex({"status": 1, "postedAt": 1});
|
|
|
|
|
|
|
|
// Publish a list of posts
|
|
|
|
|
|
|
|
Meteor.publish('postsList', function(terms) {
|
|
|
|
if(Users.can.viewById(this.userId)){
|
2015-09-03 14:22:51 +09:00
|
|
|
var parameters = Posts.parameters.get(terms),
|
2015-04-22 08:13:43 +09:00
|
|
|
posts = Posts.find(parameters.find, parameters.options);
|
|
|
|
|
|
|
|
return posts;
|
|
|
|
}
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
|
|
|
|
// Publish all the users that have posted the currently displayed list of posts
|
|
|
|
// plus the commenters for each post
|
|
|
|
|
|
|
|
Meteor.publish('postsListUsers', function(terms) {
|
|
|
|
if(Users.can.viewById(this.userId)){
|
2015-09-03 14:22:51 +09:00
|
|
|
var parameters = Posts.parameters.get(terms),
|
2015-04-22 08:13:43 +09:00
|
|
|
posts = Posts.find(parameters.find, parameters.options),
|
|
|
|
userIds = _.pluck(posts.fetch(), 'userId');
|
|
|
|
|
|
|
|
// for each post, add first four commenter's userIds to userIds array
|
|
|
|
posts.forEach(function (post) {
|
|
|
|
userIds = userIds.concat(_.first(post.commenters,4));
|
|
|
|
});
|
|
|
|
|
|
|
|
userIds = _.unique(userIds);
|
|
|
|
|
2015-04-25 13:20:51 +09:00
|
|
|
return Meteor.users.find({_id: {$in: userIds}}, {fields: Users.pubsub.avatarProperties, multi: true});
|
2015-04-22 08:13:43 +09:00
|
|
|
}
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
|
2014-09-04 16:39:35 +09:00
|
|
|
// Publish a single post
|
|
|
|
|
2015-07-10 11:05:13 +09:00
|
|
|
Meteor.publish('singlePost', function(postId) {
|
|
|
|
|
|
|
|
check(postId, String);
|
|
|
|
|
2015-04-20 13:57:37 +09:00
|
|
|
if (Users.can.viewById(this.userId)){
|
2015-07-10 11:05:13 +09:00
|
|
|
return Posts.find(postId);
|
2014-09-04 16:39:35 +09:00
|
|
|
}
|
|
|
|
return [];
|
|
|
|
});
|
|
|
|
|
2015-03-26 13:15:16 +09:00
|
|
|
// 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) {
|
2015-07-10 11:05:13 +09:00
|
|
|
|
|
|
|
check(postId, String);
|
|
|
|
|
2015-04-20 13:57:37 +09:00
|
|
|
if (Users.can.viewById(this.userId)){
|
2014-09-04 16:39:35 +09:00
|
|
|
// publish post author and post commenters
|
2015-04-28 09:44:43 +09:00
|
|
|
var post = Posts.findOne(postId);
|
|
|
|
var users = [];
|
2015-05-01 18:22:00 +02:00
|
|
|
|
2015-01-07 08:22:46 +01:00
|
|
|
if (post) {
|
2015-03-26 13:15:16 +09:00
|
|
|
|
2015-04-28 09:44:43 +09:00
|
|
|
users.push(post.userId); // publish post author's ID
|
|
|
|
|
2015-03-26 13:15:16 +09:00
|
|
|
// get IDs from all commenters on the post
|
2014-09-04 16:39:35 +09:00
|
|
|
var comments = Comments.find({postId: post._id}).fetch();
|
2015-03-26 13:15:16 +09:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2015-03-26 13:15:16 +09:00
|
|
|
// remove any duplicate IDs
|
|
|
|
users = _.unique(users);
|
2015-04-20 13:57:37 +09:00
|
|
|
|
2015-04-25 13:20:51 +09:00
|
|
|
return Meteor.users.find({_id: {$in: users}}, {fields: Users.pubsub.publicProperties});
|
2014-09-04 16:39:35 +09:00
|
|
|
}
|
|
|
|
return [];
|
|
|
|
});
|