Vulcan/packages/vulcan-posts/lib/custom_fields.js

42 lines
999 B
JavaScript
Raw Normal View History

2017-03-23 16:27:59 +09:00
import Users from "meteor/vulcan:users";
Users.addField([
/**
Count of the user's posts
*/
{
fieldName: "postCount",
fieldSchema: {
type: Number,
optional: true,
defaultValue: 0,
viewableBy: ['guests'],
}
2017-06-17 15:30:58 +09:00
},
/**
The user's associated posts (GraphQL only)
*/
{
fieldName: "posts",
fieldSchema: {
type: Array,
optional: true,
viewableBy: ['guests'],
2017-07-08 11:36:27 +09:00
resolveAs: {
fieldName: 'posts',
2017-07-14 10:37:19 +09:00
arguments: 'limit: Int = 5',
2017-07-08 11:36:27 +09:00
type: '[Post]',
resolver: (user, { limit }, { currentUser, Users, Posts }) => {
const posts = Posts.find({ userId: user._id }, { limit }).fetch();
2017-07-08 11:36:27 +09:00
// restrict documents fields
const viewablePosts = _.filter(posts, post => Posts.checkAccess(currentUser, post));
const restrictedPosts = Users.restrictViewableFields(currentUser, Posts, viewablePosts);
return restrictedPosts;
}
}
2017-06-17 15:30:58 +09:00
}
}
]);