<commit_before>Comments = new Meteor.Collection('comments');

Comments.allow({
    insert: canCommentById
  , update: canEditById
  , remove: canEditById
});

Comments.deny({
  update: function(userId, post, fieldNames) {
    if(isAdminById(userId))
      return false;
    // may only edit the following fields:
    return (_.without(fieldNames, 'text').length > 0);
  }
});

Meteor.methods({
  comment: function(postId, parentCommentId, text){
    var user = Meteor.user(),
        post=Posts.findOne(postId),
        postUser=Meteor.users.findOne(post.userId),
        timeSinceLastComment=timeSinceLast(user, Comments),
        cleanText= cleanUp(text),
        commentInterval = Math.abs(parseInt(getSetting('commentInterval',15))),
        properties={
          'commentAuthorId': user._id,
          'commentAuthorName': getDisplayName(user),
          'commentExcerpt': trimWords(stripMarkdown(cleanText),20),
          'postId': postId,
          'postHeadline' : post.headline
        };

    // check that user can comment
    if (!user || !canComment(user))
      throw new Meteor.Error('You need to login or be invited to post new comments.');
    
    // check that user waits more than 15 seconds between comments
    if(!this.isSimulation && (timeSinceLastComment < commentInterval))
      throw new Meteor.Error(704, 'Please wait '+(commentInterval-timeSinceLastComment)+' seconds before commenting again');

    // Don't allow empty comments
    if (!cleanText)
      throw new Meteor.Error(704,'Your comment is empty.');
          
    var comment = {
        post: postId,
        body: cleanText,
        userId: user._id,
        submitted: new Date().getTime(),
        author: getDisplayName(user)
    };
    
    if(parentCommentId)
      comment.parent = parentCommentId;

    var newCommentId=Comments.insert(comment);

    Posts.update(postId, {$inc: {comments: 1}});

    Meteor.call('upvoteComment', newCommentId);

    properties.commentId = newCommentId;

    if(!this.isSimulation){
      if(parentCommentId){
        // child comment
        var parentComment=Comments.findOne(parentCommentId);
        var parentUser=Meteor.users.findOne(parentComment.userId);

        properties.parentCommentId = parentCommentId;
        properties.parentAuthorId = parentComment.userId;
        properties.parentAuthorName = getDisplayName(parentUser);

        // reply notification
        // do not notify users of their own actions (i.e. they're replying to themselves)
        if(parentUser._id != user._id){
          Meteor.call('createNotification', {
            event: 'newReply', 
            properties: properties, 
            userToNotify: parentUser, 
            userDoingAction: user, 
            sendEmail: getUserSetting('notifications.replies', false, parentUser)
          });<commit_msg>fixed comment voting bug<commit_after>Comments = new Meteor.Collection('comments');

Comments.allow({
    insert: canCommentById
  , update: canEditById
  , remove: canEditById
});

Comments.deny({
  update: function(userId, post, fieldNames) {
    if(isAdminById(userId))
      return false;
    // may only edit the following fields:
    return (_.without(fieldNames, 'text').length > 0);
  }
});

Meteor.methods({
  comment: function(postId, parentCommentId, text){
    var user = Meteor.user(),
        post=Posts.findOne(postId),
        postUser=Meteor.users.findOne(post.userId),
        timeSinceLastComment=timeSinceLast(user, Comments),
        cleanText= cleanUp(text),
        commentInterval = Math.abs(parseInt(getSetting('commentInterval',15))),
        properties={
          'commentAuthorId': user._id,
          'commentAuthorName': getDisplayName(user),
          'commentExcerpt': trimWords(stripMarkdown(cleanText),20),
          'postId': postId,
          'postHeadline' : post.headline
        };

    // check that user can comment
    if (!user || !canComment(user))
      throw new Meteor.Error('You need to login or be invited to post new comments.');
    
    // check that user waits more than 15 seconds between comments
    if(!this.isSimulation && (timeSinceLastComment < commentInterval))
      throw new Meteor.Error(704, 'Please wait '+(commentInterval-timeSinceLastComment)+' seconds before commenting again');

    // Don't allow empty comments
    if (!cleanText)
      throw new Meteor.Error(704,'Your comment is empty.');
          
    var comment = {
        post: postId,
        body: cleanText,
        userId: user._id,
        submitted: new Date().getTime(),
        author: getDisplayName(user)
    };
    
    if(parentCommentId)
      comment.parent = parentCommentId;

    var newCommentId=Comments.insert(comment);

    // extend comment with newly created _id
    comment = _.extend(comment, {_id: newCommentId});

    Posts.update(postId, {$inc: {comments: 1}});

    Meteor.call('upvoteComment', comment);

    properties.commentId = newCommentId;

    if(!this.isSimulation){
      if(parentCommentId){
        // child comment
        var parentComment=Comments.findOne(parentCommentId);
        var parentUser=Meteor.users.findOne(parentComment.userId);

        properties.parentCommentId = parentCommentId;
        properties.parentAuthorId = parentComment.userId;
        properties.parentAuthorName = getDisplayName(parentUser);

        // reply notification
        // do not notify users of their own actions (i.e. they're replying to themselves)
        if(parentUser._id != user._id){
          Meteor.call('createNotification', {
            event: 'newReply', 
            properties: properties, 
            userToNotify: parentUser, 
            userDoingAction: user, 
            sendEmail: getUserSetting('notifications.replies', false, parentUser)
          });
{'fraction_non_alphanumeric': 0.11205403533079321, 'fraction_numerical': 0.0048493245583650845, 'mean_word_length': 2.896761133603239, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1841643', 'n_tokens_mistral': 1650, 'n_tokens_neox': 1578, 'n_words': 375}