feed.rb 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. # frozen_string_literal: true
  2. class Feed
  3. def initialize(type, account)
  4. @type = type
  5. @account = account
  6. end
  7. def get(limit, max_id = nil, since_id = nil)
  8. max_id = '+inf' if max_id.blank?
  9. since_id = '-inf' if since_id.blank?
  10. unhydrated = redis.zrevrangebyscore(key, "(#{max_id}", "(#{since_id}", limit: [0, limit], with_scores: true).map(&:last).map(&:to_i)
  11. # If we're after most recent items and none are there, we need to precompute the feed
  12. if unhydrated.empty? && max_id == '+inf' && since_id == '-inf'
  13. RegenerationWorker.perform_async(@account.id, @type)
  14. @statuses = Status.send("as_#{@type}_timeline", @account).cache_ids.paginate_by_max_id(limit, nil, nil)
  15. else
  16. status_map = Status.where(id: unhydrated).cache_ids.map { |s| [s.id, s] }.to_h
  17. @statuses = unhydrated.map { |id| status_map[id] }.compact
  18. end
  19. @statuses
  20. end
  21. private
  22. def key
  23. FeedManager.instance.key(@type, @account.id)
  24. end
  25. def redis
  26. Redis.current
  27. end
  28. end