2018-02-11 13:18:06 +09:00
|
|
|
import { Connectors, getSetting } from 'meteor/vulcan:core'; // import from vulcan:lib because vulcan:core isn't loaded yet
|
|
|
|
|
|
|
|
const database = getSetting('database', 'mongo');
|
|
|
|
|
2017-09-04 20:56:03 +09:00
|
|
|
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-04 20:56:03 +09:00
|
|
|
*/
|
|
|
|
{
|
2017-09-25 22:09:09 +02:00
|
|
|
fieldName: 'currentUserVotes',
|
2017-09-04 20:56:03 +09:00
|
|
|
fieldSchema: {
|
2017-09-25 22:09:09 +02:00
|
|
|
type: Array,
|
2017-09-04 20:56:03 +09:00
|
|
|
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 [];
|
2018-02-11 13:18:06 +09:00
|
|
|
const votes = await Connectors[database].find(Votes, {userId: currentUser._id, documentId: document._id});
|
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-04 20:56:03 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
2017-09-25 22:09:09 +02:00
|
|
|
All votes on the document
|
2017-09-04 20:56:03 +09:00
|
|
|
*/
|
|
|
|
{
|
2017-09-25 22:09:09 +02:00
|
|
|
fieldName: 'allVotes',
|
2017-09-04 20:56:03 +09:00
|
|
|
fieldSchema: {
|
|
|
|
type: Array,
|
|
|
|
optional: true,
|
|
|
|
viewableBy: ['guests'],
|
|
|
|
resolveAs: {
|
2018-01-26 20:15:47 -07:00
|
|
|
type: '[Vote]',
|
2017-09-25 22:09:09 +02:00
|
|
|
resolver: async (document, args, { Users, Votes, currentUser }) => {
|
2018-02-11 13:18:06 +09:00
|
|
|
const votes = await Connectors[database].find(Votes, { documentId: document._id });
|
2018-01-26 20:15:47 -07:00
|
|
|
if (!votes.length) return [];
|
2017-09-25 22:09:09 +02:00
|
|
|
return votes;
|
|
|
|
// return Users.restrictViewableFields(currentUser, Votes, votes);
|
2017-09-04 20:56:03 +09:00
|
|
|
},
|
2017-09-25 22:09:09 +02:00
|
|
|
|
|
|
|
}
|
2017-09-04 20:56:03 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-09-25 22:09:09 +02:00
|
|
|
fieldName: 'allVotes.$',
|
2017-09-04 20:56:03 +09:00
|
|
|
fieldSchema: {
|
2017-09-25 22:09:09 +02:00
|
|
|
type: Object,
|
2017-09-04 20:56:03 +09:00
|
|
|
optional: true,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
2017-09-25 22:09:09 +02:00
|
|
|
An array containing the `_id`s of the document's upvoters
|
2017-09-04 20:56:03 +09:00
|
|
|
*/
|
|
|
|
{
|
2017-09-25 22:09:09 +02:00
|
|
|
fieldName: 'voters',
|
2017-09-04 20:56:03 +09:00
|
|
|
fieldSchema: {
|
|
|
|
type: Array,
|
|
|
|
optional: true,
|
|
|
|
viewableBy: ['guests'],
|
|
|
|
resolveAs: {
|
|
|
|
type: '[User]',
|
2018-01-29 03:55:57 +01:00
|
|
|
resolver: async (document, args, { currentUser, Users }) => {
|
|
|
|
// eslint-disable-next-line no-undef
|
2018-02-11 13:18:06 +09:00
|
|
|
const votes = await Connectors[database].find(Votes, {itemId: document._id});
|
2017-09-25 22:09:09 +02:00
|
|
|
const votersIds = _.pluck(votes, 'userId');
|
2018-01-29 03:55:57 +01:00
|
|
|
// eslint-disable-next-line no-undef
|
2018-02-11 13:18:06 +09:00
|
|
|
const voters = await Connectors[database].find(Users, {_id: {$in: votersIds}});
|
2017-09-25 22:09:09 +02:00
|
|
|
return voters;
|
|
|
|
// if (!document.upvoters) return [];
|
|
|
|
// const upvoters = await Users.loader.loadMany(document.upvoters);
|
|
|
|
// return Users.restrictViewableFields(currentUser, Users, upvoters);
|
2017-09-04 20:56:03 +09:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
2017-09-25 22:09:09 +02:00
|
|
|
fieldName: 'voters.$',
|
2017-09-04 20:56:03 +09:00
|
|
|
fieldSchema: {
|
|
|
|
type: String,
|
2017-09-25 22:09:09 +02:00
|
|
|
optional: true
|
2017-09-04 20:56:03 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
The document's base score (not factoring in the document's age)
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
fieldName: 'baseScore',
|
|
|
|
fieldSchema: {
|
|
|
|
type: Number,
|
|
|
|
optional: true,
|
|
|
|
defaultValue: 0,
|
|
|
|
viewableBy: ['guests'],
|
2018-01-30 11:56:35 +09:00
|
|
|
onInsert: document => {
|
|
|
|
// default to 0 if empty
|
|
|
|
return document.baseScore || 0;
|
|
|
|
}
|
2017-09-04 20:56:03 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
The document's current score (factoring in age)
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
fieldName: 'score',
|
|
|
|
fieldSchema: {
|
|
|
|
type: Number,
|
|
|
|
optional: true,
|
|
|
|
defaultValue: 0,
|
|
|
|
viewableBy: ['guests'],
|
2018-01-30 11:56:35 +09:00
|
|
|
onInsert: document => {
|
|
|
|
// default to 0 if empty
|
|
|
|
return document.score || 0;
|
|
|
|
}
|
2017-09-04 20:56:03 +09:00
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
Whether the document is inactive. Inactive documents see their score recalculated less often
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
fieldName: 'inactive',
|
|
|
|
fieldSchema: {
|
|
|
|
type: Boolean,
|
|
|
|
optional: true,
|
|
|
|
onInsert: () => false
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
]);
|
|
|
|
}
|