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.

47 lines
1.3 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. replied_to_user = status.reply? ? status.thread.account : nil
  7. # Deliver to local self
  8. push(:home, status.account.id, status) if status.account.local?
  9. # Deliver to local followers
  10. status.account.followers.each do |follower|
  11. next if (status.reply? && !follower.following?(replied_to_user)) || !follower.local?
  12. push(:home, follower.id, status)
  13. end
  14. # Deliver to local mentioned
  15. status.mentioned_accounts.each do |mention|
  16. mentioned_account = mention.account
  17. next unless mentioned_account.local?
  18. push(:mentions, mentioned_account.id, status)
  19. end
  20. end
  21. private
  22. def push(type, receiver_id, status)
  23. redis.zadd(key(type, receiver_id), status.created_at.to_i, status.id)
  24. trim(type, receiver_id)
  25. end
  26. def trim(type, receiver_id)
  27. return unless redis.zcard(key(type, receiver_id)) > MAX_FEED_SIZE
  28. last = redis.zrevrange(key(type, receiver_id), MAX_FEED_SIZE - 1, MAX_FEED_SIZE - 1)
  29. redis.zremrangebyscore(key(type, receiver_id), '-inf', "(#{last.last}")
  30. end
  31. def key(type, id)
  32. "feed:#{type}:#{id}"
  33. end
  34. def redis
  35. $redis
  36. end
  37. end