2014-09-17 20:20:23 +02:00
|
|
|
CommentSchema = new SimpleSchema({
|
2014-09-29 10:15:21 +09:00
|
|
|
_id: {
|
|
|
|
type: String,
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
parentCommentId: {
|
|
|
|
type: String,
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
createdAt: {
|
|
|
|
type: Date,
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
postedAt: { // for now, comments are always created and posted at the same time
|
|
|
|
type: Date,
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
body: {
|
|
|
|
type: String
|
|
|
|
},
|
|
|
|
htmlBody: {
|
|
|
|
type: String,
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
baseScore: {
|
|
|
|
type: Number,
|
|
|
|
decimal: true,
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
score: {
|
|
|
|
type: Number,
|
|
|
|
decimal: true,
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
upvotes: {
|
|
|
|
type: Number,
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
upvoters: {
|
|
|
|
type: [String], // XXX
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
downvotes: {
|
|
|
|
type: Number,
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
downvoters: {
|
|
|
|
type: [String], // XXX
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
author: {
|
|
|
|
type: String,
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
inactive: {
|
|
|
|
type: Boolean,
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
postId: {
|
|
|
|
type: String, // XXX
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
userId: {
|
|
|
|
type: String, // XXX
|
|
|
|
optional: true
|
|
|
|
},
|
|
|
|
isDeleted: {
|
|
|
|
type: Boolean,
|
|
|
|
optional: true
|
|
|
|
}
|
2014-05-15 15:18:17 +09:00
|
|
|
});
|
2014-05-10 16:57:17 +09:00
|
|
|
|
2014-09-17 20:10:43 +02:00
|
|
|
Comments = new Meteor.Collection("comments");
|
2014-09-17 20:20:23 +02:00
|
|
|
Comments.attachSchema(CommentSchema);
|
2014-09-17 20:10:43 +02:00
|
|
|
|
2013-07-04 12:51:26 +09:00
|
|
|
Comments.deny({
|
|
|
|
update: function(userId, post, fieldNames) {
|
|
|
|
if(isAdminById(userId))
|
2013-07-05 07:09:15 +09:00
|
|
|
return false;
|
2013-11-05 09:32:21 +09:00
|
|
|
// deny the update if it contains something other than the following fields
|
|
|
|
return (_.without(fieldNames, 'body').length > 0);
|
2013-07-04 12:51:26 +09:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-11-05 09:32:21 +09:00
|
|
|
Comments.allow({
|
|
|
|
update: canEditById,
|
|
|
|
remove: canEditById
|
|
|
|
});
|
|
|
|
|
2014-08-31 16:11:48 +09:00
|
|
|
Comments.before.insert(function (userId, doc) {
|
2014-09-11 14:11:41 +09:00
|
|
|
if(Meteor.isServer)
|
|
|
|
doc.htmlBody = sanitize(marked(doc.body));
|
2014-08-31 16:11:48 +09:00
|
|
|
});
|
2013-11-05 09:32:21 +09:00
|
|
|
|
2014-08-31 16:11:48 +09:00
|
|
|
Comments.before.update(function (userId, doc, fieldNames, modifier, options) {
|
|
|
|
// if body is being modified, update htmlBody too
|
2014-09-11 14:11:41 +09:00
|
|
|
if (Meteor.isServer && modifier.$set && modifier.$set.body) {
|
2014-08-31 16:11:48 +09:00
|
|
|
modifier.$set = modifier.$set || {};
|
|
|
|
modifier.$set.htmlBody = sanitize(marked(modifier.$set.body));
|
|
|
|
}
|
|
|
|
});
|
2013-11-05 09:32:21 +09:00
|
|
|
|
2012-08-31 19:41:54 -04:00
|
|
|
Meteor.methods({
|
2012-10-01 11:45:18 +09:00
|
|
|
comment: function(postId, parentCommentId, text){
|
2013-04-26 17:28:09 +09:00
|
|
|
var user = Meteor.user(),
|
2014-09-16 15:18:27 -04:00
|
|
|
post = Posts.findOne(postId),
|
|
|
|
postUser = Meteor.users.findOne(post.userId),
|
|
|
|
timeSinceLastComment = timeSinceLast(user, Comments),
|
2013-04-26 17:28:09 +09:00
|
|
|
commentInterval = Math.abs(parseInt(getSetting('commentInterval',15))),
|
2014-08-04 09:47:10 +09:00
|
|
|
now = new Date();
|
2012-10-06 13:15:55 +09:00
|
|
|
|
2012-10-30 12:01:11 +09:00
|
|
|
// check that user can comment
|
2013-04-26 17:28:09 +09:00
|
|
|
if (!user || !canComment(user))
|
2014-11-19 00:00:09 +08:00
|
|
|
throw new Meteor.Error(i18n.t('you_need_to_login_or_be_invited_to_post_new_comments'));
|
2012-10-06 13:15:55 +09:00
|
|
|
|
2012-10-30 12:01:11 +09:00
|
|
|
// check that user waits more than 15 seconds between comments
|
2012-12-20 21:17:07 +01:00
|
|
|
if(!this.isSimulation && (timeSinceLastComment < commentInterval))
|
2014-11-19 00:00:09 +08:00
|
|
|
throw new Meteor.Error(704, i18n.t('please_wait')+(commentInterval-timeSinceLastComment)+i18n.t('seconds_before_commenting_again'));
|
2012-10-05 13:59:40 +09:00
|
|
|
|
2013-07-10 22:08:25 +02:00
|
|
|
// Don't allow empty comments
|
2014-08-31 16:11:48 +09:00
|
|
|
if (!text)
|
2014-11-19 00:00:09 +08:00
|
|
|
throw new Meteor.Error(704,i18n.t('your_comment_is_empty'));
|
2013-07-10 22:08:25 +02:00
|
|
|
|
2012-08-31 19:41:54 -04:00
|
|
|
var comment = {
|
2014-06-22 10:34:39 +09:00
|
|
|
postId: postId,
|
2014-08-31 16:11:48 +09:00
|
|
|
body: text,
|
2014-06-22 10:34:39 +09:00
|
|
|
userId: user._id,
|
2014-07-19 15:08:28 +09:00
|
|
|
createdAt: now,
|
|
|
|
postedAt: now,
|
2014-06-22 10:34:39 +09:00
|
|
|
upvotes: 0,
|
|
|
|
downvotes: 0,
|
|
|
|
baseScore: 0,
|
|
|
|
score: 0,
|
|
|
|
author: getDisplayName(user)
|
2012-08-31 19:41:54 -04:00
|
|
|
};
|
2013-04-26 17:28:09 +09:00
|
|
|
|
2012-10-01 11:45:18 +09:00
|
|
|
if(parentCommentId)
|
2014-07-05 16:21:28 +09:00
|
|
|
comment.parentCommentId = parentCommentId;
|
2012-08-31 19:41:54 -04:00
|
|
|
|
2014-09-20 09:57:09 +09:00
|
|
|
|
|
|
|
// ------------------------------ Callbacks ------------------------------ //
|
|
|
|
|
|
|
|
// run all post submit server callbacks on comment object successively
|
|
|
|
comment = commentSubmitMethodCallbacks.reduce(function(result, currentFunction) {
|
|
|
|
return currentFunction(result);
|
|
|
|
}, comment);
|
|
|
|
|
|
|
|
// -------------------------------- Insert ------------------------------- //
|
|
|
|
|
2014-09-20 10:42:42 +09:00
|
|
|
comment._id = Comments.insert(comment);
|
|
|
|
|
|
|
|
// ------------------------------ Callbacks ------------------------------ //
|
|
|
|
|
|
|
|
// run all post submit server callbacks on comment object successively
|
|
|
|
comment = commentAfterSubmitMethodCallbacks.reduce(function(result, currentFunction) {
|
|
|
|
return currentFunction(result);
|
|
|
|
}, comment);
|
2013-10-23 10:21:08 +08:00
|
|
|
|
2013-11-08 11:10:23 +09:00
|
|
|
// increment comment count
|
2014-08-22 17:06:22 +09:00
|
|
|
Meteor.users.update({_id: user._id}, {
|
2014-10-16 00:32:08 +02:00
|
|
|
$inc: {'commentCount': 1}
|
2014-08-22 17:06:22 +09:00
|
|
|
});
|
2013-11-08 11:10:23 +09:00
|
|
|
|
2014-08-22 10:31:05 +09:00
|
|
|
Posts.update(postId, {
|
2014-10-16 00:32:08 +02:00
|
|
|
$inc: {commentCount: 1},
|
2014-08-22 10:31:05 +09:00
|
|
|
$set: {lastCommentedAt: now},
|
|
|
|
$addToSet: {commenters: user._id}
|
|
|
|
});
|
2012-09-20 08:09:45 +09:00
|
|
|
|
2013-11-03 13:23:11 +09:00
|
|
|
Meteor.call('upvoteComment', comment);
|
2012-10-01 11:45:18 +09:00
|
|
|
|
2014-08-04 11:22:43 +09:00
|
|
|
return comment;
|
2012-10-03 15:59:03 +09:00
|
|
|
},
|
|
|
|
removeComment: function(commentId){
|
2014-09-16 15:18:27 -04:00
|
|
|
var comment = Comments.findOne(commentId);
|
2013-10-04 19:51:34 +09:00
|
|
|
if(canEdit(Meteor.user(), comment)){
|
2014-08-22 10:31:05 +09:00
|
|
|
// decrement post comment count and remove user ID from post
|
|
|
|
Posts.update(comment.postId, {
|
2014-10-16 00:32:08 +02:00
|
|
|
$inc: {commentCount: -1},
|
2014-08-22 10:31:05 +09:00
|
|
|
$pull: {commenters: comment.userId}
|
|
|
|
});
|
2013-11-08 11:10:23 +09:00
|
|
|
|
2014-08-22 17:06:22 +09:00
|
|
|
// decrement user comment count and remove comment ID from user
|
|
|
|
Meteor.users.update({_id: comment.userId}, {
|
2014-10-16 00:32:08 +02:00
|
|
|
$inc: {'commentCount': -1}
|
2014-08-22 17:06:22 +09:00
|
|
|
});
|
2013-11-08 11:10:23 +09:00
|
|
|
|
2013-10-04 19:51:34 +09:00
|
|
|
// note: should we also decrease user's comment karma ?
|
2014-09-16 12:12:48 +09:00
|
|
|
// We don't actually delete the comment to avoid losing all child comments.
|
|
|
|
// Instead, we give it a special flag
|
|
|
|
Comments.update({_id: commentId}, {$set: {
|
|
|
|
body: 'Deleted',
|
|
|
|
htmlBody: 'Deleted',
|
|
|
|
isDeleted: true
|
|
|
|
}});
|
2013-10-04 19:51:34 +09:00
|
|
|
}else{
|
Replace "throwError" with "flashMessage" and type
Currently, ``throwError`` is used for all manner of messages, including
errors, "success" messages, and "info" messages. This makes appropriate
styling of the error message difficult. In addition, the name
``throwError`` seems to create confusion, implying that an error will
actually be thrown (e.g. stopping execution when a user isn't logged in
[0][1]), when in fact it just displays a message.
Replace ``throwError`` with ``flashMessage``, and reliably include a
message "type" (e.g. "error", "success", "info") every time. rename
``lib/errors.js`` to ``lib/messages.js`` to more accurately reflect its
function.
This commit doesn't rename the message collection (``Errors``), nor the
template responsible for rendering the messages (``error_item.html``) --
that should probably still be done, but has higher likelihood of
trouble for existing alternate themes and installations.
[0] https://github.com/TelescopeJS/Telescope/blob/6ccf7d7d4704d6a8e821fe48128f81c19983ffc9/client/views/users/user_edit.js#L43
[1] https://github.com/TelescopeJS/Telescope/blob/083a4c4dc48eca15fe9d4472e24e6b4e8adfc8d6/client/views/users/user_email.js#L13
2014-11-05 13:12:09 -07:00
|
|
|
flashMessage("You don't have permission to delete this comment.", "error");
|
2013-10-04 19:51:34 +09:00
|
|
|
}
|
2012-08-31 19:41:54 -04:00
|
|
|
}
|
|
|
|
});
|