2016-11-14 20:27:23 +01:00
|
|
|
import marked from 'marked';
|
2016-11-14 17:17:44 +09:00
|
|
|
import Telescope from 'meteor/nova:lib';
|
|
|
|
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 15:15:51 +01:00
|
|
|
console.log(timeSinceLastComment);
|
|
|
|
console.log(commentInterval);
|
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);
|
|
|
|
|
|
|
|
// ------------------------------------- comments.new.async -------------------------------- //
|
2016-11-14 17:17:44 +09:00
|
|
|
|
|
|
|
// add new comment notification callback on comment submit
|
|
|
|
function CommentsNewNotifications (comment) {
|
|
|
|
|
2016-12-02 09:19:41 +01:00
|
|
|
if (typeof Telescope.notifications !== "undefined" && !comment.isDummy) {
|
2016-11-14 17:17:44 +09:00
|
|
|
|
|
|
|
// note: dummy content has disableNotifications set to true
|
|
|
|
if(Meteor.isServer && !comment.disableNotifications){
|
|
|
|
|
|
|
|
var post = Posts.findOne(comment.postId),
|
|
|
|
postAuthor = Users.findOne(post.userId),
|
|
|
|
userIdsNotified = [],
|
|
|
|
notificationData = {
|
|
|
|
comment: _.pick(comment, '_id', 'userId', 'author', 'htmlBody', 'postId'),
|
|
|
|
post: _.pick(post, '_id', 'userId', 'title', 'url')
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
// 1. Notify author of post (if they have new comment notifications turned on)
|
|
|
|
// but do not notify author of post if they're the ones posting the comment
|
|
|
|
if (Users.getSetting(postAuthor, "notifications_comments", true) && comment.userId !== postAuthor._id) {
|
|
|
|
Telescope.notifications.create(post.userId, 'newComment', notificationData);
|
|
|
|
userIdsNotified.push(post.userId);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Notify author of comment being replied to
|
|
|
|
if (!!comment.parentCommentId) {
|
|
|
|
|
|
|
|
var parentComment = Comments.findOne(comment.parentCommentId);
|
|
|
|
|
|
|
|
// do not notify author of parent comment if they're also post author or comment author
|
|
|
|
// (someone could be replying to their own comment)
|
|
|
|
if (parentComment.userId !== post.userId && parentComment.userId !== comment.userId) {
|
|
|
|
|
|
|
|
var parentCommentAuthor = Users.findOne(parentComment.userId);
|
|
|
|
|
|
|
|
// do not notify parent comment author if they have reply notifications turned off
|
|
|
|
if (Users.getSetting(parentCommentAuthor, "notifications_replies", true)) {
|
|
|
|
|
|
|
|
// add parent comment to notification data
|
|
|
|
notificationData.parentComment = _.pick(parentComment, '_id', 'userId', 'author', 'htmlBody');
|
|
|
|
|
|
|
|
Telescope.notifications.create(parentComment.userId, 'newReply', notificationData);
|
|
|
|
userIdsNotified.push(parentComment.userId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-12-13 11:40:24 +09:00
|
|
|
addCallback("comments.new.async", CommentsNewNotifications);
|