distribution_worker.rb 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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&.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