feed.rb 978 B

12345678910111213141516171819202122232425262728293031323334353637
  1. # frozen_string_literal: true
  2. class Feed
  3. include Redisable
  4. def initialize(type, id)
  5. @type = type
  6. @id = id
  7. end
  8. def get(limit, max_id = nil, since_id = nil, min_id = nil)
  9. limit = limit.to_i
  10. max_id = max_id.to_i if max_id.present?
  11. since_id = since_id.to_i if since_id.present?
  12. min_id = min_id.to_i if min_id.present?
  13. from_redis(limit, max_id, since_id, min_id)
  14. end
  15. protected
  16. def from_redis(limit, max_id, since_id, min_id)
  17. max_id = '+inf' if max_id.blank?
  18. if min_id.blank?
  19. since_id = '-inf' if since_id.blank?
  20. unhydrated = redis.zrevrangebyscore(key, "(#{max_id}", "(#{since_id}", limit: [0, limit], with_scores: true).map { |id| id.first.to_i }
  21. else
  22. unhydrated = redis.zrangebyscore(key, "(#{min_id}", "(#{max_id}", limit: [0, limit], with_scores: true).map { |id| id.first.to_i }
  23. end
  24. Status.where(id: unhydrated)
  25. end
  26. def key
  27. FeedManager.instance.key(@type, @id)
  28. end
  29. end