<commit_before>Hash = require 'hashish'

getEpoch = ->
  date = new Date()
  return date.getTime()

Amico =
  redis: null
  namespace: 'amico'
  followingKey: 'following'
  followersKey: 'followers'
  blockedKey: 'blocked'
  reciprocatedKey: 'reciprocated'
  pageSize: 25

  configure: (config) ->
   config.apply(@)

  follow: (fromId, toId, callback) ->
    if fromId == toId
      if callback?
        callback(false)
      else
        return false

    self = @

    @isBlocked toId, fromId, (result) =>
      if result == true
        if callback?
          return callback(false)
        else
          return false
      else
        @redis.multi()
          .zadd("#{@namespace}:#{@followingKey}:#{fromId}", getEpoch(), toId)
          .zadd("#{@namespace}:#{@followersKey}:#{toId}", getEpoch(), fromId)
          .exec (err, replies) ->
            self.isReciprocated fromId, toId, (result) ->
              self.redis.multi()
                .zadd("#{self.namespace}:#{self.reciprocatedKey}:#{fromId}", getEpoch(), toId)
                .zadd("#{self.namespace}:#{self.reciprocatedKey}:#{toId}", getEpoch(), fromId)
                .exec (err, replies) ->
                  if callback?
                    callback(true)

  unfollow: (fromId, toId, callback) ->
    if fromId == toId
      if callback?
        callback(false)
      else
        return false

    @redis.multi()
      .zrem("#{@namespace}:#{@followingKey}:#{fromId}", toId)
      .zrem("#{@namespace}:#{@followersKey}:#{toId}", fromId)
      .zrem("#{@namespace}:#{@reciprocatedKey}:#{fromId}", toId)
      .zrem("#{@namespace}:#{@reciprocatedKey}:#{toId}", fromId)
      .exec (err, replies) ->
        if callback?
          callback(true)

  block: (fromId, toId, callback) ->
    if fromId == toId
      if callback?
        callback(false)
      else
        return false

    @redis.multi()
      .zrem("#{@namespace}:#{@followingKey}:#{fromId}", toId)
      .zrem("#{@namespace}:#{@followingKey}:#{toId}", fromId)
      .zrem("#{@namespace}:#{@followersKey}:#{fromId}", toId)
      .zrem("#{@namespace}:#{@followersKey}:#{toId}", fromId)
      .zrem("#{@namespace}:#{@reciprocatedKey}:#{fromId}", toId)
      .zrem("#{@namespace}:#{@reciprocatedKey}:#{toId}", fromId)
      .zadd("#{@namespace}:#{@blockedKey}:#{fromId}", getEpoch(), toId)
      .exec (err, replies) ->
        if err?
          if callback?
            callback(false)
        else if callback?
          callback(true)

  unblock: (fromId, toId, callback) ->
    if !callback?
      callback = ->
    if fromId == toId
      return false

    @redis.zrem("#{@namespace}:#{@blockedKey}:#{fromId}", toId, callback)

  following_count: (id, callback) ->
    @redis.zcard("#{@namespace}:#{@followingKey}:#{id}", callback)

  followers_count: (id, callback) ->
    @redis.zcard("#{@namespace}:#{@followersKey}:#{id}", callback)

  blocked_count: (id, callback) ->
    @redis.zcard("#{@namespace}:#{@blockedKey}:#{id}", callback)

  reciprocated_count: (id, callback) ->
    @redis.zcard("#{@namespace}:#{@reciprocatedKey}:#{id}", callback)



  isFollowing: (id, followingId, callback) ->
    @redis.zscore "#{@namespace}:#{@followingKey}:#{id}", followingId, (err, score) ->
      callback((score != null))

  isFollower: (id, followerId, callback) ->
    @redis.zscore "#{@namespace}:#{@followersKey}:#{id}", followerId, (err, score) ->
      callback((score != null))

  isBlocked: (id, blockedId, callback) ->
    @redis.zscore "#{@namespace}:#{@blockedKey}:#{id}", blockedId, (err, score) ->
      callback((score != null))

  isReciprocated: (fromId, toId, callback) ->
    @isFollowing fromId, toId, (err, result) =>
      if result == true
        @isFollowing toId, fromId, (err, result) ->
          callback(result)
      else
        callback(false)

  following: (id, options, callback) ->
    if !callback?
      callback = options
      options = {}

    @members("#{@namespace}:#{@followingKey}:#{id}", options, callback)

  followers: (id, options, callback) ->
    if !callback?
      callback = options
      options = {}

    @members("#{@namespace}:#{@followingKey}:#{id}", options, callback)

  blocked: (id, options, callback) ->
    if !callback?
      callback = options
      options = {}

    @members("#{@namespace}:#{@blockedKey}:#{id}", options, callback)

  reciprocated: (id, options, callback) ->
    if !callback?
      callback = options
      options = {}

    @members("#{@namespace}:#{@reciprocatedKey}:#{id}", options, callback)

  following_page_count: (id, pageSize, callback) ->
    if !callback?
      callback = pageSize
      pageSize = @pageSize

    @totalPages("#{@namespace}:#{@followingKey}:#{id}", pageSize, callback)

  followers_page_count: (id, pageSize, callback) ->
    if !callback?
      callback = pageSize
      pageSize = @pageSize

    @totalPages("#{@namespace}:#{@followersKey}:#{id}", pageSize, callback)

  blocked_page_count: (id, pageSize, callback) ->
    if !callback?
      callback = pageSize
      pageSize = @pageSize

    @totalPages("#{@namespace}:#{@blockedKey}:#{id}", pageSize, callback)

  reciprocated_page_count: (id, pageSize, callback) ->
    if !callback?
      callback = pageSize
      pageSize = @pageSize

    @totalPages("#{@namespace}:#{@reciprocatedKey}:#{id}", pageSize, callback)

  defaultOptions: ->
    {
      pageSize: @pageSize
      page: 1
    }

  totalPages: (key, pageSize, callback) ->
    @redis.zcard key, (err, card) ->
      callback(Math.ceil(card / pageSize))

  members: (key, options, callback) ->
    if options?
      options = Hash.merge(Hash(@defaultOptions), Hash(options))
    else
      options = @defaultOptions

    if options.page < 1
      options.page = 1

    @totalPages (pages) ->
      if options.page > pages
        options.page = pages

      indexForRedis = options.page - 1
      startingOffset = (indexForRedis * options.pageSize)

      if startingOffset < 0
        startingOffset = 0

      endingOffset = (startingOffset + options.pageSize)

      @redis.zrevrange(key, startingOffset, endingOffset, false, callback)

