precompute_feed_service.rb 979 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # frozen_string_literal: true
  2. class PrecomputeFeedService < BaseService
  3. LIMIT = FeedManager::MAX_ITEMS / 4
  4. def call(account)
  5. @account = account
  6. populate_feed
  7. end
  8. private
  9. attr_reader :account
  10. def populate_feed
  11. redis.pipelined do
  12. statuses.each do |status|
  13. process_status(status)
  14. end
  15. end
  16. end
  17. def process_status(status)
  18. add_status_to_feed(status) unless skip_status?(status)
  19. end
  20. def skip_status?(status)
  21. status.direct_visibility? || status_filtered?(status)
  22. end
  23. def add_status_to_feed(status)
  24. redis.zadd(account_home_key, status.id, status.reblog? ? status.reblog_of_id : status.id)
  25. end
  26. def status_filtered?(status)
  27. FeedManager.instance.filter?(:home, status, account.id)
  28. end
  29. def account_home_key
  30. FeedManager.instance.key(:home, account.id)
  31. end
  32. def statuses
  33. Status.as_home_timeline(account).order(account_id: :desc).limit(LIMIT)
  34. end
  35. def redis
  36. Redis.current
  37. end
  38. end