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.

35 lines
859 B

  1. class PrecomputeFeedService < BaseService
  2. MAX_FEED_SIZE = 800
  3. # Fill up a user's home/mentions feed from DB and return it
  4. # @param [Symbol] type :home or :mentions
  5. # @param [Account] account
  6. # @return [Array]
  7. def call(type, account)
  8. statuses = send(type.to_s, account).order('created_at desc').limit(MAX_FEED_SIZE)
  9. statuses.each { |status| push(type, account.id, status) }
  10. statuses
  11. end
  12. private
  13. def push(type, receiver_id, status)
  14. redis.zadd(key(type, receiver_id), status.id, status.id)
  15. end
  16. def home(account)
  17. Status.where(account: [account] + account.following).with_includes.with_counters
  18. end
  19. def mentions(account)
  20. Status.where(id: Mention.where(account: account).pluck(:status_id)).with_includes.with_counters
  21. end
  22. def key(type, id)
  23. "feed:#{type}:#{id}"
  24. end
  25. def redis
  26. $redis
  27. end
  28. end