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

119 lines
3.4 KiB
JavaScript
Raw Normal View History

2016-08-08 11:18:21 +09:00
import Telescope from 'meteor/nova:lib';
2016-06-23 12:17:39 +09:00
import Posts from '../collection.js';
// import Comments from "meteor/nova:comments";
2016-06-23 15:00:58 +09:00
import Users from 'meteor/nova:users';
Posts._ensureIndex({"status": 1, "postedAt": 1});
2016-02-17 11:28:00 +09:00
// ------------------------------------- Helpers -------------------------------- //
/**
* @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);
return Users.find({_id: {$in: userIds}}, {fields: Users.publishedFields.list});
2016-02-17 11:28:00 +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-06-23 12:17:39 +09:00
/*
NOTE: to avoid circular dependencies between nova:posts and nova:comments,
use callback hook to get comment authors
*/
users = Telescope.callbacks.run("posts.single.getUsers", users, post);
2016-05-12 11:49:15 +09: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);
return Users.find({_id: {$in: users}}, {fields: Users.publishedFields.list});
2016-02-17 11:28:00 +09:00
};
// ------------------------------------- Publications -------------------------------- //
/**
* @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-02-17 17:46:34 +09: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-06-06 16:11:51 +09:00
const currentUser = this.userId && Users.findOne(this.userId);
2016-02-16 15:08:30 +09:00
terms.currentUserId = this.userId; // add currentUserId to terms
const {selector, options} = Posts.parameters.get(terms);
2016-06-06 15:03:02 +09:00
Counts.publish(this, terms.listId, Posts.find(selector, options), {noReady: true});
options.fields = Posts.publishedFields.list;
const posts = Posts.find(selector, options);
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);
const users = Tracker.nonreactive(function () {
return getPostsListUsers(posts);
});
2016-07-20 10:25:05 +09:00
return Users.canDo(currentUser, "posts.view.approved.all") ? [posts, users] : [];
2016-03-28 11:43:42 +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
/**
* @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}));
const currentUser = this.userId && Users.findOne(this.userId);
2016-02-17 12:54:18 +09:00
const options = {fields: Posts.publishedFields.single};
const posts = Posts.find(terms._id, options);
const post = posts.fetch()[0];
2014-09-04 16:39:35 +09:00
if (post) {
const users = getSinglePostUsers(post);
2016-07-20 10:25:05 +09:00
return Users.canView(currentUser, post) ? [posts, users] : [];
} else {
console.log(`// posts.single: no post found for _id “${terms._id}`)
return [];
}
2016-02-17 11:28:00 +09:00
});