Vulcan/server/publications/posts_list.js

41 lines
1.3 KiB
JavaScript
Raw Normal View History

// Publish a list of posts
Meteor.publish('postsList', function(terms) {
var user = Meteor.users.findOne(this.userId);
if(canViewById(this.userId)){
var parameters = getPostsParameters(terms, user),
posts = Posts.find(parameters.find, parameters.options);
2014-09-08 17:39:59 +09:00
// console.log('//-------- Subscription Parameters:');
// console.log(parameters.find);
// console.log(parameters.options);
// console.log('Found '+posts.fetch().length+ ' posts:');
// posts.rewind();
// console.log(_.pluck(posts.fetch(), 'title'));
return posts;
}
return [];
});
// Publish all the users that have posted the currently displayed list of posts
2014-12-14 15:15:34 +09:00
// plus the commenters for each post
Meteor.publish('postsListUsers', function(terms) {
if(canViewById(this.userId)){
var parameters = getPostsParameters(terms),
posts = Posts.find(parameters.find, parameters.options),
2014-12-14 15:15:34 +09:00
postsIds = _.pluck(posts.fetch(), '_id'),
userIds = _.pluck(posts.fetch(), 'userId');
2014-12-14 15:15:34 +09:00
2014-12-17 11:42:08 +09:00
// for each post, add first four commenter's userIds to userIds array
posts.forEach(function (post) {
2014-12-17 11:42:08 +09:00
userIds = userIds.concat(_.first(post.commenters,4));
});
userIds = _.unique(userIds);
2014-12-17 11:42:08 +09:00
2014-12-14 15:15:34 +09:00
return Meteor.users.find({_id: {$in: userIds}}, {fields: avatarOptions, multi: true});
}
return [];
});