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.

104 lines
3.1 KiB

7 years ago
  1. # frozen_string_literal: true
  2. require 'singleton'
  3. class FeedManager
  4. include Singleton
  5. MAX_ITEMS = 800
  6. def key(type, id)
  7. "feed:#{type}:#{id}"
  8. end
  9. def filter?(timeline_type, status, receiver)
  10. if timeline_type == :home
  11. filter_from_home?(status, receiver)
  12. elsif timeline_type == :mentions
  13. filter_from_mentions?(status, receiver)
  14. elsif timeline_type == :public
  15. filter_from_public?(status, receiver)
  16. else
  17. false
  18. end
  19. end
  20. def push(timeline_type, account, status)
  21. redis.zadd(key(timeline_type, account.id), status.id, status.reblog? ? status.reblog_of_id : status.id)
  22. trim(timeline_type, account.id)
  23. broadcast(account.id, type: 'update', timeline: timeline_type, message: inline_render(account, status))
  24. end
  25. def broadcast(timeline_id, options = {})
  26. ActionCable.server.broadcast("timeline:#{timeline_id}", options)
  27. end
  28. def trim(type, account_id)
  29. return unless redis.zcard(key(type, account_id)) > FeedManager::MAX_ITEMS
  30. last = redis.zrevrange(key(type, account_id), FeedManager::MAX_ITEMS - 1, FeedManager::MAX_ITEMS - 1)
  31. redis.zremrangebyscore(key(type, account_id), '-inf', "(#{last.last}")
  32. end
  33. def inline_render(target_account, status)
  34. rabl_scope = Class.new do
  35. include RoutingHelper
  36. def initialize(account)
  37. @account = account
  38. end
  39. def current_user
  40. @account.try(:user)
  41. end
  42. def current_account
  43. @account
  44. end
  45. end
  46. Rabl::Renderer.new('api/v1/statuses/show', status, view_path: 'app/views', format: :json, scope: rabl_scope.new(target_account)).render
  47. end
  48. private
  49. def redis
  50. Redis.current
  51. end
  52. def filter_from_home?(status, receiver)
  53. should_filter = false
  54. if status.reply? && !status.thread.account.nil? # Filter out if it's a reply
  55. should_filter = !receiver.following?(status.thread.account) # and I'm not following the person it's a reply to
  56. should_filter &&= !(receiver.id == status.thread.account_id) # and it's not a reply to me
  57. should_filter &&= !(status.account_id == status.thread.account_id) # and it's not a self-reply
  58. elsif status.reblog? # Filter out a reblog
  59. should_filter = receiver.blocking?(status.reblog.account) # if I'm blocking the reblogged person
  60. end
  61. should_filter
  62. end
  63. def filter_from_mentions?(status, receiver)
  64. should_filter = receiver.id == status.account_id # Filter if I'm mentioning myself
  65. should_filter ||= receiver.blocking?(status.account) # or it's from someone I blocked
  66. if status.reply? && !status.thread.account.nil? # or it's a reply
  67. should_filter ||= receiver.blocking?(status.thread.account) # to a user I blocked
  68. end
  69. should_filter
  70. end
  71. def filter_from_public?(status, receiver)
  72. should_filter = receiver.blocking?(status.account)
  73. if status.reply? && !status.thread.account.nil?
  74. should_filter ||= receiver.blocking?(status.thread.account)
  75. elsif status.reblog?
  76. should_filter ||= receiver.blocking?(status.reblog.account)
  77. end
  78. should_filter
  79. end
  80. end