2016-08-08 11:18:21 +09:00
import Telescope from 'meteor/nova:lib' ;
2016-06-23 15:00:58 +09:00
import Users from 'meteor/nova:users' ;
2016-11-22 18:14:51 -05:00
import mutations from './mutations.js' ;
2016-06-23 15:00:58 +09:00
2016-07-21 09:40:05 +09:00
// check if user can create a new comment
const canInsert = user => Users . canDo ( user , "comments.new" ) ;
// check if user can edit a comment
2016-11-22 18:14:51 -05:00
const canEdit = mutations . edit . check ;
2016-07-21 09:40:05 +09:00
// check if user can edit *all* comments
2016-11-22 18:14:51 -05:00
const canEditAll = user => Users . canDo ( user , "comments.edit.all" ) ; // we don't use the mutations.edit check here, to be changed later with ability to give options to mutations.edit.check?
2016-07-21 09:40:05 +09:00
2016-10-29 16:02:43 +09:00
const alwaysPublic = user => true ;
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 ,
publish : true ,
2016-10-29 16:02:43 +09:00
viewableIf : alwaysPublic ,
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-10-29 16:02:43 +09:00
viewableIf : alwaysPublic ,
2016-07-21 09:40:05 +09:00
insertableIf : canInsert ,
2016-06-23 15:00:58 +09:00
optional : true ,
publish : true ,
2016-11-08 14:56:39 +09:00
resolveAs : 'parentComment: Comment' ,
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-10-29 16:02:43 +09:00
viewableIf : alwaysPublic ,
2016-07-21 09:40:05 +09:00
insertableIf : canInsert ,
2016-06-23 15:00:58 +09:00
optional : true ,
publish : true ,
2016-11-08 14:56:39 +09:00
resolveAs : 'topLevelComment: Comment' ,
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-10-29 16:02:43 +09:00
publish : false ,
viewableIf : canEditAll ,
2016-11-14 17:17:44 +09:00
autoValue : ( documentOrModifier ) => {
if ( documentOrModifier && ! documentOrModifier . $set ) return new Date ( ) // if this is an insert, set createdAt to current timestamp
}
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 ,
publish : true ,
2016-11-14 17:17:44 +09:00
viewableIf : alwaysPublic ,
autoValue : ( documentOrModifier ) => {
if ( documentOrModifier && ! documentOrModifier . $set ) return new Date ( ) // if this is an insert, set createdAt to current timestamp
}
2016-06-23 15:00:58 +09:00
} ,
/ * *
The comment body ( Markdown )
* /
body : {
type : String ,
max : 3000 ,
2016-10-29 16:02:43 +09:00
viewableIf : alwaysPublic ,
2016-07-21 09:40:05 +09:00
insertableIf : canInsert ,
editableIf : canEdit ,
2016-06-23 15:00:58 +09:00
publish : true ,
control : "textarea"
} ,
/ * *
The HTML version of the comment body
* /
htmlBody : {
type : String ,
optional : true ,
publish : true ,
2016-10-29 16:02:43 +09:00
viewableIf : alwaysPublic ,
2016-06-23 15:00:58 +09:00
} ,
/ * *
The comment author ' s name
* /
author : {
type : String ,
optional : true ,
publish : true ,
2016-10-29 16:02:43 +09:00
viewableIf : alwaysPublic ,
2016-11-14 17:17:44 +09:00
autoValue : ( documentOrModifier ) => {
// if userId is changing, change the author name too
const userId = documentOrModifier . userId || documentOrModifier . $set && documentOrModifier . $set . userId
if ( userId ) return Users . getDisplayNameById ( userId )
}
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 ,
publish : true ,
2016-10-29 16:02:43 +09:00
viewableIf : alwaysPublic ,
2016-06-23 15:00:58 +09:00
} ,
/ * *
The post ' s ` _id `
* /
postId : {
type : String ,
optional : true ,
publish : true ,
2016-10-29 16:02:43 +09:00
viewableIf : alwaysPublic ,
2016-11-08 13:49:41 +01:00
insertableIf : canInsert ,
2016-06-23 15:00:58 +09:00
// regEx: SimpleSchema.RegEx.Id,
max : 500 ,
2016-11-08 14:56:39 +09:00
resolveAs : 'post: Post' ,
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 ,
publish : true ,
2016-10-29 16:02:43 +09:00
viewableIf : alwaysPublic ,
2016-11-18 09:28:32 +01:00
insertableIf : canInsert ,
2016-11-18 10:03:01 +01:00
hidden : true ,
2016-11-18 09:28:32 +01:00
resolveAs : 'user: User' ,
2016-11-15 15:59:34 +09:00
// join: {
// joinAs: "user",
// collection: () => Users
// },
2016-06-23 15:00:58 +09:00
} ,
/ * *
Whether the comment is deleted . Delete comments ' content doesn' t appear on the site .
* /
isDeleted : {
type : Boolean ,
optional : true ,
publish : true ,
2016-10-29 16:02:43 +09:00
viewableIf : alwaysPublic ,
2016-06-23 15:00:58 +09:00
} ,
userIP : {
type : String ,
optional : true ,
2016-10-29 16:02:43 +09:00
publish : false ,
viewableIf : canEditAll ,
2016-06-23 15:00:58 +09:00
} ,
userAgent : {
type : String ,
optional : true ,
2016-10-29 16:02:43 +09:00
publish : false ,
viewableIf : canEditAll ,
2016-06-23 15:00:58 +09:00
} ,
referrer : {
type : String ,
optional : true ,
2016-10-29 16:02:43 +09:00
publish : false ,
viewableIf : canEditAll ,
2016-06-23 15:00:58 +09:00
}
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 ;
2016-06-23 15:00:58 +09:00
2016-11-22 18:14:51 -05:00
// todo: move to nova:notifications
// if (typeof Telescope.notifications !== "undefined") {
// Comments.addField({
// fieldName: 'disableNotifications',
// fieldSchema: {
// type: Boolean,
// optional: true,
// hidden: true // never show this
// }
// });
// }