<commit_before>var _ = require('lodash');
var async = require('async');
var debug = require('debug')('seguir:comment');

module.exports = function (api) {
  var client = api.client;
  var q = client.queries;

  function createComment (keyspace, user, post, commented, commentdata, next) {
    var newCommentRow = [client.generateId(), user, post, commented, commentdata];
    var newComment = _.zipObject(['comment', 'user', 'post', 'commented', 'commentdata'], newCommentRow);

    async.parallel([
      function getNewComment (cb) {
        api.user.mapUserIdToUser(keyspace, newComment, ['user'], cb);
      },
      function insertComment (cb) {
        debug('insert into comments:', 'comments', newCommentRow);
        client.execute(q(keyspace, 'insertComment'), newCommentRow, {}, cb);
      },
      function insertCommentsTimeline (cb) {
        var newCommentTimelineRow = [post, client.generateTimeId(commented), newComment.comment];
        debug('insert into comments timeline:', 'comments_timeline', newCommentTimelineRow);
        client.execute(q(keyspace, 'insertCommentsTimeline'), newCommentTimelineRow, {}, cb);
      }
    ], function (err, results) {
      if (err) { return next(err); }

      var countUpdate = [1, post.toString()];
      debug('update comment counts:', 'counts', countUpdate);
      client.execute(q(keyspace, 'updateCounter', {TYPE: 'comment'}), countUpdate, {cacheKey: 'count:comment:' + post}, function (err) {
        if (err) { return next(err); }

        api.metrics.increment('comment.add');
        next(null, results[0]);
      });
    });
  }

  function updateComment (keyspace, user, comment, commentdata, next) {
    debug('select comment:', 'comments', [comment]);
    client.get(q(keyspace, 'selectComment'), [comment], {cacheKey: 'comment:' + comment}, function (err, commentRecord) {
      if (err) { return next(err); }
      if (commentRecord.user.toString() !== user.toString()) {
        return next(new Error('Unable to update comment created by user ' + commentRecord.user));
      }

      commentRecord.commentdata = commentdata;
      async.parallel([
        function (cb) {
          api.user.mapUserIdToUser(keyspace, commentRecord, ['user'], cb);
        },
        function (cb) {
          var commentUpdate = [commentdata, comment];
          debug('update comment:', 'comments', commentUpdate);
          client.execute(q(keyspace, 'updateComment'), commentUpdate, {cacheKey: 'comment:' + comment}, cb);
        }
      ], function (err, results) {
        if (err) { return next(err); }

        next(null, results[0]);
      });
    });
  }

  function deleteComment (keyspace, user, comment, next) {
    client.get(q(keyspace, 'selectComment'), [comment], {cacheKey: 'comment:' + comment}, function (err, commentRecord) {
      if (err) { return next(err); }
      if (commentRecord.user.toString() !== user.toString()) {
        return next(new Error('Unable to delete comment created by user ' + commentRecord.user));
      }

      var commentDeletion = [comment];
      debug('delete comment:', 'comments', commentDeletion);
      client.execute(q(keyspace, 'deleteComment'), commentDeletion, {cacheKey: 'comment:' + comment}, function (err) {
        if (err) { return next(err); }

        var countUpdate = [-1, commentRecord.post.toString()];
        debug('update comment counts:', 'counts', countUpdate);
        client.execute(q(keyspace, 'updateCounter', {TYPE: 'comment'}), countUpdate, {cacheKey: 'count:comment:' + commentRecord.post}, next);
      });
    });
  }

  // returns upto latest 5000 (default fetchSize of cassandra-driver) comments for a post
  function getComments (keyspace, liu, post, next) {
    client.get(q(keyspace, 'selectCount', {TYPE: 'comment'}), [post.toString()], {cacheKey: 'count:comment:' + post}, function (err, result) {
      if (err) { return next(err); }

      if (result && +result.count > 0) {
        api.metrics.increment('comments_timeline.list');
        debug('select comments timeline:', 'comments_timeline', [post]);
        client.execute(q(keyspace, 'selectCommentsTimeline'), [post], {}, function (err, timeline, nextPageState) {
          if (err) { return next(err); }

          async.mapSeries(timeline, function (timelineEntry, cb) {
            debug('select comment:', 'comments', [timelineEntry.comment]);
            client.get(q(keyspace, 'selectComment'), [timelineEntry.comment], {cacheKey: 'comment:' + timelineEntry.comment}, function (err, comment) {
              if (err || !comment) { return cb(err); }

              api.like.checkLike(keyspace, liu, comment.comment, function (err, likeStatus) {
                if (err) { return cb(err); }

                comment.userLiked = likeStatus.userLiked;
                comment.likedTotal = likeStatus.likedTotal;
                cb(null, comment);
              });
            });
          }, function (err, comments) {
            if (err) { return next(err); }

            api.user.mapUserIdToUser(keyspace, comments.filter(_.identity), ['user'], function (err, commentsWithUser) {<commit_msg>return updated/new comment along with userLiked and likedTotal field<commit_after>var _ = require('lodash');
var async = require('async');
var debug = require('debug')('seguir:comment');

module.exports = function (api) {
  var client = api.client;
  var q = client.queries;

  function getComment (keyspace, liu, comment, next) {
    debug('select comment:', 'comments', [comment]);
    client.get(q(keyspace, 'selectComment'), [comment], {cacheKey: 'comment:' + comment}, function (err, result) {
      if (err || !result) { return next(err); }

      api.like.checkLike(keyspace, liu, comment, function (err, likeStatus) {
        if (err) { return next(err); }

        result.userLiked = likeStatus.userLiked;
        result.likedTotal = likeStatus.likedTotal;
        next(null, result);
      });
    });
  }

  function createComment (keyspace, user, post, commented, commentdata, next) {
    var comment = client.generateId();
    var newCommentRow = [comment, user, post, commented, commentdata];

    async.parallel([
      function insertComment (cb) {
        debug('insert into comments:', 'comments', newCommentRow);
        client.execute(q(keyspace, 'insertComment'), newCommentRow, {}, cb);
      },
      function insertCommentsTimeline (cb) {
        var newCommentTimelineRow = [post, client.generateTimeId(commented), comment];
        debug('insert into comments timeline:', 'comments_timeline', newCommentTimelineRow);
        client.execute(q(keyspace, 'insertCommentsTimeline'), newCommentTimelineRow, {}, cb);
      }
    ], function (err, results) {
      if (err) { return next(err); }

      var countUpdate = [1, post.toString()];
      debug('update comment counts:', 'counts', countUpdate);
      client.execute(q(keyspace, 'updateCounter', {TYPE: 'comment'}), countUpdate, {cacheKey: 'count:comment:' + post}, function (err) {
        if (err) { return next(err); }

        api.metrics.increment('comment.add');
        getComment(keyspace, user, comment, function (err, newComment) {
          if (err) { return next(err); }
          api.user.mapUserIdToUser(keyspace, newComment, ['user'], next);
        });
      });
    });
  }

  function updateComment (keyspace, user, comment, commentdata, next) {
    debug('select comment:', 'comments', [comment]);
    client.get(q(keyspace, 'selectComment'), [comment], {cacheKey: 'comment:' + comment}, function (err, commentRecord) {
      if (err) { return next(err); }
      if (commentRecord.user.toString() !== user.toString()) {
        return next(new Error('Unable to update comment created by user ' + commentRecord.user));
      }

      var commentUpdate = [commentdata, comment];
      debug('update comment:', 'comments', commentUpdate);
      client.execute(q(keyspace, 'updateComment'), commentUpdate, {cacheKey: 'comment:' + comment}, function (err) {
        if (err) { return next(err); }

        getComment(keyspace, user, comment, function (err, updatedComment) {
          if (err) { return next(err); }
          api.user.mapUserIdToUser(keyspace, updatedComment, ['user'], next);
        });
      });
    });
  }

  function deleteComment (keyspace, user, comment, next) {
    client.get(q(keyspace, 'selectComment'), [comment], {cacheKey: 'comment:' + comment}, function (err, commentRecord) {
      if (err) { return next(err); }
      if (commentRecord.user.toString() !== user.toString()) {
        return next(new Error('Unable to delete comment created by user ' + commentRecord.user));
      }

      var commentDeletion = [comment];
      debug('delete comment:', 'comments', commentDeletion);
      client.execute(q(keyspace, 'deleteComment'), commentDeletion, {cacheKey: 'comment:' + comment}, function (err) {
        if (err) { return next(err); }

        var countUpdate = [-1, commentRecord.post.toString()];
        debug('update comment counts:', 'counts', countUpdate);
        client.execute(q(keyspace, 'updateCounter', {TYPE: 'comment'}), countUpdate, {cacheKey: 'count:comment:' + commentRecord.post}, next);
      });
    });
  }

  // returns upto latest 5000 (default fetchSize of cassandra-driver) comments for a post
  function getComments (keyspace, liu, post, next) {
    client.get(q(keyspace, 'selectCount', {TYPE: 'comment'}), [post.toString()], {cacheKey: 'count:comment:' + post}, function (err, result) {
      if (err) { return next(err); }

      if (result && +result.count > 0) {
        api.metrics.increment('comments_timeline.list');
        debug('select comments timeline:', 'comments_timeline', [post]);
        client.execute(q(keyspace, 'selectCommentsTimeline'), [post], {}, function (err, timeline, nextPageState) {
          if (err) { return next(err); }

          async.mapSeries(timeline, function (timelineEntry, cb) {
            getComment(keyspace, liu, timelineEntry.comment, cb);
          }, function (err, comments) {
            if (err) { return next(err); }

            api.user.mapUserIdToUser(keyspace, comments.filter(_.identity), ['user'], function (err, commentsWithUser) {
{'fraction_non_alphanumeric': 0.1546259842519685, 'fraction_numerical': 0.0015748031496062992, 'mean_word_length': 3.396797922977066, 'pattern_counts': {'":': 0, '<': 3, '<?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': '6232612', 'n_tokens_mistral': 2950, 'n_tokens_neox': 2873, 'n_words': 731}