闭社主体 forked from https://github.com/tootsuite/mastodon
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.

50 lines
1.6 KiB

  1. # frozen_string_literal: true
  2. class Pubsubhubbub::DistributionWorker
  3. include Sidekiq::Worker
  4. sidekiq_options queue: 'push'
  5. def perform(stream_entry_ids)
  6. stream_entries = StreamEntry.where(id: stream_entry_ids).includes(:status).reject { |e| e.status.nil? || e.status.direct_visibility? }
  7. return if stream_entries.empty?
  8. @account = stream_entries.first.account
  9. @subscriptions = active_subscriptions.to_a
  10. distribute_public!(stream_entries.reject(&:hidden?))
  11. distribute_hidden!(stream_entries.select(&:hidden?)) if Rails.configuration.x.use_ostatus_privacy
  12. end
  13. private
  14. def distribute_public!(stream_entries)
  15. return if stream_entries.empty?
  16. @payload = OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.feed(@account, stream_entries))
  17. Pubsubhubbub::DeliveryWorker.push_bulk(@subscriptions) do |subscription|
  18. [subscription.id, @payload]
  19. end
  20. end
  21. def distribute_hidden!(stream_entries)
  22. return if stream_entries.empty?
  23. @payload = OStatus::AtomSerializer.render(OStatus::AtomSerializer.new.feed(@account, stream_entries))
  24. @domains = @account.followers.domains
  25. Pubsubhubbub::DeliveryWorker.push_bulk(@subscriptions.select { |s| allowed_to_receive?(s.callback_url, s.domain) }) do |subscription|
  26. [subscription.id, @payload]
  27. end
  28. end
  29. def active_subscriptions
  30. Subscription.where(account: @account).active.select('id, callback_url, domain')
  31. end
  32. def allowed_to_receive?(callback_url, domain)
  33. (!domain.nil? && @domains.include?(domain)) || @domains.include?(Addressable::URI.parse(callback_url).host)
  34. end
  35. end