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.

65 lines
1.8 KiB

7 years ago
  1. class FanOutOnWriteService < BaseService
  2. # Push a status into home and mentions feeds
  3. # @param [Status] status
  4. def call(status)
  5. deliver_to_self(status) if status.account.local?
  6. deliver_to_followers(status)
  7. deliver_to_mentioned(status)
  8. end
  9. private
  10. def deliver_to_self(status)
  11. push(:home, status.account, status)
  12. end
  13. def deliver_to_followers(status)
  14. status.account.followers.each do |follower|
  15. next if !follower.local? || FeedManager.filter_status?(status, follower)
  16. push(:home, follower, status)
  17. end
  18. end
  19. def deliver_to_mentioned(status)
  20. status.mentions.each do |mention|
  21. mentioned_account = mention.account
  22. next unless mentioned_account.local?
  23. push(:mentions, mentioned_account, status)
  24. end
  25. end
  26. def push(type, receiver, status)
  27. redis.zadd(FeedManager.key(type, receiver.id), status.id, status.id)
  28. trim(type, receiver)
  29. ActionCable.server.broadcast("timeline:#{receiver.id}", message: inline_render(receiver, status))
  30. end
  31. def trim(type, receiver)
  32. return unless redis.zcard(FeedManager.key(type, receiver.id)) > FeedManager::MAX_ITEMS
  33. last = redis.zrevrange(FeedManager.key(type, receiver.id), FeedManager::MAX_ITEMS - 1, FeedManager::MAX_ITEMS - 1)
  34. redis.zremrangebyscore(FeedManager.key(type, receiver.id), '-inf', "(#{last.last}")
  35. end
  36. def redis
  37. $redis
  38. end
  39. def inline_render(receiver, status)
  40. rabl_scope = Class.new(BaseService) do
  41. def initialize(account)
  42. @account = account
  43. end
  44. def current_user
  45. @account.user
  46. end
  47. def current_account
  48. @account
  49. end
  50. end
  51. Rabl::Renderer.new('api/statuses/show', status, view_path: 'app/views', format: :json, scope: rabl_scope.new(receiver)).render
  52. end
  53. end