2016-11-14 20:27:23 +01:00
|
|
|
import marked from 'marked';
|
2016-11-14 17:17:44 +09:00
|
|
|
import Posts from "meteor/nova:posts";
|
|
|
|
import Comments from '../collection.js';
|
|
|
|
import Users from 'meteor/nova:users';
|
2016-12-13 11:40:24 +09:00
|
|
|
import { addCallback, Utils, getSetting } from 'meteor/nova:core';
|
2016-11-14 17:17:44 +09:00
|
|
|
|
|
|
|
// ------------------------------------- comments.new.validate -------------------------------- //
|
|
|
|
|
|
|
|
function CommentsNewRateLimit (comment, user) {
|
|
|
|
if (!Users.isAdmin(user)) {
|
|
|
|
const timeSinceLastComment = Users.timeSinceLast(user, Comments);
|
2016-12-12 15:00:56 +09:00
|
|
|
const commentInterval = Math.abs(parseInt(getSetting('commentInterval',15)));
|
2017-02-02 16:18:33 +01:00
|
|
|
|
2016-11-14 17:17:44 +09:00
|
|
|
// check that user waits more than 15 seconds between comments
|
|
|
|
if((timeSinceLastComment < commentInterval)) {
|
2017-02-02 15:15:51 +01:00
|
|
|
throw new Error(Utils.encodeIntlError({id: "comments.rate_limit_error", value: commentInterval-timeSinceLastComment}));
|
2016-11-14 17:17:44 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return comment;
|
|
|
|
}
|
2016-12-13 11:40:24 +09:00
|
|
|
addCallback("comments.new.validate", CommentsNewRateLimit);
|
2016-11-14 17:17:44 +09:00
|
|
|
|
|
|
|
// ------------------------------------- comments.new.sync -------------------------------- //
|
|
|
|
|
2016-11-14 20:27:23 +01:00
|
|
|
function CommentsNewGenerateHTMLBody (comment, user) {
|
2016-12-12 11:34:28 +09:00
|
|
|
comment.htmlBody = Utils.sanitize(marked(comment.body));
|
2016-11-14 20:27:23 +01:00
|
|
|
return comment;
|
|
|
|
}
|
2016-12-13 11:40:24 +09:00
|
|
|
addCallback("comments.new.sync", CommentsNewGenerateHTMLBody);
|
2016-11-14 17:17:44 +09:00
|
|
|
|
|
|
|
function CommentsNewOperations (comment) {
|
|
|
|
|
|
|
|
var userId = comment.userId;
|
|
|
|
|
|
|
|
// increment comment count
|
|
|
|
Users.update({_id: userId}, {
|
2017-01-18 10:18:33 +09:00
|
|
|
$inc: {'commentCount': 1}
|
2016-11-14 17:17:44 +09:00
|
|
|
});
|
|
|
|
|
|
|
|
// update post
|
|
|
|
Posts.update(comment.postId, {
|
|
|
|
$inc: {commentCount: 1},
|
|
|
|
$set: {lastCommentedAt: new Date()},
|
|
|
|
$addToSet: {commenters: userId}
|
|
|
|
});
|
|
|
|
|
|
|
|
return comment;
|
|
|
|
}
|
2017-01-23 11:01:14 +09:00
|
|
|
addCallback("comments.new.sync", CommentsNewOperations);
|