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.

141 lines
5.8 KiB

Account domain blocks (#2381) * Add <ostatus:conversation /> tag to Atom input/output Only uses ref attribute (not href) because href would be the alternate link that's always included also. Creates new conversation for every non-reply status. Carries over conversation for every reply. Keeps remote URIs verbatim, generates local URIs on the fly like the rest of them. * Conversation muting - prevents notifications that reference a conversation (including replies, favourites, reblogs) from being created. API endpoints /api/v1/statuses/:id/mute and /api/v1/statuses/:id/unmute Currently no way to tell when a status/conversation is muted, so the web UI only has a "disable notifications" button, doesn't work as a toggle * Display "Dismiss notifications" on all statuses in notifications column, not just own * Add "muted" as a boolean attribute on statuses JSON For now always false on contained reblogs, since it's only relevant for statuses returned from the notifications endpoint, which are not nested Remove "Disable notifications" from detailed status view, since it's only relevant in the notifications column * Up max class length * Remove pending test for conversation mute * Add tests, clean up * Rename to "mute conversation" and "unmute conversation" * Raise validation error when trying to mute/unmute status without conversation * Adding account domain blocks that filter notifications and public timelines * Add tests for domain blocks in notifications, public timelines Filter reblogs of blocked domains from home * Add API for listing and creating account domain blocks * API for creating/deleting domain blocks, tests for Status#ancestors and Status#descendants, filter domain blocks from them * Filter domains in streaming API * Update account_domain_block_spec.rb
7 years ago
Account domain blocks (#2381) * Add <ostatus:conversation /> tag to Atom input/output Only uses ref attribute (not href) because href would be the alternate link that's always included also. Creates new conversation for every non-reply status. Carries over conversation for every reply. Keeps remote URIs verbatim, generates local URIs on the fly like the rest of them. * Conversation muting - prevents notifications that reference a conversation (including replies, favourites, reblogs) from being created. API endpoints /api/v1/statuses/:id/mute and /api/v1/statuses/:id/unmute Currently no way to tell when a status/conversation is muted, so the web UI only has a "disable notifications" button, doesn't work as a toggle * Display "Dismiss notifications" on all statuses in notifications column, not just own * Add "muted" as a boolean attribute on statuses JSON For now always false on contained reblogs, since it's only relevant for statuses returned from the notifications endpoint, which are not nested Remove "Disable notifications" from detailed status view, since it's only relevant in the notifications column * Up max class length * Remove pending test for conversation mute * Add tests, clean up * Rename to "mute conversation" and "unmute conversation" * Raise validation error when trying to mute/unmute status without conversation * Adding account domain blocks that filter notifications and public timelines * Add tests for domain blocks in notifications, public timelines Filter reblogs of blocked domains from home * Add API for listing and creating account domain blocks * API for creating/deleting domain blocks, tests for Status#ancestors and Status#descendants, filter domain blocks from them * Filter domains in streaming API * Update account_domain_block_spec.rb
7 years ago
  1. # frozen_string_literal: true
  2. require 'singleton'
  3. class FeedManager
  4. include Singleton
  5. MAX_ITEMS = 400
  6. def key(type, id)
  7. "feed:#{type}:#{id}"
  8. end
  9. def filter?(timeline_type, status, receiver_id)
  10. if timeline_type == :home
  11. filter_from_home?(status, receiver_id)
  12. elsif timeline_type == :mentions
  13. filter_from_mentions?(status, receiver_id)
  14. else
  15. false
  16. end
  17. end
  18. def push(timeline_type, account, status)
  19. timeline_key = key(timeline_type, account.id)
  20. if status.reblog?
  21. # If the original status is within 40 statuses from top, do not re-insert it into the feed
  22. rank = redis.zrevrank(timeline_key, status.reblog_of_id)
  23. return if !rank.nil? && rank < 40
  24. redis.zadd(timeline_key, status.id, status.reblog_of_id)
  25. else
  26. redis.zadd(timeline_key, status.id, status.id)
  27. trim(timeline_type, account.id)
  28. end
  29. PushUpdateWorker.perform_async(account.id, status.id) if push_update_required?(timeline_type, account.id)
  30. end
  31. def trim(type, account_id)
  32. return unless redis.zcard(key(type, account_id)) > FeedManager::MAX_ITEMS
  33. last = redis.zrevrange(key(type, account_id), FeedManager::MAX_ITEMS - 1, FeedManager::MAX_ITEMS - 1)
  34. redis.zremrangebyscore(key(type, account_id), '-inf', "(#{last.last}")
  35. end
  36. def push_update_required?(timeline_type, account_id)
  37. timeline_type != :home || redis.get("subscribed:timeline:#{account_id}").present?
  38. end
  39. def merge_into_timeline(from_account, into_account)
  40. timeline_key = key(:home, into_account.id)
  41. query = from_account.statuses.limit(FeedManager::MAX_ITEMS / 4)
  42. if redis.zcard(timeline_key) >= FeedManager::MAX_ITEMS / 4
  43. oldest_home_score = redis.zrange(timeline_key, 0, 0, with_scores: true)&.first&.last&.to_i || 0
  44. query = query.where('id > ?', oldest_home_score)
  45. end
  46. redis.pipelined do
  47. query.each do |status|
  48. next if status.direct_visibility? || filter?(:home, status, into_account)
  49. redis.zadd(timeline_key, status.id, status.id)
  50. end
  51. end
  52. trim(:home, into_account.id)
  53. end
  54. def unmerge_from_timeline(from_account, into_account)
  55. timeline_key = key(:home, into_account.id)
  56. oldest_home_score = redis.zrange(timeline_key, 0, 0, with_scores: true)&.first&.last&.to_i || 0
  57. from_account.statuses.select('id').where('id > ?', oldest_home_score).reorder(nil).find_in_batches do |statuses|
  58. redis.pipelined do
  59. statuses.each do |status|
  60. redis.zrem(timeline_key, status.id)
  61. redis.zremrangebyscore(timeline_key, status.id, status.id)
  62. end
  63. end
  64. end
  65. end
  66. def clear_from_timeline(account, target_account)
  67. timeline_key = key(:home, account.id)
  68. timeline_status_ids = redis.zrange(timeline_key, 0, -1)
  69. target_status_ids = Status.where(id: timeline_status_ids, account: target_account).ids
  70. redis.zrem(timeline_key, target_status_ids) if target_status_ids.present?
  71. end
  72. private
  73. def redis
  74. Redis.current
  75. end
  76. def filter_from_home?(status, receiver_id)
  77. # extremely violent filtering code BEGIN
  78. #filter_string = 'e'
  79. #reggie = Regexp.new(filter_string)
  80. #return true if reggie === status.content || reggie === status.spoiler_text
  81. # extremely violent filtering code END
  82. return true if status.reply? && status.in_reply_to_id.nil?
  83. check_for_mutes = [status.account_id]
  84. check_for_mutes.concat([status.reblog.account_id]) if status.reblog?
  85. return true if Mute.where(account_id: receiver_id, target_account_id: check_for_mutes).any?
  86. check_for_blocks = status.mentions.pluck(:account_id)
  87. check_for_blocks.concat([status.reblog.account_id]) if status.reblog?
  88. return true if Block.where(account_id: receiver_id, target_account_id: check_for_blocks).any?
  89. if status.reply? && !status.in_reply_to_account_id.nil? # Filter out if it's a reply
  90. should_filter = !Follow.where(account_id: receiver_id, target_account_id: status.in_reply_to_account_id).exists? # and I'm not following the person it's a reply to
  91. should_filter &&= receiver_id != status.in_reply_to_account_id # and it's not a reply to me
  92. should_filter &&= status.account_id != status.in_reply_to_account_id # and it's not a self-reply
  93. return should_filter
  94. elsif status.reblog? # Filter out a reblog
  95. should_filter = Block.where(account_id: status.reblog.account_id, target_account_id: receiver_id).exists? # or if the author of the reblogged status is blocking me
  96. should_filter ||= AccountDomainBlock.where(account_id: receiver_id, domain: status.reblog.account.domain).exists? # or the author's domain is blocked
  97. return should_filter
  98. end
  99. false
  100. end
  101. def filter_from_mentions?(status, receiver_id)
  102. check_for_blocks = [status.account_id]
  103. check_for_blocks.concat(status.mentions.pluck(:account_id))
  104. check_for_blocks.concat([status.in_reply_to_account]) if status.reply? && !status.in_reply_to_account_id.nil?
  105. should_filter = receiver_id == status.account_id # Filter if I'm mentioning myself
  106. should_filter ||= Block.where(account_id: receiver_id, target_account_id: check_for_blocks).any? # or it's from someone I blocked, in reply to someone I blocked, or mentioning someone I blocked
  107. should_filter ||= (status.account.silenced? && !Follow.where(account_id: receiver_id, target_account_id: status.account_id).exists?) # of if the account is silenced and I'm not following them
  108. should_filter
  109. end
  110. end