Vulcan/packages/nova-comments/lib/callbacks/callbacks_comments_new.js
xavcz b67989fbc2 work on notifications
* Telescope.notifications.create -> import { createNotifications } from 'meteor/nova:notifications' ;
* leverage Meteor's weak dependencies on packages & move code related to emails / notifications from nova:posts et al. to nova:notifications
2017-02-16 10:04:00 +01:00

50 lines
1.6 KiB
JavaScript

import marked from 'marked';
import Telescope from 'meteor/nova:lib';
import Posts from "meteor/nova:posts";
import Comments from '../collection.js';
import Users from 'meteor/nova:users';
import { addCallback, Utils, getSetting } from 'meteor/nova:core';
// ------------------------------------- comments.new.validate -------------------------------- //
function CommentsNewRateLimit (comment, user) {
if (!Users.isAdmin(user)) {
const timeSinceLastComment = Users.timeSinceLast(user, Comments);
const commentInterval = Math.abs(parseInt(getSetting('commentInterval',15)));
// check that user waits more than 15 seconds between comments
if((timeSinceLastComment < commentInterval)) {
throw new Error(Utils.encodeIntlError({id: "comments.rate_limit_error", value: commentInterval-timeSinceLastComment}));
}
}
return comment;
}
addCallback("comments.new.validate", CommentsNewRateLimit);
// ------------------------------------- comments.new.sync -------------------------------- //
function CommentsNewGenerateHTMLBody (comment, user) {
comment.htmlBody = Utils.sanitize(marked(comment.body));
return comment;
}
addCallback("comments.new.sync", CommentsNewGenerateHTMLBody);
function CommentsNewOperations (comment) {
var userId = comment.userId;
// increment comment count
Users.update({_id: userId}, {
$inc: {'commentCount': 1}
});
// update post
Posts.update(comment.postId, {
$inc: {commentCount: 1},
$set: {lastCommentedAt: new Date()},
$addToSet: {commenters: userId}
});
return comment;
}
addCallback("comments.new.sync", CommentsNewOperations);