2017-04-07 16:06:33 +09:00
|
|
|
import { GraphQLSchema, Utils } from 'meteor/vulcan:core';
|
|
|
|
import Users from 'meteor/vulcan:users';
|
2016-11-03 21:39:09 +09:00
|
|
|
|
2017-01-16 12:24:59 +09:00
|
|
|
const specificResolvers = {
|
2016-11-03 21:39:09 +09:00
|
|
|
Post: {
|
2017-04-15 12:03:25 +09:00
|
|
|
async user(post, args, context) {
|
2017-04-15 16:30:31 +09:00
|
|
|
if (!post.userId) return null;
|
2017-04-15 12:03:25 +09:00
|
|
|
const user = await context.Users.loader.load(post.userId, `Post.user (${post.title})`);
|
|
|
|
return context.Users.restrictViewableFields(context.currentUser, context.Users, user);
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
|
|
|
},
|
2017-02-01 16:37:06 +01:00
|
|
|
Mutation: {
|
2017-03-05 13:10:16 +00:00
|
|
|
increasePostViewCount(root, { postId }, context) {
|
|
|
|
return context.Posts.update({_id: postId}, { $inc: { viewCount: 1 }});
|
2017-02-01 16:37:06 +01:00
|
|
|
}
|
|
|
|
}
|
2016-11-22 18:14:51 -05:00
|
|
|
};
|
|
|
|
|
2017-01-16 12:24:59 +09:00
|
|
|
GraphQLSchema.addResolvers(specificResolvers);
|
2016-11-22 18:14:51 -05:00
|
|
|
|
|
|
|
const resolvers = {
|
|
|
|
|
|
|
|
list: {
|
|
|
|
|
|
|
|
name: 'postsList',
|
|
|
|
|
2017-04-15 12:03:25 +09:00
|
|
|
check(user, terms, Posts) {
|
|
|
|
const {selector} = Posts.getParameters(terms);
|
2017-04-19 12:19:17 +09:00
|
|
|
if (selector.status) {
|
|
|
|
const statuses = selector.status['$in'] ? selector.status['$in'] : [selector.status];
|
|
|
|
const statusesLabel = statuses.map(status => _.findWhere(Posts.statuses, {value: status}).label)
|
|
|
|
return _.every(statusesLabel, label => Users.canDo(user, `posts.view.${label}.all`));
|
|
|
|
} else {
|
|
|
|
return Users.canDo(user, `posts.view.approved.all`);
|
|
|
|
}
|
2017-04-07 16:06:33 +09:00
|
|
|
},
|
|
|
|
|
2017-04-15 16:30:31 +09:00
|
|
|
resolver(root, {terms}, {currentUser, Users, Posts}, info) {
|
2017-04-15 12:03:25 +09:00
|
|
|
|
|
|
|
// check that the current user can access the current query terms
|
|
|
|
Utils.performCheck(this, currentUser, terms, Posts);
|
|
|
|
|
|
|
|
// get selector and options from terms and perform Mongo query
|
|
|
|
let {selector, options} = Posts.getParameters(terms);
|
2017-03-05 13:10:16 +00:00
|
|
|
options.limit = (terms.limit < 1 || terms.limit > 100) ? 100 : terms.limit;
|
|
|
|
options.skip = terms.offset;
|
2017-04-15 12:03:25 +09:00
|
|
|
const posts = Posts.find(selector, options).fetch();
|
|
|
|
|
|
|
|
// restrict documents fields
|
|
|
|
const restrictedPosts = Users.restrictViewableFields(currentUser, Posts, posts);
|
2017-04-07 16:06:33 +09:00
|
|
|
|
2017-04-15 12:03:25 +09:00
|
|
|
// prime the cache
|
|
|
|
restrictedPosts.forEach(post => Posts.loader.prime(post._id, post));
|
2017-04-07 16:06:33 +09:00
|
|
|
|
2017-04-15 12:03:25 +09:00
|
|
|
return restrictedPosts;
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
2016-11-22 18:14:51 -05:00
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
single: {
|
|
|
|
|
|
|
|
name: 'postsSingle',
|
2016-11-30 16:55:34 +09:00
|
|
|
|
2017-04-07 17:13:12 +09:00
|
|
|
check(user, document, collection) {
|
|
|
|
if (!document) return false;
|
|
|
|
const status = _.findWhere(collection.statuses, {value: document.status});
|
2017-04-07 16:06:33 +09:00
|
|
|
return Users.owns(user, document) ? Users.canDo(user, `posts.view.${status.label}.own`) : Users.canDo(user, `posts.view.${status.label}.all`);
|
|
|
|
},
|
|
|
|
|
2017-04-15 16:30:31 +09:00
|
|
|
async resolver(root, {documentId, slug}, {currentUser, Users, Posts}) {
|
2017-04-07 16:06:33 +09:00
|
|
|
|
2017-04-15 16:30:31 +09:00
|
|
|
// don't use Dataloader if post is selected by slug
|
|
|
|
const post = documentId ? await Posts.loader.load(documentId) : Posts.findOne({slug});
|
2017-04-07 16:06:33 +09:00
|
|
|
|
2017-04-15 16:30:31 +09:00
|
|
|
Utils.performCheck(this, currentUser, post, Posts);
|
2017-04-07 16:06:33 +09:00
|
|
|
|
2017-04-15 16:30:31 +09:00
|
|
|
return Users.restrictViewableFields(currentUser, Posts, post);
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
2016-11-22 18:14:51 -05:00
|
|
|
|
2016-11-03 21:39:09 +09:00
|
|
|
},
|
2016-11-08 15:04:17 +09:00
|
|
|
|
2016-11-22 18:14:51 -05:00
|
|
|
total: {
|
|
|
|
|
|
|
|
name: 'postsTotal',
|
|
|
|
|
2017-04-15 16:30:31 +09:00
|
|
|
resolver(root, {terms}, {Posts}) {
|
|
|
|
const {selector} = Posts.getParameters(terms);
|
|
|
|
return Posts.find(selector).count();
|
2016-11-22 18:14:51 -05:00
|
|
|
},
|
|
|
|
|
|
|
|
}
|
|
|
|
};
|
2016-11-18 16:01:27 +09:00
|
|
|
|
2017-01-05 15:53:41 +01:00
|
|
|
export default resolvers;
|