The code powering m.abunchtell.com https://m.abunchtell.com
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

129 строки
4.2 KiB

  1. # frozen_string_literal: true
  2. class BatchedRemoveStatusService < BaseService
  3. include StreamEntryRenderer
  4. # Delete given statuses and reblogs of them
  5. # Dispatch PuSH updates of the deleted statuses, but only local ones
  6. # Dispatch Salmon deletes, unique per domain, of the deleted statuses, but only local ones
  7. # Remove statuses from home feeds
  8. # Push delete events to streaming API for home feeds and public feeds
  9. # @param [Status] statuses A preferably batched array of statuses
  10. def call(statuses)
  11. statuses = Status.where(id: statuses.map(&:id)).includes(:account, :stream_entry).flat_map { |status| [status] + status.reblogs.includes(:account, :stream_entry).to_a }
  12. @mentions = statuses.map { |s| [s.id, s.mentions.includes(:account).to_a] }.to_h
  13. @tags = statuses.map { |s| [s.id, s.tags.pluck(:name)] }.to_h
  14. @stream_entry_batches = []
  15. @salmon_batches = []
  16. @json_payloads = statuses.map { |s| [s.id, Oj.dump(event: :delete, payload: s.id.to_s)] }.to_h
  17. @activity_xml = {}
  18. # Ensure that rendered XML reflects destroyed state
  19. statuses.each do |status|
  20. status.mark_for_mass_destruction!
  21. status.destroy
  22. end
  23. # Batch by source account
  24. statuses.group_by(&:account_id).each_value do |account_statuses|
  25. account = account_statuses.first.account
  26. unpush_from_home_timelines(account, account_statuses)
  27. unpush_from_list_timelines(account, account_statuses)
  28. batch_stream_entries(account, account_statuses) if account.local?
  29. end
  30. # Cannot be batched
  31. statuses.each do |status|
  32. unpush_from_public_timelines(status)
  33. unpush_from_direct_timelines(status) if status.direct_visibility?
  34. batch_salmon_slaps(status) if status.local?
  35. end
  36. Pubsubhubbub::RawDistributionWorker.push_bulk(@stream_entry_batches) { |batch| batch }
  37. NotificationWorker.push_bulk(@salmon_batches) { |batch| batch }
  38. end
  39. private
  40. def batch_stream_entries(account, statuses)
  41. statuses.each do |status|
  42. @stream_entry_batches << [build_xml(status.stream_entry), account.id]
  43. end
  44. end
  45. def unpush_from_home_timelines(account, statuses)
  46. recipients = account.followers_for_local_distribution.to_a
  47. recipients << account if account.local?
  48. recipients.each do |follower|
  49. statuses.each do |status|
  50. FeedManager.instance.unpush_from_home(follower, status)
  51. end
  52. end
  53. end
  54. def unpush_from_list_timelines(account, statuses)
  55. account.lists_for_local_distribution.select(:id, :account_id).each do |list|
  56. statuses.each do |status|
  57. FeedManager.instance.unpush_from_list(list, status)
  58. end
  59. end
  60. end
  61. def unpush_from_public_timelines(status)
  62. return unless status.public_visibility?
  63. payload = @json_payloads[status.id]
  64. redis.pipelined do
  65. redis.publish('timeline:public', payload)
  66. redis.publish('timeline:public:local', payload) if status.local?
  67. if status.media_attachments.any?
  68. redis.publish('timeline:public:media', payload)
  69. redis.publish('timeline:public:local:media', payload) if status.local?
  70. end
  71. @tags[status.id].each do |hashtag|
  72. redis.publish("timeline:hashtag:#{hashtag}", payload)
  73. redis.publish("timeline:hashtag:#{hashtag}:local", payload) if status.local?
  74. end
  75. end
  76. end
  77. def unpush_from_direct_timelines(status)
  78. payload = @json_payloads[status.id]
  79. redis.pipelined do
  80. @mentions[status.id].each do |mention|
  81. redis.publish("timeline:direct:#{mention.account.id}", payload) if mention.account.local?
  82. end
  83. redis.publish("timeline:direct:#{status.account.id}", payload) if status.account.local?
  84. end
  85. end
  86. def batch_salmon_slaps(status)
  87. return if @mentions[status.id].empty?
  88. recipients = @mentions[status.id].map(&:account).reject(&:local?).select(&:ostatus?).uniq(&:domain).map(&:id)
  89. recipients.each do |recipient_id|
  90. @salmon_batches << [build_xml(status.stream_entry), status.account_id, recipient_id]
  91. end
  92. end
  93. def redis
  94. Redis.current
  95. end
  96. def build_xml(stream_entry)
  97. return @activity_xml[stream_entry.id] if @activity_xml.key?(stream_entry.id)
  98. @activity_xml[stream_entry.id] = stream_entry_to_xml(stream_entry)
  99. end
  100. end