Vulcan/packages/nova-comments/lib/server/publications.js
Comus Leong 464e20a96c eslint & clean up code, also fixed some bugs (#1515)
* [eslint] update eslint rules & add .eslintignore to ignore non-ready nova packages

* [clean-up] nova-voting

* [clean-up] [bug] nova-users: missing user parameter

* [clean-up] nova-users

* [clean-up] nova-subscribe

* [clean-up] nova-settings

* [clean-up] nova-rss

* [clean-up] [bug] nova-posts: correct UsersRemoveDeletePosts

* [clean-up] nova-posts

* [clean-up] nova-notifications

* [clean-up] [bug] nova-newsletter: no error.message on throw error

* [clean-up] nova-newsletter

* [clean-up] nova-lib

* [clean-up] nova-kadira

* [clean-up] nova-inject-data

* [clean-up] nova-getting-started

* [clean-up] nova-forms

* [clean-up] nova-events

* [clean-up] [bug] nova-embedly: no FlowRouter

* [clean-up] nova-embedly

* [clean-up] nova-email-templates

* [clean-up] nova-email

* [clean-up] nova-debug

* [clean-up] nova-core

* [clean-up] [bug] nova-comments: correct UsersRemoveDeleteComments

* [clean-up] nova-comments

* [clean-up] [bug] nova-cloudinary: use Telescope.settings.collection instand

* [clean-up] nova-cloudinary

* [clean-up] nova-categories

* [clean-up] nova-base-components

* [clean-up] nova-api

* [eslint] extends react recommended

* [clean-up] for jsx files

* [eslint] extends meteor recommended

* i forgot this one little change
2016-11-25 13:46:55 -05:00

103 lines
2.6 KiB
JavaScript

import Posts from "meteor/nova:posts";
import Users from 'meteor/nova:users';
import Comments from '../collection.js';
Comments._ensureIndex({postId: 1});
Comments._ensureIndex({parentCommentId: 1});
/**
* @summary Publish a list of comments, along with the posts and users corresponding to these comments
* @param {Object} terms
*/
Meteor.publish('comments.list', function (terms) {
const currentUser = this.userId && Users.findOne(this.userId);
terms.currentUserId = this.userId; // add currentUserId to terms
const {selector, options} = Comments.parameters.get(terms);
// commenting this because of FR-SSR issue
// Counts.publish(this, 'comments.list', Comments.find(selector, options));
options.fields = Comments.publishedFields.list;
const comments = Comments.find(selector, options);
const posts = Posts.find({_id: {$in: _.pluck(comments.fetch(), 'postId')}}, {fields: Posts.publishedFields.list});
const users = Users.find({_id: {$in: _.pluck(comments.fetch(), 'userId')}}, {fields: Users.publishedFields.list});
return Users.canDo(currentUser, "comments.view.all") ? [comments, posts, users] : [];
});
// /**
// * Publish a single comment, along with all relevant users
// * @param {Object} terms
// */
// Meteor.publish('comments.single', function(terms) {
// check(terms, {_id: String});
//
// let commentIds = [terms._id];
// const childCommentIds = _.pluck(Comments.find({parentCommentId: terms._id}, {fields: {_id: 1}}).fetch(), '_id');
// commentIds = commentIds.concat(childCommentIds);
// return Users.canView(currentUser) ? Comments.find({_id: {$in: commentIds}}, {sort: {score: -1, postedAt: -1}}) : [];
// });
// // Publish the post related to the current comment
// Meteor.publish('commentPost', function(commentId) {
// check(commentId, String);
//
// if(Users.canViewById(this.userId)){
// var comment = Comments.findOne(commentId);
// return Posts.find({_id: comment && comment.postId});
// }
// return [];
// });
// // Publish author of the current comment, and author of the post related to the current comment
// Meteor.publish('commentUsers', function(commentId) {
// check(commentId, String);
//
// var userIds = [];
// if(Users.canViewById(this.userId)){
// var comment = Comments.findOne(commentId);
// if (!!comment) {
// userIds.push(comment.userId);
// var post = Posts.findOne(comment.postId);
// if (!!post) {
// userIds.push(post.userId);
// }
// return Users.find({_id: {$in: userIds}}, {fields: Users.pubsub.publicProperties});
// }
// }
// return [];
// });