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.

40 lines
1.1 KiB

  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. if redis.exists("account:#{@account.id}:regeneration")
  9. from_database(limit, max_id, since_id)
  10. else
  11. from_redis(limit, max_id, since_id)
  12. end
  13. end
  14. private
  15. def from_redis(limit, max_id, since_id)
  16. max_id = '+inf' if max_id.blank?
  17. since_id = '-inf' if since_id.blank?
  18. unhydrated = redis.zrevrangebyscore(key, "(#{max_id}", "(#{since_id}", limit: [0, limit], with_scores: true).map(&:last).map(&:to_i)
  19. status_map = Status.where(id: unhydrated).cache_ids.map { |s| [s.id, s] }.to_h
  20. unhydrated.map { |id| status_map[id] }.compact
  21. end
  22. def from_database(limit, max_id, since_id)
  23. Status.as_home_timeline(@account)
  24. .paginate_by_max_id(limit, max_id, since_id)
  25. .reject { |status| FeedManager.instance.filter?(:home, status, @account.id) }
  26. end
  27. def key
  28. FeedManager.instance.key(@type, @account.id)
  29. end
  30. def redis
  31. Redis.current
  32. end
  33. end