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

102 lines
2.6 KiB
JavaScript
Raw Normal View History

2016-06-23 11:40:35 +09:00
import Posts from "meteor/nova:posts";
2016-06-23 15:00:58 +09:00
import Users from 'meteor/nova:users';
2016-06-23 11:40:35 +09:00
2015-08-13 11:32:41 +09:00
Comments._ensureIndex({postId: 1});
Comments._ensureIndex({parentCommentId: 1});
2016-02-17 12:54:18 +09:00
/**
* @summary Publish a list of comments, along with the posts and users corresponding to these comments
2016-02-17 12:54:18 +09:00
* @param {Object} terms
*/
Meteor.publish('comments.list', function (terms) {
2016-01-02 18:40:49 +01:00
2016-02-17 12:54:18 +09:00
const currentUser = Meteor.users.findOne(this.userId);
terms.currentUserId = this.userId; // add currentUserId to terms
({selector, options} = Comments.parameters.get(terms));
2016-02-17 12:54:18 +09:00
2016-04-03 15:56:12 +09:00
// commenting this because of FR-SSR issue
// Counts.publish(this, 'comments.list', Comments.find(selector, options));
2016-02-17 12:54:18 +09:00
options.fields = Comments.publishedFields.list;
const comments = Comments.find(selector, options);
2016-02-17 12:54:18 +09:00
const posts = Posts.find({_id: {$in: _.pluck(comments.fetch(), 'postId')}}, {fields: Posts.publishedFields.list});
const users = Meteor.users.find({_id: {$in: _.pluck(comments.fetch(), 'userId')}}, {fields: Users.publishedFields.list});
return Users.can.view(currentUser) ? [comments, posts, users] : [];
});
2015-04-22 07:50:26 +09:00
2016-06-23 12:17:39 +09:00
2016-02-17 12:54:18 +09:00
// /**
// * Publish a single comment, along with all relevant users
// * @param {Object} terms
// */
// Meteor.publish('comments.single', function(terms) {
2016-02-17 12:54:18 +09:00
// check(terms, {_id: String});
2016-02-17 17:46:34 +09:00
//
2016-01-02 18:40:49 +01:00
2016-02-17 12:54:18 +09:00
// let commentIds = [terms._id];
// const childCommentIds = _.pluck(Comments.find({parentCommentId: terms._id}, {fields: {_id: 1}}).fetch(), '_id');
// commentIds = commentIds.concat(childCommentIds);
// return Users.can.view(currentUser) ? Comments.find({_id: {$in: commentIds}}, {sort: {score: -1, postedAt: -1}}) : [];
2016-02-16 16:12:13 +09:00
// });
2015-04-22 07:50:26 +09:00
2016-02-17 12:54:18 +09:00
2016-02-16 16:12:13 +09:00
// // Publish the post related to the current comment
2015-04-22 07:50:26 +09:00
2016-02-16 16:12:13 +09:00
// Meteor.publish('commentPost', function(commentId) {
2016-02-16 16:12:13 +09:00
// check(commentId, String);
2016-02-17 17:46:34 +09:00
//
2016-01-02 18:40:49 +01:00
2016-02-16 16:12:13 +09:00
// if(Users.can.viewById(this.userId)){
// var comment = Comments.findOne(commentId);
// return Posts.find({_id: comment && comment.postId});
// }
// return [];
// });
2015-04-22 07:50:26 +09:00
2016-02-16 16:12:13 +09:00
// // Publish author of the current comment, and author of the post related to the current comment
2015-04-22 07:50:26 +09:00
2016-02-16 16:12:13 +09:00
// Meteor.publish('commentUsers', function(commentId) {
2015-04-22 07:50:26 +09:00
2016-02-16 16:12:13 +09:00
// check(commentId, String);
2016-01-02 18:40:49 +01:00
2016-02-17 17:46:34 +09:00
//
2016-01-02 18:40:49 +01:00
2016-02-16 16:12:13 +09:00
// var userIds = [];
2015-04-22 07:50:26 +09:00
2016-02-16 16:12:13 +09:00
// if(Users.can.viewById(this.userId)){
2015-04-22 07:50:26 +09:00
2016-02-16 16:12:13 +09:00
// var comment = Comments.findOne(commentId);
2015-05-21 15:51:19 +09:00
2016-02-16 16:12:13 +09:00
// if (!!comment) {
// userIds.push(comment.userId);
2015-04-22 07:50:26 +09:00
2016-02-16 16:12:13 +09:00
// var post = Posts.findOne(comment.postId);
// if (!!post) {
// userIds.push(post.userId);
// }
2015-04-22 07:50:26 +09:00
2016-02-16 16:12:13 +09:00
// return Meteor.users.find({_id: {$in: userIds}}, {fields: Users.pubsub.publicProperties});
2015-05-21 15:51:19 +09:00
2016-02-16 16:12:13 +09:00
// }
2015-04-22 07:50:26 +09:00
2016-02-16 16:12:13 +09:00
// }
2015-04-22 07:50:26 +09:00
2016-02-16 16:12:13 +09:00
// return [];
2015-04-22 07:50:26 +09:00
2016-02-16 16:12:13 +09:00
// });