2017-03-23 16:27:59 +09:00
|
|
|
import Users from 'meteor/vulcan:users';
|
2016-06-23 15:00:58 +09:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @summary Comments schema
|
2016-11-22 18:14:51 -05:00
|
|
|
* @type {Object}
|
2016-06-23 15:00:58 +09:00
|
|
|
*/
|
2016-11-22 18:14:51 -05:00
|
|
|
const schema = {
|
2016-06-23 15:00:58 +09:00
|
|
|
/**
|
|
|
|
ID
|
|
|
|
*/
|
|
|
|
_id: {
|
|
|
|
type: String,
|
|
|
|
optional: true,
|
2016-12-06 10:55:47 +01:00
|
|
|
viewableBy: ['guests'],
|
2016-06-23 15:00:58 +09:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
The `_id` of the parent comment, if there is one
|
|
|
|
*/
|
|
|
|
parentCommentId: {
|
|
|
|
type: String,
|
|
|
|
// regEx: SimpleSchema.RegEx.Id,
|
|
|
|
max: 500,
|
2016-12-06 10:55:47 +01:00
|
|
|
viewableBy: ['guests'],
|
|
|
|
insertableBy: ['members'],
|
2016-06-23 15:00:58 +09:00
|
|
|
optional: true,
|
2017-07-08 11:36:27 +09:00
|
|
|
resolveAs: {
|
|
|
|
fieldName: 'parentComment',
|
|
|
|
type: 'Comment',
|
|
|
|
resolver: async (comment, args, {currentUser, Users, Comments}) => {
|
|
|
|
if (!comment.parentCommentId) return null;
|
|
|
|
const parentComment = await Comments.loader.load(comment.parentCommentId);
|
|
|
|
return Users.restrictViewableFields(currentUser, Comments, parentComment);
|
2017-07-08 11:43:43 +09:00
|
|
|
},
|
2017-07-14 10:37:19 +09:00
|
|
|
addOriginalField: true
|
2017-07-08 11:36:27 +09:00
|
|
|
},
|
2016-11-18 10:03:01 +01:00
|
|
|
hidden: true // never show this
|
2016-06-23 15:00:58 +09:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
The `_id` of the top-level parent comment, if there is one
|
|
|
|
*/
|
|
|
|
topLevelCommentId: {
|
|
|
|
type: String,
|
|
|
|
// regEx: SimpleSchema.RegEx.Id,
|
|
|
|
max: 500,
|
2016-12-06 10:55:47 +01:00
|
|
|
viewableBy: ['guests'],
|
|
|
|
insertableBy: ['members'],
|
2016-06-23 15:00:58 +09:00
|
|
|
optional: true,
|
2017-07-08 11:36:27 +09:00
|
|
|
resolveAs: {
|
|
|
|
fieldName: 'topLevelComment',
|
|
|
|
type: 'Comment',
|
|
|
|
resolver: async (comment, args, {currentUser, Users, Comments}) => {
|
|
|
|
if (!comment.topLevelCommentId) return null;
|
|
|
|
const topLevelComment = await Comments.loader.load(comment.topLevelCommentId);
|
|
|
|
return Users.restrictViewableFields(currentUser, Comments, topLevelComment);
|
|
|
|
},
|
2017-07-14 10:37:19 +09:00
|
|
|
addOriginalField: true
|
2017-07-08 11:36:27 +09:00
|
|
|
},
|
2016-11-18 10:03:01 +01:00
|
|
|
hidden: true // never show this
|
2016-06-23 15:00:58 +09:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
The timestamp of comment creation
|
|
|
|
*/
|
|
|
|
createdAt: {
|
|
|
|
type: Date,
|
|
|
|
optional: true,
|
2016-12-06 10:55:47 +01:00
|
|
|
viewableBy: ['admins'],
|
2017-04-28 09:24:28 +09:00
|
|
|
onInsert: (document, currentUser) => {
|
|
|
|
return new Date();
|
2016-11-14 17:17:44 +09:00
|
|
|
}
|
2016-06-23 15:00:58 +09:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
The timestamp of the comment being posted. For now, comments are always created and posted at the same time
|
|
|
|
*/
|
|
|
|
postedAt: {
|
|
|
|
type: Date,
|
|
|
|
optional: true,
|
2016-12-08 23:48:16 +01:00
|
|
|
viewableBy: ['guests'],
|
2017-04-28 09:24:28 +09:00
|
|
|
onInsert: (document, currentUser) => {
|
|
|
|
return new Date();
|
2016-11-14 17:17:44 +09:00
|
|
|
}
|
2016-06-23 15:00:58 +09:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
The comment body (Markdown)
|
|
|
|
*/
|
|
|
|
body: {
|
|
|
|
type: String,
|
|
|
|
max: 3000,
|
2016-12-06 10:55:47 +01:00
|
|
|
viewableBy: ['guests'],
|
|
|
|
insertableBy: ['members'],
|
|
|
|
editableBy: ['members'],
|
2016-06-23 15:00:58 +09:00
|
|
|
control: "textarea"
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
The HTML version of the comment body
|
|
|
|
*/
|
|
|
|
htmlBody: {
|
|
|
|
type: String,
|
|
|
|
optional: true,
|
2016-12-06 10:55:47 +01:00
|
|
|
viewableBy: ['guests'],
|
2016-06-23 15:00:58 +09:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
The comment author's name
|
|
|
|
*/
|
|
|
|
author: {
|
|
|
|
type: String,
|
|
|
|
optional: true,
|
2016-12-06 10:55:47 +01:00
|
|
|
viewableBy: ['guests'],
|
2017-04-28 09:24:28 +09:00
|
|
|
onEdit: (modifier, document, currentUser) => {
|
2016-11-14 17:17:44 +09:00
|
|
|
// if userId is changing, change the author name too
|
2017-04-28 09:24:28 +09:00
|
|
|
if (modifier.$set && modifier.$set.userId) {
|
|
|
|
return Users.getDisplayNameById(modifier.$set.userId)
|
|
|
|
}
|
2016-11-14 17:17:44 +09:00
|
|
|
}
|
2016-06-23 15:00:58 +09:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
Whether the comment is inactive. Inactive comments' scores gets recalculated less often
|
|
|
|
*/
|
|
|
|
inactive: {
|
|
|
|
type: Boolean,
|
|
|
|
optional: true,
|
2016-12-06 10:55:47 +01:00
|
|
|
viewableBy: ['guests'],
|
2016-06-23 15:00:58 +09:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
The post's `_id`
|
|
|
|
*/
|
|
|
|
postId: {
|
|
|
|
type: String,
|
|
|
|
optional: true,
|
2016-12-06 10:55:47 +01:00
|
|
|
viewableBy: ['guests'],
|
|
|
|
insertableBy: ['members'],
|
2016-06-23 15:00:58 +09:00
|
|
|
// regEx: SimpleSchema.RegEx.Id,
|
|
|
|
max: 500,
|
2017-07-08 11:36:27 +09:00
|
|
|
resolveAs: {
|
|
|
|
fieldName: 'post',
|
|
|
|
type: 'Post',
|
|
|
|
resolver: async (comment, args, {currentUser, Users, Posts}) => {
|
|
|
|
if (!comment.postId) return null;
|
|
|
|
const post = await Posts.loader.load(comment.postId);
|
|
|
|
return Users.restrictViewableFields(currentUser, Posts, post);
|
|
|
|
},
|
2017-07-14 10:37:19 +09:00
|
|
|
addOriginalField: true
|
2017-07-08 11:36:27 +09:00
|
|
|
},
|
2016-11-18 10:03:01 +01:00
|
|
|
hidden: true // never show this
|
2016-06-23 15:00:58 +09:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
The comment author's `_id`
|
|
|
|
*/
|
|
|
|
userId: {
|
|
|
|
type: String,
|
|
|
|
optional: true,
|
2016-12-06 10:55:47 +01:00
|
|
|
viewableBy: ['guests'],
|
|
|
|
insertableBy: ['members'],
|
2016-11-18 10:03:01 +01:00
|
|
|
hidden: true,
|
2017-07-08 11:36:27 +09:00
|
|
|
resolveAs: {
|
|
|
|
fieldName: 'user',
|
|
|
|
type: 'User',
|
|
|
|
resolver: async (comment, args, {currentUser, Users}) => {
|
|
|
|
if (!comment.userId) return null;
|
|
|
|
const user = await Users.loader.load(comment.userId);
|
|
|
|
return Users.restrictViewableFields(currentUser, Users, user);
|
2017-07-08 11:43:43 +09:00
|
|
|
},
|
2017-07-14 10:37:19 +09:00
|
|
|
addOriginalField: true
|
2017-07-08 11:36:27 +09:00
|
|
|
},
|
2016-06-23 15:00:58 +09:00
|
|
|
},
|
|
|
|
/**
|
2016-11-26 02:46:55 +08:00
|
|
|
Whether the comment is deleted. Delete comments' content doesn't appear on the site.
|
2016-06-23 15:00:58 +09:00
|
|
|
*/
|
|
|
|
isDeleted: {
|
|
|
|
type: Boolean,
|
|
|
|
optional: true,
|
2016-12-06 10:55:47 +01:00
|
|
|
viewableBy: ['guests'],
|
2016-06-23 15:00:58 +09:00
|
|
|
},
|
|
|
|
userIP: {
|
|
|
|
type: String,
|
|
|
|
optional: true,
|
2016-12-06 10:55:47 +01:00
|
|
|
viewableBy: ['admins'],
|
2016-06-23 15:00:58 +09:00
|
|
|
},
|
|
|
|
userAgent: {
|
|
|
|
type: String,
|
|
|
|
optional: true,
|
2016-12-06 10:55:47 +01:00
|
|
|
viewableBy: ['admins'],
|
2016-06-23 15:00:58 +09:00
|
|
|
},
|
|
|
|
referrer: {
|
|
|
|
type: String,
|
|
|
|
optional: true,
|
2016-12-06 10:55:47 +01:00
|
|
|
viewableBy: ['admins'],
|
2017-08-20 17:01:57 +09:00
|
|
|
},
|
|
|
|
|
|
|
|
// GraphQL only fields
|
|
|
|
|
|
|
|
pageUrl: {
|
|
|
|
type: String,
|
|
|
|
optional: true,
|
|
|
|
resolveAs: {
|
|
|
|
fieldName: 'pageUrl',
|
|
|
|
type: 'String',
|
|
|
|
resolver: (comment, args, context) => {
|
|
|
|
return context.Comments.getPageUrl(comment, true);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
2016-11-22 18:14:51 -05:00
|
|
|
};
|
2016-06-23 15:00:58 +09:00
|
|
|
|
2016-11-22 18:14:51 -05:00
|
|
|
export default schema;
|