2016-06-23 12:17:39 +09:00
|
|
|
// import Comments from "meteor/nova:comments";
|
2016-06-23 15:00:58 +09:00
|
|
|
import Users from 'meteor/nova:users';
|
2016-11-26 02:46:55 +08:00
|
|
|
import { Counts } from 'meteor/tmeasday:publish-counts';
|
|
|
|
import Posts from '../collection.js';
|
2016-12-13 11:40:24 +09:00
|
|
|
import { runCallbacks } from 'meteor/nova:core';
|
2016-06-06 10:06:53 +09:00
|
|
|
|
2015-04-22 08:13:43 +09:00
|
|
|
Posts._ensureIndex({"status": 1, "postedAt": 1});
|
|
|
|
|
2016-02-17 11:28:00 +09:00
|
|
|
// ------------------------------------- Helpers -------------------------------- //
|
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Get all users relevant to a list of posts
|
2016-02-17 11:28:00 +09:00
|
|
|
* (authors of the listed posts, and first four commenters of each post)
|
|
|
|
* @param {Object} posts
|
|
|
|
*/
|
|
|
|
const getPostsListUsers = posts => {
|
|
|
|
|
|
|
|
// add the userIds of each post authors
|
|
|
|
let userIds = _.pluck(posts.fetch(), 'userId');
|
|
|
|
|
|
|
|
// for each post, also add first four commenter's userIds to userIds array
|
|
|
|
posts.forEach(function (post) {
|
|
|
|
userIds = userIds.concat(_.first(post.commenters,4));
|
|
|
|
});
|
|
|
|
|
|
|
|
userIds = _.unique(userIds);
|
|
|
|
|
2016-10-05 08:37:48 +02:00
|
|
|
return Users.find({_id: {$in: userIds}}, {fields: Users.publishedFields.list});
|
2016-11-26 02:46:55 +08:00
|
|
|
|
2016-02-17 11:28:00 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Get all users relevant to a single post
|
2016-02-17 11:28:00 +09:00
|
|
|
* (author of the current post, authors of its comments, and upvoters & downvoters of the post)
|
|
|
|
* @param {Object} post
|
|
|
|
*/
|
|
|
|
const getSinglePostUsers = post => {
|
|
|
|
|
|
|
|
let users = [post.userId]; // publish post author's ID
|
|
|
|
|
2016-11-26 02:46:55 +08:00
|
|
|
/*
|
|
|
|
NOTE: to avoid circular dependencies between nova:posts and nova:comments,
|
2016-06-23 12:17:39 +09:00
|
|
|
use callback hook to get comment authors
|
|
|
|
*/
|
2016-12-13 11:40:24 +09:00
|
|
|
users = runCallbacks("posts.single.getUsers", users, post);
|
2016-11-26 02:46:55 +08:00
|
|
|
|
2016-02-17 11:28:00 +09:00
|
|
|
// add upvoters
|
|
|
|
if (post.upvoters && post.upvoters.length) {
|
|
|
|
users = users.concat(post.upvoters);
|
|
|
|
}
|
|
|
|
|
|
|
|
// add downvoters
|
|
|
|
if (post.downvoters && post.downvoters.length) {
|
|
|
|
users = users.concat(post.downvoters);
|
|
|
|
}
|
|
|
|
|
|
|
|
// remove any duplicate IDs
|
|
|
|
users = _.unique(users);
|
|
|
|
|
2016-10-05 08:37:48 +02:00
|
|
|
return Users.find({_id: {$in: users}}, {fields: Users.publishedFields.list});
|
2016-02-17 11:28:00 +09:00
|
|
|
};
|
|
|
|
|
|
|
|
// ------------------------------------- Publications -------------------------------- //
|
|
|
|
|
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Publish a list of posts, along with the users corresponding to these posts
|
2016-02-17 11:28:00 +09:00
|
|
|
* @param {Object} terms
|
|
|
|
*/
|
2016-02-17 12:54:18 +09:00
|
|
|
Meteor.publish('posts.list', function (terms) {
|
2016-02-16 15:08:30 +09:00
|
|
|
|
2016-11-26 02:46:55 +08:00
|
|
|
// this.unblock(); // causes bug where publication returns 0 results
|
2016-02-16 15:08:30 +09:00
|
|
|
|
2016-03-28 11:43:42 +09:00
|
|
|
this.autorun(function () {
|
2016-11-26 02:46:55 +08:00
|
|
|
|
2016-10-05 08:37:48 +02:00
|
|
|
const currentUser = this.userId && Users.findOne(this.userId);
|
2016-02-16 15:08:30 +09:00
|
|
|
|
2016-02-27 16:41:50 +09:00
|
|
|
terms.currentUserId = this.userId; // add currentUserId to terms
|
2016-06-06 10:06:53 +09:00
|
|
|
const {selector, options} = Posts.parameters.get(terms);
|
2016-11-26 02:46:55 +08:00
|
|
|
|
2016-06-06 15:03:02 +09:00
|
|
|
Counts.publish(this, terms.listId, Posts.find(selector, options), {noReady: true});
|
2016-02-06 12:30:29 +09:00
|
|
|
|
2016-02-27 16:41:50 +09:00
|
|
|
options.fields = Posts.publishedFields.list;
|
2015-04-22 08:13:43 +09:00
|
|
|
|
2016-02-27 16:41:50 +09:00
|
|
|
const posts = Posts.find(selector, options);
|
2016-06-06 10:06:53 +09:00
|
|
|
|
2016-06-06 12:19:07 +09:00
|
|
|
// note: doesn't work yet :(
|
2016-06-06 15:03:02 +09:00
|
|
|
// CursorCounts.set(terms, posts.count(), this.connection.id);
|
2016-06-06 10:06:53 +09:00
|
|
|
|
2016-07-05 16:55:11 +09:00
|
|
|
const users = Tracker.nonreactive(function () {
|
|
|
|
return getPostsListUsers(posts);
|
|
|
|
});
|
2015-04-22 08:13:43 +09:00
|
|
|
|
2016-07-20 10:25:05 +09:00
|
|
|
return Users.canDo(currentUser, "posts.view.approved.all") ? [posts, users] : [];
|
2016-11-26 02:46:55 +08:00
|
|
|
|
2016-03-28 11:43:42 +09:00
|
|
|
});
|
2016-02-27 16:41:50 +09:00
|
|
|
|
2016-02-17 11:28:00 +09:00
|
|
|
});
|
2015-04-28 09:44:43 +09:00
|
|
|
|
2016-02-17 11:28:00 +09:00
|
|
|
/**
|
2016-04-09 09:41:20 +09:00
|
|
|
* @summary Publish a single post, along with all relevant users
|
2016-02-17 11:28:00 +09:00
|
|
|
* @param {Object} terms
|
|
|
|
*/
|
2016-02-17 12:54:18 +09:00
|
|
|
Meteor.publish('posts.single', function (terms) {
|
2016-02-27 10:58:57 +09:00
|
|
|
|
2016-04-05 16:27:48 +09:00
|
|
|
check(terms, Match.OneOf({_id: String}, {_id: String, slug: Match.Any}));
|
2015-03-26 13:15:16 +09:00
|
|
|
|
2016-10-05 08:37:48 +02:00
|
|
|
const currentUser = this.userId && Users.findOne(this.userId);
|
2016-02-17 12:54:18 +09:00
|
|
|
const options = {fields: Posts.publishedFields.single};
|
2016-03-30 19:08:06 +09:00
|
|
|
const posts = Posts.find(terms._id, options);
|
2016-02-18 11:55:00 +09:00
|
|
|
const post = posts.fetch()[0];
|
2014-09-04 16:39:35 +09:00
|
|
|
|
2016-06-08 11:01:13 +09:00
|
|
|
if (post) {
|
|
|
|
const users = getSinglePostUsers(post);
|
2016-07-20 10:25:05 +09:00
|
|
|
return Users.canView(currentUser, post) ? [posts, users] : [];
|
2016-06-08 11:01:13 +09:00
|
|
|
} else {
|
2016-11-26 02:46:55 +08:00
|
|
|
console.log(`// posts.single: no post found for _id “${terms._id}”`); // eslint-disable-line
|
2016-06-08 11:01:13 +09:00
|
|
|
return [];
|
|
|
|
}
|
2015-04-20 13:57:37 +09:00
|
|
|
|
2016-11-26 02:46:55 +08:00
|
|
|
});
|