Module: Flocks::Relationships
- Included in:
- Flocks
- Defined in:
- lib/flocks/relationships.rb
Instance Method Summary collapse
- #block(other_user_id) ⇒ Object
- #blocked ⇒ Object
- #blocked?(other_user_id) ⇒ Boolean
- #follow(other_user_id, other_username) ⇒ Object
- #followers(page = 0, limit = 0) ⇒ Object
- #followers_count ⇒ Object
- #followers_page_count(limit = page_size) ⇒ Object
- #following(page = 0, limit = 0) ⇒ Object
- #following?(other_user_id) ⇒ Boolean
- #following_count ⇒ Object
- #following_page_count(limit = page_size) ⇒ Object
- #relationship_blocked_key ⇒ Object
- #relationship_followers_key(id = user_id) ⇒ Object
- #relationship_following_key ⇒ Object
- #unblock(other_user_id) ⇒ Object
- #unfollow(other_user_id) ⇒ Object
Instance Method Details
#block(other_user_id) ⇒ Object
60 61 62 63 64 65 66 67 |
# 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 ⇒ Object
73 74 75 |
# File 'lib/flocks/relationships.rb', line 73 def blocked redis.smembers relationship_blocked_key end |
#blocked?(other_user_id) ⇒ Boolean
77 78 79 |
# 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) ⇒ Object
16 17 18 19 20 21 22 |
# 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) ⇒ Object
35 36 37 38 |
# 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 ⇒ Object
48 49 50 |
# File 'lib/flocks/relationships.rb', line 48 def followers_count redis.zcard relationship_followers_key end |
#followers_page_count(limit = page_size) ⇒ Object
56 57 58 |
# 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) ⇒ Object
29 30 31 32 33 |
# 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) ⇒ Boolean
40 41 42 |
# File 'lib/flocks/relationships.rb', line 40 def following?(other_user_id) following.include? other_user_id.to_s end |
#following_count ⇒ Object
44 45 46 |
# File 'lib/flocks/relationships.rb', line 44 def following_count redis.zcard relationship_following_key end |
#following_page_count(limit = page_size) ⇒ Object
52 53 54 |
# 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 ⇒ Object
12 13 14 |
# File 'lib/flocks/relationships.rb', line 12 def relationship_blocked_key "#{blocked_key}/#{user_id}" end |
#relationship_followers_key(id = user_id) ⇒ Object
8 9 10 |
# File 'lib/flocks/relationships.rb', line 8 def relationship_followers_key(id = user_id) "#{followers_key}/#{id}" end |
#relationship_following_key ⇒ Object
4 5 6 |
# File 'lib/flocks/relationships.rb', line 4 def relationship_following_key "#{following_key}/#{user_id}" end |
#unblock(other_user_id) ⇒ Object
69 70 71 |
# 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) ⇒ Object
24 25 26 27 |
# 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 |