<commit_before>Meteor.methods({
  upvoteComment: function (comment) {
    var user = Meteor.user();
    var userId = user._id;
    var commentId = comment._id;

    if (!comment)
      throw new Meteor.Error('invalid-content', 'This content does not exist.');

    if (!user || !canUpvote(user, comment))
      throw new Meteor.Error('duplicate-content', 'This content already exists.');

    // in case user is upvoting a previously downvoted comment, cancel downvote first
    // cancelDownvote(collection, comment, user);

    // votes/score
    var result = Comments.update({ '_id': commentId, 'upvoters': { $ne: userId } }, {
      $addToSet: { 'upvoters': userId },
      $inc: { 'upvotes': 1 }
    });

    if (!!result) {
      // add comment to list of user's upvoted items
      Meteor.users.update(userId, { $addToSet: { 'activity.upvotedComments': commentId } });

      // if the comment is upvoted by owner, don't modify reputation
      if (comment.userId !== userId) 
        Meteor.users.update(userId, { $inc: { 'stats.reputation': 1 } });
    }
  },
  cancelUpvoteComment: function (comment) {
    var user = Meteor.user();
    var userId = user._id;
    var commentId = comment._id;

    // if user isn't among the upvoters, abort
    if (!comment || !user || canUpvote(user, comment))
      throw new Meteor.Error('invalid-content', 'This content does not exist.');

    // votes/score
    var result = Comments.update({ '_id': commentId, 'upvoters': userId }, {
      $pull: { 'upvoters': userId },
      $inc: { 'upvotes': -1 }
    });

    if (!!result) {
      // Remove item from list of upvoted items
      Meteor.users.update(userId, { $pull: { 'activity.upvotedComments': commentId } });

      // if the item is upvoted by owner, don't modify reputation
      if (comment.userId !== userId) 
        Meteor.users.update(userId, { $inc: { 'stats.reputation': -1 } });
    }
  },
  vote: function (topic, side) {
    var user = Meteor.user();
    var userId = user._id;
    var topicId = topic._id;

    if (!topic || !user || !canUpvote(user))
      throw new Meteor.Error('invalid-content', 'This content does not exist.');

    // in case user voted already, cancel previous vote
    if (topic.proUsers && _.contains(topic.proUsers, userId))
      var field = 'pro';
    else if (topic.conUsers && _.contains(topic.conUsers, userId))
      var field = 'con';

    if (field) {
      Topics.update({ _id: topicId }, {
        $pull: setProperty({}, field + 'Users', userId),
        $inc: { field: -1 }
      });  
    }

    Topics.update({ _id: topicId }, {
      $addToSet: setProperty({}, side + 'Users', userId),
      $inc: { side: 1 }
    });

    // store voting history in user activity ?
  }
});<commit_msg>Fix topic voting, guard against undefined params<commit_after>Meteor.methods({
  upvoteComment: function (comment) {
    if (!comment)
      throw new Meteor.Error('invalid-content', 'This content does not exist.');

    var user = Meteor.user();
    var userId = this.userId;
    var commentId = comment._id;

    if (!user || !canUpvote(user, comment))
      throw new Meteor.Error('duplicate-content', 'This content already exists.');

    // in case user is upvoting a previously downvoted comment, cancel downvote first
    // cancelDownvote(collection, comment, user);

    // votes/score
    var result = Comments.update({ '_id': commentId, 'upvoters': { $ne: userId } }, {
      $addToSet: { 'upvoters': userId },
      $inc: { 'upvotes': 1 }
    });

    if (!!result) {
      // add comment to list of user's upvoted items
      Meteor.users.update(userId, { $addToSet: { 'activity.upvotedComments': commentId } });

      // if the comment is upvoted by owner, don't modify reputation
      if (comment.userId !== userId) 
        Meteor.users.update(userId, { $inc: { 'stats.reputation': 1 } });
    }
  },
  cancelUpvoteComment: function (comment) {
    if (!comment)
      throw new Meteor.Error('invalid-content', 'This content does not exist.');

    var user = Meteor.user();
    var userId = this.userId;
    var commentId = comment._id;

    // if user isn't among the upvoters, abort
    if (!user || canUpvote(user, comment))
      throw new Meteor.Error('invalid-content', 'This content does not exist.');

    // votes/score
    var result = Comments.update({ '_id': commentId, 'upvoters': userId }, {
      $pull: { 'upvoters': userId },
      $inc: { 'upvotes': -1 }
    });

    if (!!result) {
      // Remove item from list of upvoted items
      Meteor.users.update(userId, { $pull: { 'activity.upvotedComments': commentId } });

      // if the item is upvoted by owner, don't modify reputation
      if (comment.userId !== userId) 
        Meteor.users.update(userId, { $inc: { 'stats.reputation': -1 } });
    }
  },
  vote: function (topic, side) {
    if (!topic || !side)
      throw new Meteor.Error('invalid-content', 'This content does not exist.');

    var user = Meteor.user();
    var userId = this.userId;
    var topicId = topic._id;

    if (!user || !canUpvote(user))
      throw new Meteor.Error('invalid-content', 'This content already exists.');

    // in case user voted already, cancel previous vote
    if (topic.proUsers && _.contains(topic.proUsers, userId))
      var field = 'pro';
    else if (topic.conUsers && _.contains(topic.conUsers, userId))
      var field = 'con';

    if (field) {
      Topics.update(topicId, {
        $pull: setProperty({}, field + 'Users', userId),
        $inc: setProperty({}, field, -1) // need the function to convert variable key
      });  
    }

    Topics.update(topicId, {
      $addToSet: setProperty({}, side + 'Users', userId),
      $inc: setProperty({}, side, 1)
    });

    // store voting history in user activity ?
  }
});
{'fraction_non_alphanumeric': 0.15096952908587258, 'fraction_numerical': 0.002077562326869806, 'mean_word_length': 2.751298701298701, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '4360559', 'n_tokens_mistral': 1836, 'n_tokens_neox': 1802, 'n_words': 527}