Vulcan/packages/example-forum/lib/modules/posts/custom_fields.js

47 lines
1,014 B
JavaScript
Raw Normal View History

2017-09-04 18:37:21 +09:00
/*
Custom fields on Users collection
*/
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'],
}
},
/**
The user's associated posts (GraphQL only)
*/
{
fieldName: 'posts',
fieldSchema: {
type: Array,
optional: true,
viewableBy: ['guests'],
resolveAs: {
arguments: 'limit: Int = 5',
type: '[Post]',
resolver: (user, { limit }, { currentUser, Users, Posts }) => {
const posts = Posts.find({ userId: user._id }, { limit }).fetch();
// restrict documents fields
const viewablePosts = _.filter(posts, post => Posts.checkAccess(currentUser, post));
const restrictedPosts = Users.restrictViewableFields(currentUser, Posts, viewablePosts);
return restrictedPosts;
}
}
}
}
]);