module Flocks::Relationships

Public Instance Methods

block(other_user_id) click to toggle source
# File lib/flocks/relationships.rb, line 60
def block(other_user_id)
  # Unfollow
  unfollow other_user_id
  self.new(other_user_id).unfollow user_id

  # Block
  redis.sadd relationship_blocked_key, other_user_id
end
blocked() click to toggle source
# File lib/flocks/relationships.rb, line 73
def blocked
  redis.smembers relationship_blocked_key
end
blocked?(other_user_id) click to toggle source
# File lib/flocks/relationships.rb, line 77
def blocked?(other_user_id)
  blocked.include? other_user_id.to_s
end
follow(other_user_id, other_username) click to toggle source
# File lib/flocks/relationships.rb, line 16
def follow(other_user_id, other_username)
  return if (user_id == other_user_id) || (blocked? other_user_id)

  other_username_score = rank_username(other_username)
  redis.zadd relationship_following_key, other_username_score, other_user_id
  redis.zadd relationship_followers_key(other_user_id), username_score, user_id
end
followers(page = 0, limit= 0) click to toggle source
# File lib/flocks/relationships.rb, line 35
def followers(page = 0, limit= 0)
  start, stop = bounds(page, limit)
  redis.zrange relationship_followers_key(user_id), start, stop
end
followers_count() click to toggle source
# File lib/flocks/relationships.rb, line 48
def followers_count
  redis.zcard relationship_followers_key
end
followers_page_count(limit = page_size) click to toggle source
# File lib/flocks/relationships.rb, line 56
def followers_page_count(limit = page_size)
  (followers_count.to_f / limit.to_f).ceil
end
following(page = 0, limit = 0) click to toggle source
# File lib/flocks/relationships.rb, line 29
def following(page = 0, limit = 0)
  # Without arguments returns the full set
  start, stop = bounds(page, limit)
  redis.zrange relationship_following_key, start, stop
end
following?(other_user_id) click to toggle source
# File lib/flocks/relationships.rb, line 40
def following?(other_user_id)
  following.include? other_user_id.to_s
end
following_count() click to toggle source
# File lib/flocks/relationships.rb, line 44
def following_count
  redis.zcard relationship_following_key
end
following_page_count(limit = page_size) click to toggle source
# File lib/flocks/relationships.rb, line 52
def following_page_count(limit = page_size)
  (following_count.to_f / limit.to_f).ceil
end
relationship_blocked_key() click to toggle source
# File lib/flocks/relationships.rb, line 12
def relationship_blocked_key
  "#{blocked_key}/#{user_id}"
end
relationship_followers_key(id = user_id) click to toggle source
# File lib/flocks/relationships.rb, line 8
def relationship_followers_key(id = user_id)
  "#{followers_key}/#{id}"
end
relationship_following_key() click to toggle source
# File lib/flocks/relationships.rb, line 4
def relationship_following_key
  "#{following_key}/#{user_id}"
end
unblock(other_user_id) click to toggle source
# File lib/flocks/relationships.rb, line 69
def unblock(other_user_id)
  redis.srem relationship_blocked_key, other_user_id
end
unfollow(other_user_id) click to toggle source
# File lib/flocks/relationships.rb, line 24
def unfollow(other_user_id)
  redis.zrem relationship_following_key, other_user_id
  redis.zrem relationship_followers_key(other_user_id), user_id
end

Protected Instance Methods

bounds(page, limit) click to toggle source
# File lib/flocks/relationships.rb, line 83
def bounds(page, limit)
  [(page - 1) * limit, (page * limit) - 1]
end