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.4 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&.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.reject { |s| !s.hidden? })
  12. end
  13. private
  14. def distribute_public!(stream_entries)
  15. return if stream_entries.empty?
  16. @payload = AtomSerializer.render(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 = AtomSerializer.render(AtomSerializer.new.feed(@account, stream_entries))
  24. @domains = @account.followers.domains
  25. Pubsubhubbub::DeliveryWorker.push_bulk(@subscriptions.reject { |s| !allowed_to_receive?(s.callback_url) }) do |subscription|
  26. [subscription.id, @payload]
  27. end
  28. end
  29. def active_subscriptions
  30. Subscription.where(account: @account).active.select('id, callback_url')
  31. end
  32. def allowed_to_receive?(callback_url)
  33. @domains.include?(Addressable::URI.parse(callback_url).host)
  34. end
  35. end