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.

39 lines
1004 B

  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.where(id: unhydrated).cache_ids
  20. end
  21. def from_database(limit, max_id, since_id)
  22. Status.as_home_timeline(@account)
  23. .paginate_by_max_id(limit, max_id, since_id)
  24. .reject { |status| FeedManager.instance.filter?(:home, status, @account.id) }
  25. end
  26. def key
  27. FeedManager.instance.key(@type, @account.id)
  28. end
  29. def redis
  30. Redis.current
  31. end
  32. end