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.

83 lines
2.6 KiB

7 years ago
  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. def filter?(timeline_type, status, receiver)
  9. if timeline_type == :home
  10. filter_from_home?(status, receiver)
  11. elsif timeline_type == :mentions
  12. filter_from_mentions?(status, receiver)
  13. else
  14. false
  15. end
  16. end
  17. def push(timeline_type, account, status)
  18. redis.zadd(key(timeline_type, account.id), status.id, status.reblog? ? status.reblog_of_id : status.id)
  19. trim(timeline_type, account.id)
  20. broadcast(account.id, type: 'update', timeline: timeline_type, message: inline_render(account, status))
  21. end
  22. def broadcast(timeline_id, options = {})
  23. ActionCable.server.broadcast("timeline:#{timeline_id}", options)
  24. end
  25. def trim(type, account_id)
  26. return unless redis.zcard(key(type, account_id)) > FeedManager::MAX_ITEMS
  27. last = redis.zrevrange(key(type, account_id), FeedManager::MAX_ITEMS - 1, FeedManager::MAX_ITEMS - 1)
  28. redis.zremrangebyscore(key(type, account_id), '-inf', "(#{last.last}")
  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.try(: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. private
  46. def redis
  47. $redis
  48. end
  49. def filter_from_home?(status, receiver)
  50. should_filter = false
  51. if status.reply? && !status.thread.account.nil? # Filter out if it's a reply
  52. should_filter = !receiver.following?(status.thread.account) # and I'm not following the person it's a reply to
  53. should_filter = should_filter && !(receiver.id == status.thread.account_id) # and it's not a reply to me
  54. should_filter = should_filter && !(status.account_id == status.thread.account_id) # and it's not a self-reply
  55. elsif status.reblog? # Filter out a reblog
  56. should_filter = receiver.blocking?(status.reblog.account) # if I'm blocking the reblogged person
  57. end
  58. should_filter
  59. end
  60. def filter_from_mentions?(status, receiver)
  61. should_filter = receiver.id == status.account_id # Filter if I'm mentioning myself
  62. should_filter = should_filter || receiver.blocking?(status.account) # or it's from someone I blocked
  63. should_filter
  64. end
  65. end