You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.4 KiB

  1. class FanOutOnWriteService < BaseService
  2. MAX_FEED_SIZE = 800
  3. # Push a status into home and mentions feeds
  4. # @param [Status] status
  5. def call(status)
  6. deliver_to_self(status) if status.account.local?
  7. deliver_to_followers(status, status.reply? ? status.thread.account : nil)
  8. deliver_to_mentioned(status)
  9. end
  10. private
  11. def deliver_to_self(status)
  12. push(:home, status.account.id, status)
  13. end
  14. def deliver_to_followers(status, replied_to_user)
  15. status.account.followers.each do |follower|
  16. next if (status.reply? && !(follower.id = replied_to_user.id || follower.following?(replied_to_user))) || !follower.local?
  17. push(:home, follower.id, status)
  18. end
  19. end
  20. def deliver_to_mentioned(status)
  21. status.mentioned_accounts.each do |mention|
  22. mentioned_account = mention.account
  23. next unless mentioned_account.local?
  24. push(:mentions, mentioned_account.id, status)
  25. end
  26. end
  27. def push(type, receiver_id, status)
  28. redis.zadd(key(type, receiver_id), status.id, status.id)
  29. trim(type, receiver_id)
  30. end
  31. def trim(type, receiver_id)
  32. return unless redis.zcard(key(type, receiver_id)) > MAX_FEED_SIZE
  33. last = redis.zrevrange(key(type, receiver_id), MAX_FEED_SIZE - 1, MAX_FEED_SIZE - 1)
  34. redis.zremrangebyscore(key(type, receiver_id), '-inf', "(#{last.last}")
  35. end
  36. def key(type, id)
  37. "feed:#{type}:#{id}"
  38. end
  39. def redis
  40. $redis
  41. end
  42. end