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

213 lines
4.4 KiB
JavaScript
Raw Normal View History

import Posts from '../posts/index.js';
import Comments from '../comments/index.js';
Posts.addField([
/**
How many upvotes the post has received
*/
{
fieldName: 'upvotes',
fieldSchema: {
type: Number,
optional: true,
defaultValue: 0,
viewableBy: ['guests'],
}
},
/**
An array containing the `_id`s of the post's upvoters
*/
{
fieldName: 'upvoters',
fieldSchema: {
type: Array,
optional: true,
viewableBy: ['guests'],
resolveAs: {
fieldName: 'upvoters',
type: '[User]',
resolver: async (post, args, {currentUser, Users}) => {
if (!post.upvoters) return [];
const upvoters = await Users.loader.loadMany(post.upvoters);
return Users.restrictViewableFields(currentUser, Users, upvoters);
},
},
}
},
{
fieldName: 'upvoters.$',
fieldSchema: {
type: String,
optional: true
}
},
/**
How many downvotes the post has received
*/
{
fieldName: 'downvotes',
fieldSchema: {
type: Number,
optional: true,
defaultValue: 0,
viewableBy: ['guests'],
}
},
/**
An array containing the `_id`s of the post's downvoters
*/
{
fieldName: 'downvoters',
fieldSchema: {
type: Array,
optional: true,
viewableBy: ['guests'],
resolveAs: {
fieldName: 'downvoters',
type: '[User]',
resolver: async (post, args, {currentUser, Users}) => {
if (!post.downvoters) return [];
const downvoters = await Users.loader.loadMany(post.downvoters);
return Users.restrictViewableFields(currentUser, Users, downvoters);
},
},
}
},
{
fieldName: 'downvoters.$',
fieldSchema: {
type: String,
optional: true,
}
},
/**
The post's base score (not factoring in the post's age)
*/
{
fieldName: 'baseScore',
fieldSchema: {
type: Number,
optional: true,
defaultValue: 0,
viewableBy: ['guests'],
}
},
/**
The post's current score (factoring in age)
*/
{
fieldName: 'score',
fieldSchema: {
type: Number,
optional: true,
defaultValue: 0,
viewableBy: ['guests'],
}
},
]);
Comments.addField([
/**
The number of upvotes the comment has received
*/
{
fieldName: 'upvotes',
fieldSchema: {
type: Number,
optional: true,
defaultValue: 0,
viewableBy: ['guests'],
}
},
/**
An array containing the `_id`s of upvoters
*/
{
fieldName: 'upvoters',
fieldSchema: {
type: Array,
optional: true,
viewableBy: ['guests'],
resolveAs: {
fieldName: 'upvoters',
type: '[User]',
resolver: async (comment, args, {currentUser, Users}) => {
if (!comment.upvoters) return [];
const upvoters = await Users.loader.loadMany(comment.upvoters);
return Users.restrictViewableFields(currentUser, Users, upvoters);
},
},
}
},
{
fieldName: 'upvoters.$',
fieldSchema: {
type: String,
optional: true
}
},
/**
The number of downvotes the comment has received
*/
{
fieldName: 'downvotes',
fieldSchema: {
type: Number,
optional: true,
defaultValue: 0,
viewableBy: ['guests'],
}
},
/**
An array containing the `_id`s of downvoters
*/
{
fieldName: 'downvoters',
fieldSchema: {
type: Array,
optional: true,
viewableBy: ['guests'],
resolveAs: {
fieldName:'downvoters',
type: '[User]',
resolver: async (comment, args, {currentUser, Users}) => {
if (!comment.downvoters) return [];
const downvoters = await Users.loader.loadMany(comment.downvoters);
return Users.restrictViewableFields(currentUser, Users, downvoters);
},
},
}
},
{
fieldName: 'downvoters.$',
fieldSchema: {
type: String,
optional: true,
}
},
/**
The comment's base score (not factoring in the comment's age)
*/
{
fieldName: 'baseScore',
fieldSchema: {
type: Number,
optional: true,
defaultValue: 0,
viewableBy: ['guests'],
}
},
/**
The comment's current score (factoring in age)
*/
{
fieldName: 'score',
fieldSchema: {
type: Number,
optional: true,
defaultValue: 0,
viewableBy: ['guests'],
}
},
]);