闭社主体 forked from https://github.com/tootsuite/mastodon
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.

33 lines
1010 B

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