potential_friendship_tracker.rb 910 B

12345678910111213141516171819202122232425262728293031323334353637
  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. include Redisable
  12. def record(account_id, target_account_id, action)
  13. return if account_id == target_account_id
  14. key = "interactions:#{account_id}"
  15. weight = WEIGHTS[action]
  16. redis.zincrby(key, weight, target_account_id)
  17. redis.zremrangebyrank(key, 0, -MAX_ITEMS)
  18. redis.expire(key, EXPIRE_AFTER)
  19. end
  20. def remove(account_id, target_account_id)
  21. redis.zrem("interactions:#{account_id}", target_account_id)
  22. end
  23. def get(account_id, limit: 20, offset: 0)
  24. account_ids = redis.zrevrange("interactions:#{account_id}", offset, limit)
  25. return [] if account_ids.empty?
  26. Account.searchable.where(id: account_ids)
  27. end
  28. end
  29. end