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.

46 lines
1.2 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.mentions.each do |mentioned_account|
  16. next unless mentioned_account.local?
  17. push(:mentions, mentioned_account.id, status)
  18. end
  19. end
  20. private
  21. def push(type, receiver_id, status)
  22. redis.zadd(key(type, receiver_id), status.created_at.to_i, status.id)
  23. trim(type, receiver_id)
  24. end
  25. def trim(type, receiver_id)
  26. return unless redis.zcard(key(type, receiver_id)) > MAX_FEED_SIZE
  27. last = redis.zrevrange(key(type, receiver_id), MAX_FEED_SIZE - 1, MAX_FEED_SIZE - 1)
  28. redis.zremrangebyscore(key(type, receiver_id), '-inf', "(#{last.last}")
  29. end
  30. def key(type, id)
  31. "feed:#{type}:#{id}"
  32. end
  33. def redis
  34. $redis
  35. end
  36. end