potential_friendship_tracker.rb 943 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. # frozen_string_literal: true
  2. class PotentialFriendshipTracker
  3. EXPIRE_AFTER = 90.days.seconds
  4. MAX_ITEMS = 80
  5. WEIGHTS = {
  6. reply: 1,
  7. favourite: 10,
  8. reblog: 20,
  9. }.freeze
  10. class << self
  11. def record(account_id, target_account_id, action)
  12. return if account_id == target_account_id
  13. key = "interactions:#{account_id}"
  14. weight = WEIGHTS[action]
  15. redis.zincrby(key, weight, target_account_id)
  16. redis.zremrangebyrank(key, 0, -MAX_ITEMS)
  17. redis.expire(key, EXPIRE_AFTER)
  18. end
  19. def remove(account_id, target_account_id)
  20. redis.zrem("interactions:#{account_id}", target_account_id)
  21. end
  22. def get(account_id, limit: 20, offset: 0)
  23. account_ids = redis.zrevrange("interactions:#{account_id}", offset, limit)
  24. return [] if account_ids.empty?
  25. Account.searchable.where(id: account_ids)
  26. end
  27. private
  28. def redis
  29. Redis.current
  30. end
  31. end
  32. end