module.exports = Amico
<commit_msg>Checks for isReciprocated truth before reciprocating<commit_after>Hash = require 'hashish'

getEpoch = ->
  date = new Date()
  return date.getTime()

Amico =
  redis: null
  namespace: 'amico'
  followingKey: 'following'
  followersKey: 'followers'
  blockedKey: 'blocked'
  reciprocatedKey: 'reciprocated'
  pageSize: 25

  configure: (config) ->
   config.apply(@)

  follow: (fromId, toId, callback) ->
    if fromId == toId
      if callback?
        callback(false)
      else
        return false

    self = @

    @isBlocked toId, fromId, (result) =>
      if result == true
        if callback?
          return callback(false)
        else
          return false
      else
        @redis.multi()
          .zadd("#{@namespace}:#{@followingKey}:#{fromId}", getEpoch(), toId)
          .zadd("#{@namespace}:#{@followersKey}:#{toId}", getEpoch(), fromId)
          .exec (err, replies) ->
            console.log "Replies: #{replies.inspect}"
            self.isReciprocated fromId, toId, (result) ->
              if result == true
                self.redis.multi()
                  .zadd("#{self.namespace}:#{self.reciprocatedKey}:#{fromId}", getEpoch(), toId)
                  .zadd("#{self.namespace}:#{self.reciprocatedKey}:#{toId}", getEpoch(), fromId)
                  .exec (err, replies) ->
                    console.log "Replies: #{replies.inspect}"
                    if callback?
                      callback(true)

  unfollow: (fromId, toId, callback) ->
    if fromId == toId
      if callback?
        callback(false)
      else
        return false

    @redis.multi()
      .zrem("#{@namespace}:#{@followingKey}:#{fromId}", toId)
      .zrem("#{@namespace}:#{@followersKey}:#{toId}", fromId)
      .zrem("#{@namespace}:#{@reciprocatedKey}:#{fromId}", toId)
      .zrem("#{@namespace}:#{@reciprocatedKey}:#{toId}", fromId)
      .exec (err, replies) ->
        if callback?
          callback(true)

  block: (fromId, toId, callback) ->
    if fromId == toId
      if callback?
        callback(false)
      else
        return false

    @redis.multi()
      .zrem("#{@namespace}:#{@followingKey}:#{fromId}", toId)
      .zrem("#{@namespace}:#{@followingKey}:#{toId}", fromId)
      .zrem("#{@namespace}:#{@followersKey}:#{fromId}", toId)
      .zrem("#{@namespace}:#{@followersKey}:#{toId}", fromId)
      .zrem("#{@namespace}:#{@reciprocatedKey}:#{fromId}", toId)
      .zrem("#{@namespace}:#{@reciprocatedKey}:#{toId}", fromId)
      .zadd("#{@namespace}:#{@blockedKey}:#{fromId}", getEpoch(), toId)
      .exec (err, replies) ->
        if err?
          if callback?
            callback(false)
        else if callback?
          callback(true)

  unblock: (fromId, toId, callback) ->
    if !callback?
      callback = ->
    if fromId == toId
      return false

    @redis.zrem("#{@namespace}:#{@blockedKey}:#{fromId}", toId, callback)

  following_count: (id, callback) ->
    @redis.zcard("#{@namespace}:#{@followingKey}:#{id}", callback)

  followers_count: (id, callback) ->
    @redis.zcard("#{@namespace}:#{@followersKey}:#{id}", callback)

  blocked_count: (id, callback) ->
    @redis.zcard("#{@namespace}:#{@blockedKey}:#{id}", callback)

  reciprocated_count: (id, callback) ->
    @redis.zcard("#{@namespace}:#{@reciprocatedKey}:#{id}", callback)



  isFollowing: (id, followingId, callback) ->
    @redis.zscore "#{@namespace}:#{@followingKey}:#{id}", followingId, (err, score) ->
      callback((score != null))

  isFollower: (id, followerId, callback) ->
    @redis.zscore "#{@namespace}:#{@followersKey}:#{id}", followerId, (err, score) ->
      callback((score != null))

  isBlocked: (id, blockedId, callback) ->
    @redis.zscore "#{@namespace}:#{@blockedKey}:#{id}", blockedId, (err, score) ->
      callback((score != null))

  isReciprocated: (fromId, toId, callback) ->
    @isFollowing fromId, toId, (err, result) =>
      if result == true
        @isFollowing toId, fromId, (err, result) ->
          callback(result)
      else
        callback(false)

  following: (id, options, callback) ->
    if !callback?
      callback = options
      options = {}

    @members("#{@namespace}:#{@followingKey}:#{id}", options, callback)

  followers: (id, options, callback) ->
    if !callback?
      callback = options
      options = {}

    @members("#{@namespace}:#{@followingKey}:#{id}", options, callback)

  blocked: (id, options, callback) ->
    if !callback?
      callback = options
      options = {}

    @members("#{@namespace}:#{@blockedKey}:#{id}", options, callback)

  reciprocated: (id, options, callback) ->
    if !callback?
      callback = options
      options = {}

    @members("#{@namespace}:#{@reciprocatedKey}:#{id}", options, callback)

  following_page_count: (id, pageSize, callback) ->
    if !callback?
      callback = pageSize
      pageSize = @pageSize

    @totalPages("#{@namespace}:#{@followingKey}:#{id}", pageSize, callback)

  followers_page_count: (id, pageSize, callback) ->
    if !callback?
      callback = pageSize
      pageSize = @pageSize

    @totalPages("#{@namespace}:#{@followersKey}:#{id}", pageSize, callback)

  blocked_page_count: (id, pageSize, callback) ->
    if !callback?
      callback = pageSize
      pageSize = @pageSize

    @totalPages("#{@namespace}:#{@blockedKey}:#{id}", pageSize, callback)

  reciprocated_page_count: (id, pageSize, callback) ->
    if !callback?
      callback = pageSize
      pageSize = @pageSize

    @totalPages("#{@namespace}:#{@reciprocatedKey}:#{id}", pageSize, callback)

  defaultOptions: ->
    {
      pageSize: @pageSize
      page: 1
    }

  totalPages: (key, pageSize, callback) ->
    @redis.zcard key, (err, card) ->
      callback(Math.ceil(card / pageSize))

  members: (key, options, callback) ->
    if options?
      options = Hash.merge(Hash(@defaultOptions), Hash(options))
    else
      options = @defaultOptions

    if options.page < 1
      options.page = 1

    @totalPages (pages) ->
      if options.page > pages
        options.page = pages

      indexForRedis = options.page - 1
      startingOffset = (indexForRedis * options.pageSize)

      if startingOffset < 0
        startingOffset = 0

      endingOffset = (startingOffset + options.pageSize)

      @redis.zrevrange(key, startingOffset, endingOffset, false, callback)

module.exports = Amico

{'fraction_non_alphanumeric': 0.17410106510771203, 'fraction_numerical': 0.001281332585889325, 'mean_word_length': 2.9493991144845033, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 83, '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': '4692363', 'n_tokens_mistral': 4089, 'n_tokens_neox': 4042, 'n_words': 814}