<commit_before>var _ = require('lodash');

/**
 * This is a collection of methods that allow you to create, update and delete social items.
 *
 * These methods all exclude the 'loggedinuser' parameter as they are all carried out only by
 * the currently logged in user and / or system level calls (e.g. adding a user via integration
 * with an SSO flow).
 *
 * TODO: Exception may be creating a post on someone elses feed.
 *
 */
module.exports = function (api) {

  var client = api.client,
      q = client.queries;

  function addFollower (keyspace, user, user_follower, timestamp, isprivate, ispersonal, backfill, next) {
    if (!next) { next = backfill; backfill = null; }
    var follow = client.generateId();
    var data = [follow, user, user_follower, timestamp, isprivate, ispersonal];
    var object = _.object(['follow', 'user', 'user_follower', 'timestamp', 'isprivate', 'ispersonal'], data);
    client.execute(q(keyspace, 'upsertFollower'), data, {prepare: true}, function (err) {
      /* istanbul ignore if */
      if (err) { return next(err); }
      alterFollowerCount(keyspace, user, 1, function () {
        api.feed.addFeedItem(keyspace, user, object, 'follow', function (err, result) {
          if (err) { return next(err); }
          var follower = {
            follow: follow,
            user: user,
            user_follower: user_follower,
            isprivate: isprivate,
            ispersonal: ispersonal,
            since: timestamp
          };
          api.user.mapUserIdToUser(keyspace, follower, ['user', 'user_follower'], user, function (err, follow) {
            if (err) return next(err);
            if (!backfill) return next(null, follow);
            api.feed.seedFeed(keyspace, user_follower, user, backfill, function (err) {
              if (err) return next(err);
              return next(null, follow);
            });
          });
        });
      });

    });
  }

  function alterFollowerCount (keyspace, user, count, next) {
    next = next || function () {};
    var data = [count, user];
    client.execute(q(keyspace, 'updateCounter', {TYPE: 'followers'}), data, {prepare: true}, next);
  }

  function followerCount (keyspace, user, next) {
    next = next || function () {};
    var data = [user];
    client.get(q(keyspace, 'selectCount', {TYPE: 'followers', ITEM: 'user'}), data, {prepare: true}, next);
  }

  function addFollowerByName (keyspace, username, username_follower, timestamp, isprivate, ispersonal, next) {
    api.user.getUserByName(keyspace, username, function (err, user) {
      if (err || !user) { return next(err); }
      api.user.getUserByName(keyspace, username_follower, function (err, follower) {
        if (err || !follower) { return next(err); }
        addFollower(keyspace, user.user, follower.user, timestamp, isprivate, ispersonal, next);
      });
    });
  }

  function removeFollower (keyspace, user, user_follower, next) {
    isFollower(keyspace, user, user_follower, function (err, isFollower, isFollowerSince, follow) {
      if (err || !isFollower) { return next(err); }
      var deleteData = [user, user_follower];
      client.execute(q(keyspace, 'removeFollower'), deleteData, {prepare: true}, function (err, result) {
        if (err) return next(err);
        alterFollowerCount(keyspace, user, -1, function () {
          api.feed.removeFeedsForItem(keyspace, follow.follow, function (err) {
            if (err) return next(err);
            next(null, {status: 'removed'});
          });
        });
      });
    });
  }
<commit_msg>Convert uuid to string to fit with more generic counter structure<commit_after>var _ = require('lodash');

/**
 * This is a collection of methods that allow you to create, update and delete social items.
 *
 * These methods all exclude the 'loggedinuser' parameter as they are all carried out only by
 * the currently logged in user and / or system level calls (e.g. adding a user via integration
 * with an SSO flow).
 *
 * TODO: Exception may be creating a post on someone elses feed.
 *
 */
module.exports = function (api) {

  var client = api.client,
      q = client.queries;

  function addFollower (keyspace, user, user_follower, timestamp, isprivate, ispersonal, backfill, next) {
    if (!next) { next = backfill; backfill = null; }
    var follow = client.generateId();
    var data = [follow, user, user_follower, timestamp, isprivate, ispersonal];
    var object = _.object(['follow', 'user', 'user_follower', 'timestamp', 'isprivate', 'ispersonal'], data);
    client.execute(q(keyspace, 'upsertFollower'), data, {prepare: true}, function (err) {
      /* istanbul ignore if */
      if (err) { return next(err); }
      alterFollowerCount(keyspace, user, 1, function () {
        api.feed.addFeedItem(keyspace, user, object, 'follow', function (err, result) {
          if (err) { return next(err); }
          var follower = {
            follow: follow,
            user: user,
            user_follower: user_follower,
            isprivate: isprivate,
            ispersonal: ispersonal,
            since: timestamp
          };
          api.user.mapUserIdToUser(keyspace, follower, ['user', 'user_follower'], user, function (err, follow) {
            if (err) return next(err);
            if (!backfill) return next(null, follow);
            api.feed.seedFeed(keyspace, user_follower, user, backfill, function (err) {
              if (err) return next(err);
              return next(null, follow);
            });
          });
        });
      });

    });
  }

  function alterFollowerCount (keyspace, user, count, next) {
    next = next || function () {};
    var data = [count, user.toString()];
    client.execute(q(keyspace, 'updateCounter', {TYPE: 'followers'}), data, {prepare: true}, next);
  }

  function followerCount (keyspace, user, next) {
    next = next || function () {};
    var data = [user.toString()];
    client.get(q(keyspace, 'selectCount', {TYPE: 'followers', ITEM: 'user'}), data, {prepare: true}, next);
  }

  function addFollowerByName (keyspace, username, username_follower, timestamp, isprivate, ispersonal, next) {
    api.user.getUserByName(keyspace, username, function (err, user) {
      if (err || !user) { return next(err); }
      api.user.getUserByName(keyspace, username_follower, function (err, follower) {
        if (err || !follower) { return next(err); }
        addFollower(keyspace, user.user, follower.user, timestamp, isprivate, ispersonal, next);
      });
    });
  }

  function removeFollower (keyspace, user, user_follower, next) {
    isFollower(keyspace, user, user_follower, function (err, isFollower, isFollowerSince, follow) {
      if (err || !isFollower) { return next(err); }
      var deleteData = [user, user_follower];
      client.execute(q(keyspace, 'removeFollower'), deleteData, {prepare: true}, function (err, result) {
        if (err) return next(err);
        alterFollowerCount(keyspace, user, -1, function () {
          api.feed.removeFeedsForItem(keyspace, follow.follow, function (err) {
            if (err) return next(err);
            next(null, {status: 'removed'});
          });
        });
      });
    });
  }

{'fraction_non_alphanumeric': 0.13533834586466165, 'fraction_numerical': 0.000556947925368978, 'mean_word_length': 2.9445359692476663, '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': '2053154', 'n_tokens_mistral': 2191, 'n_tokens_neox': 2109, 'n_words': 628}