From 6fb6a539389d82caf1a55ea517919977d8ee6e88 Mon Sep 17 00:00:00 2001 From: ThibG Date: Sun, 30 Dec 2018 19:00:04 +0100 Subject: [PATCH] Avoid duplicate work by merging ReplyDistributionWorker into DistributionWorker (#9660) --- app/services/post_status_service.rb | 1 - .../activitypub/distribution_worker.rb | 9 +++- .../activitypub/reply_distribution_worker.rb | 42 ------------------- 3 files changed, 8 insertions(+), 44 deletions(-) delete mode 100644 app/workers/activitypub/reply_distribution_worker.rb diff --git a/app/services/post_status_service.rb b/app/services/post_status_service.rb index d0c4fe146e..eff1b1461c 100644 --- a/app/services/post_status_service.rb +++ b/app/services/post_status_service.rb @@ -44,7 +44,6 @@ class PostStatusService < BaseService DistributionWorker.perform_async(status.id) Pubsubhubbub::DistributionWorker.perform_async(status.stream_entry.id) ActivityPub::DistributionWorker.perform_async(status.id) - ActivityPub::ReplyDistributionWorker.perform_async(status.id) if status.reply? && status.thread.account.local? if options[:idempotency].present? redis.setex("idempotency:status:#{account.id}:#{options[:idempotency]}", 3_600, status.id) diff --git a/app/workers/activitypub/distribution_worker.rb b/app/workers/activitypub/distribution_worker.rb index 59aacabfd7..d83f017000 100644 --- a/app/workers/activitypub/distribution_worker.rb +++ b/app/workers/activitypub/distribution_worker.rb @@ -31,7 +31,14 @@ class ActivityPub::DistributionWorker end def inboxes - @inboxes ||= @account.followers.inboxes + # Deliver the status to all followers. + # If the status is a reply to another local status, also forward it to that + # status' authors' followers. + @inboxes ||= if @status.reply? && @status.thread.account.local? && @status.distributable? + @account.followers.or(@status.thread.account.followers).inboxes + else + @account.followers.inboxes + end end def signed_payload diff --git a/app/workers/activitypub/reply_distribution_worker.rb b/app/workers/activitypub/reply_distribution_worker.rb deleted file mode 100644 index 892cc14747..0000000000 --- a/app/workers/activitypub/reply_distribution_worker.rb +++ /dev/null @@ -1,42 +0,0 @@ -# frozen_string_literal: true - -class ActivityPub::ReplyDistributionWorker - include Sidekiq::Worker - - sidekiq_options queue: 'push' - - def perform(status_id) - @status = Status.find(status_id) - @account = @status.thread&.account - - return unless @account.present? && @status.distributable? - - ActivityPub::DeliveryWorker.push_bulk(inboxes) do |inbox_url| - [payload, @status.account_id, inbox_url] - end - rescue ActiveRecord::RecordNotFound - true - end - - private - - def inboxes - @inboxes ||= @account.followers.inboxes - end - - def signed_payload - Oj.dump(ActivityPub::LinkedDataSignature.new(unsigned_payload).sign!(@status.account)) - end - - def unsigned_payload - ActiveModelSerializers::SerializableResource.new( - @status, - serializer: ActivityPub::ActivitySerializer, - adapter: ActivityPub::Adapter - ).as_json - end - - def payload - @payload ||= @status.distributable? ? signed_payload : Oj.dump(unsigned_payload) - end -end