Vulcan/packages/vulcan-voting/lib/modules/make_voteable.js

132 lines
3.2 KiB
JavaScript
Raw Normal View History

export const VoteableCollections = [];
export const makeVoteable = collection => {
VoteableCollections.push(collection);
collection.addField([
/**
2017-09-25 22:09:09 +02:00
The current user's votes on the document, if they exists
*/
{
2017-09-25 22:09:09 +02:00
fieldName: 'currentUserVotes',
fieldSchema: {
2017-09-25 22:09:09 +02:00
type: Array,
optional: true,
viewableBy: ['guests'],
2017-09-25 22:09:09 +02:00
resolveAs: {
type: '[Vote]',
resolver: async (document, args, { Users, Votes, currentUser }) => {
2017-09-27 17:15:49 +02:00
if (!currentUser) return [];
2017-09-25 22:09:09 +02:00
const votes = Votes.find({userId: currentUser._id, itemId: document._id}).fetch();
2017-09-27 17:15:49 +02:00
if (!votes.length) return [];
2017-09-25 22:09:09 +02:00
return votes;
// return Users.restrictViewableFields(currentUser, Votes, votes);
},
}
}
},
{
fieldName: 'currentUserVotes.$',
fieldSchema: {
type: Object,
optional: true,
}
},
/**
2017-09-25 22:09:09 +02:00
All votes on the document
*/
{
2017-09-25 22:09:09 +02:00
fieldName: 'allVotes',
fieldSchema: {
type: Array,
optional: true,
viewableBy: ['guests'],
resolveAs: {
2017-09-25 22:09:09 +02:00
type: 'Vote',
resolver: async (document, args, { Users, Votes, currentUser }) => {
const votes = Votes.find({itemId: document._id}).fetch();
if (!votes.length) return null;
return votes;
// return Users.restrictViewableFields(currentUser, Votes, votes);
},
2017-09-25 22:09:09 +02:00
}
}
},
{
2017-09-25 22:09:09 +02:00
fieldName: 'allVotes.$',
fieldSchema: {
2017-09-25 22:09:09 +02:00
type: Object,
optional: true,
}
},
/**
2017-09-25 22:09:09 +02:00
An array containing the `_id`s of the document's upvoters
*/
{
2017-09-25 22:09:09 +02:00
fieldName: 'voters',
fieldSchema: {
type: Array,
optional: true,
viewableBy: ['guests'],
resolveAs: {
type: '[User]',
resolver: async (document, args, {currentUser, Users}) => {
2017-09-25 22:09:09 +02:00
const votes = Votes.find({itemId: document._id}).fetch();
const votersIds = _.pluck(votes, 'userId');
const voters = Users.find({_id: {$in: votersIds}});
return voters;
// if (!document.upvoters) return [];
// const upvoters = await Users.loader.loadMany(document.upvoters);
// return Users.restrictViewableFields(currentUser, Users, upvoters);
},
},
}
},
{
2017-09-25 22:09:09 +02:00
fieldName: 'voters.$',
fieldSchema: {
type: String,
2017-09-25 22:09:09 +02:00
optional: true
}
},
/**
The document's base score (not factoring in the document's age)
*/
{
fieldName: 'baseScore',
fieldSchema: {
type: Number,
optional: true,
defaultValue: 0,
viewableBy: ['guests'],
}
},
/**
The document's current score (factoring in age)
*/
{
fieldName: 'score',
fieldSchema: {
type: Number,
optional: true,
defaultValue: 0,
viewableBy: ['guests'],
}
},
/**
Whether the document is inactive. Inactive documents see their score recalculated less often
*/
{
fieldName: 'inactive',
fieldSchema: {
type: Boolean,
optional: true,
onInsert: () => false
}
},
]);
}