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.

59 lines
1.6 KiB

  1. require 'singleton'
  2. class FeedManager
  3. include Singleton
  4. MAX_ITEMS = 800
  5. def key(type, id)
  6. "feed:#{type}:#{id}"
  7. end
  8. # Filter status out of the home feed if it is a reply to someone the user doesn't follow
  9. def filter_status?(status, follower)
  10. replied_to_user = status.reply? ? status.thread.account : nil
  11. (status.reply? && !(follower.id == replied_to_user.id || replied_to_user.id == status.account_id || follower.following?(replied_to_user)))
  12. end
  13. def push(timeline_type, account, status)
  14. redis.zadd(key(timeline_type, account.id), status.id, status.reblog? ? status.reblog_of_id : status.id)
  15. trim(timeline_type, account.id)
  16. broadcast(account.id, type: 'update', timeline: timeline_type, message: inline_render(account, status))
  17. end
  18. def broadcast(account_id, options = {})
  19. ActionCable.server.broadcast("timeline:#{account_id}", options)
  20. end
  21. def trim(type, account_id)
  22. return unless redis.zcard(key(type, account_id)) > FeedManager::MAX_ITEMS
  23. last = redis.zrevrange(key(type, account_id), FeedManager::MAX_ITEMS - 1, FeedManager::MAX_ITEMS - 1)
  24. redis.zremrangebyscore(key(type, account_id), '-inf', "(#{last.last}")
  25. end
  26. private
  27. def redis
  28. $redis
  29. end
  30. def inline_render(target_account, status)
  31. rabl_scope = Class.new do
  32. include RoutingHelper
  33. def initialize(account)
  34. @account = account
  35. end
  36. def current_user
  37. @account.user
  38. end
  39. def current_account
  40. @account
  41. end
  42. end
  43. Rabl::Renderer.new('api/v1/statuses/show', status, view_path: 'app/views', format: :json, scope: rabl_scope.new(target_account)).render
  44. end
  45. end