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

119 lines
3.6 KiB
JavaScript
Raw Normal View History

2016-11-17 17:38:38 +09:00
// import Telescope from 'meteor/nova:lib';
// import Posts from '../collection.js';
// // import Comments from "meteor/nova:comments";
// import Users from 'meteor/nova:users';
2016-11-17 17:38:38 +09:00
// Posts._ensureIndex({"status": 1, "postedAt": 1});
2016-11-17 17:38:38 +09:00
// // ------------------------------------- Helpers -------------------------------- //
2016-02-17 11:28:00 +09:00
2016-11-17 17:38:38 +09:00
// /**
// * @summary Get all users relevant to a list of posts
// * (authors of the listed posts, and first four commenters of each post)
// * @param {Object} posts
// */
// const getPostsListUsers = posts => {
2016-02-17 11:28:00 +09:00
2016-11-17 17:38:38 +09:00
// // add the userIds of each post authors
// let userIds = _.pluck(posts.fetch(), 'userId');
2016-02-17 11:28:00 +09:00
2016-11-17 17:38:38 +09:00
// // for each post, also add first four commenter's userIds to userIds array
// posts.forEach(function (post) {
// userIds = userIds.concat(_.first(post.commenters,4));
// });
2016-02-17 11:28:00 +09:00
2016-11-17 17:38:38 +09:00
// userIds = _.unique(userIds);
2016-02-17 11:28:00 +09:00
2016-11-17 17:38:38 +09:00
// return Users.find({_id: {$in: userIds}}, {fields: Users.publishedFields.list});
2016-02-17 11:28:00 +09:00
2016-11-17 17:38:38 +09:00
// };
// /**
// * @summary Get all users relevant to a single post
// * (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
// /*
// 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-11-17 17:38:38 +09:00
// // add upvoters
// if (post.upvoters && post.upvoters.length) {
// users = users.concat(post.upvoters);
// }
2016-02-17 11:28:00 +09:00
2016-11-17 17:38:38 +09:00
// // add downvoters
// if (post.downvoters && post.downvoters.length) {
// users = users.concat(post.downvoters);
// }
2016-02-17 11:28:00 +09:00
2016-11-17 17:38:38 +09:00
// // remove any duplicate IDs
// users = _.unique(users);
2016-02-17 11:28:00 +09:00
2016-11-17 17:38:38 +09:00
// return Users.find({_id: {$in: users}}, {fields: Users.publishedFields.list});
// };
2016-02-17 11:28:00 +09:00
2016-11-17 17:38:38 +09:00
// // ------------------------------------- Publications -------------------------------- //
2016-02-17 11:28:00 +09:00
2016-11-17 17:38:38 +09:00
// /**
// * @summary Publish a list of posts, along with the users corresponding to these posts
// * @param {Object} terms
// */
// Meteor.publish('posts.list', function (terms) {
2016-02-16 15:08:30 +09:00
2016-11-17 17:38:38 +09:00
// // this.unblock(); // causes bug where publication returns 0 results
2016-02-16 15:08:30 +09:00
2016-11-17 17:38:38 +09:00
// this.autorun(function () {
2016-06-06 16:11:51 +09:00
2016-11-17 17:38:38 +09:00
// const currentUser = this.userId && Users.findOne(this.userId);
2016-02-16 15:08:30 +09:00
2016-11-17 17:38:38 +09:00
// terms.currentUserId = this.userId; // add currentUserId to terms
// const {selector, options} = Posts.parameters.get(terms);
2016-11-17 17:38:38 +09:00
// Counts.publish(this, terms.listId, Posts.find(selector, options), {noReady: true});
2016-11-17 17:38:38 +09:00
// options.fields = Posts.publishedFields.list;
2016-11-17 17:38:38 +09:00
// const posts = Posts.find(selector, options);
2016-11-17 17:38:38 +09:00
// // note: doesn't work yet :(
// // CursorCounts.set(terms, posts.count(), this.connection.id);
2016-11-17 17:38:38 +09:00
// const users = Tracker.nonreactive(function () {
// return getPostsListUsers(posts);
// });
2016-11-17 17:38:38 +09:00
// return Users.canDo(currentUser, "posts.view.approved.all") ? [posts, users] : [];
2016-11-17 17:38:38 +09:00
// });
2016-11-17 17:38:38 +09:00
// });
2015-04-28 09:44:43 +09:00
2016-11-17 17:38:38 +09:00
// /**
// * @summary Publish a single post, along with all relevant users
// * @param {Object} terms
// */
// Meteor.publish('posts.single', function (terms) {
2016-02-27 10:58:57 +09:00
2016-11-17 17:38:38 +09:00
// check(terms, Match.OneOf({_id: String}, {_id: String, slug: Match.Any}));
2016-11-17 17:38:38 +09:00
// const currentUser = this.userId && Users.findOne(this.userId);
// 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
2016-11-17 17:38:38 +09:00
// if (post) {
// const users = getSinglePostUsers(post);
// return Users.canView(currentUser, post) ? [posts, users] : [];
// } else {
// console.log(`// posts.single: no post found for _id “${terms._id}”`)
// return [];
// }
2016-11-17 17:38:38 +09:00
// });