From 27965ce5edff20db2de1dd233c88f8393bb0da0b Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 25 Feb 2022 00:34:14 +0100 Subject: Add trending statuses (#17431) * Add trending statuses * Fix dangling items with stale scores in localized sets * Various fixes and improvements - Change approve_all/reject_all to approve_accounts/reject_accounts - Change Trends::Query methods to not mutate the original query - Change Trends::Query#skip to offset - Change follow recommendations to be refreshed in a transaction * Add tests for trending statuses filtering behaviour * Fix not applying filtering scope in controller --- app/lib/activitypub/activity/announce.rb | 3 +-- app/lib/activitypub/activity/like.rb | 2 ++ 2 files changed, 3 insertions(+), 2 deletions(-) (limited to 'app/lib/activitypub') diff --git a/app/lib/activitypub/activity/announce.rb b/app/lib/activitypub/activity/announce.rb index 12fad8da4..7cd5a41e8 100644 --- a/app/lib/activitypub/activity/announce.rb +++ b/app/lib/activitypub/activity/announce.rb @@ -23,8 +23,7 @@ class ActivityPub::Activity::Announce < ActivityPub::Activity visibility: visibility_from_audience ) - Trends.tags.register(@status) - Trends.links.register(@status) + Trends.register!(@status) distribute end diff --git a/app/lib/activitypub/activity/like.rb b/app/lib/activitypub/activity/like.rb index c065f01f8..ebbda15b9 100644 --- a/app/lib/activitypub/activity/like.rb +++ b/app/lib/activitypub/activity/like.rb @@ -7,6 +7,8 @@ class ActivityPub::Activity::Like < ActivityPub::Activity return if original_status.nil? || !original_status.account.local? || delete_arrived_first?(@json['id']) || @account.favourited?(original_status) favourite = original_status.favourites.create!(account: @account) + NotifyService.new.call(original_status.account, :favourite, favourite) + Trends.statuses.register(original_status) end end -- cgit From 0dc57ab6ed67657e0a77e08bcd99c7b809fe5e42 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Sat, 26 Feb 2022 17:51:59 +0100 Subject: Fix status updates not being forwarded like deletes through ActivityPub (#17648) Fix #17521 --- app/lib/activitypub/activity/delete.rb | 40 ++------------------- app/lib/activitypub/activity/update.rb | 11 ++++-- app/lib/activitypub/forwarder.rb | 65 ++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 40 deletions(-) create mode 100644 app/lib/activitypub/forwarder.rb (limited to 'app/lib/activitypub') diff --git a/app/lib/activitypub/activity/delete.rb b/app/lib/activitypub/activity/delete.rb index 801647cf7..f5ef863f3 100644 --- a/app/lib/activitypub/activity/delete.rb +++ b/app/lib/activitypub/activity/delete.rb @@ -37,50 +37,16 @@ class ActivityPub::Activity::Delete < ActivityPub::Activity return if @status.nil? - forward! if @json['signature'].present? && @status.distributable? + forwarder.forward! if forwarder.forwardable? delete_now! end end - def rebloggers_ids - return @rebloggers_ids if defined?(@rebloggers_ids) - @rebloggers_ids = @status.reblogs.includes(:account).references(:account).merge(Account.local).pluck(:account_id) - end - - def inboxes_for_reblogs - Account.where(id: ::Follow.where(target_account_id: rebloggers_ids).select(:account_id)).inboxes - end - - def replied_to_status - return @replied_to_status if defined?(@replied_to_status) - @replied_to_status = @status.thread - end - - def reply_to_local? - !replied_to_status.nil? && replied_to_status.account.local? - end - - def inboxes_for_reply - replied_to_status.account.followers.inboxes - end - - def forward! - inboxes = inboxes_for_reblogs - inboxes += inboxes_for_reply if reply_to_local? - inboxes -= [@account.preferred_inbox_url] - - sender_id = reply_to_local? ? replied_to_status.account_id : rebloggers_ids.first - - ActivityPub::LowPriorityDeliveryWorker.push_bulk(inboxes.uniq) do |inbox_url| - [payload, sender_id, inbox_url] - end + def forwarder + @forwarder ||= ActivityPub::Forwarder.new(@account, @json, @status) end def delete_now! RemoveStatusService.new.call(@status, redraft: false) end - - def payload - @payload ||= Oj.dump(@json) - end end diff --git a/app/lib/activitypub/activity/update.rb b/app/lib/activitypub/activity/update.rb index f04ad321b..0bfead55b 100644 --- a/app/lib/activitypub/activity/update.rb +++ b/app/lib/activitypub/activity/update.rb @@ -22,10 +22,15 @@ class ActivityPub::Activity::Update < ActivityPub::Activity def update_status return reject_payload! if invalid_origin?(@object['id']) - status = Status.find_by(uri: object_uri, account_id: @account.id) + @status = Status.find_by(uri: object_uri, account_id: @account.id) - return if status.nil? + return if @status.nil? - ActivityPub::ProcessStatusUpdateService.new.call(status, @object) + forwarder.forward! if forwarder.forwardable? + ActivityPub::ProcessStatusUpdateService.new.call(@status, @object) + end + + def forwarder + @forwarder ||= ActivityPub::Forwarder.new(@account, @json, @status) end end diff --git a/app/lib/activitypub/forwarder.rb b/app/lib/activitypub/forwarder.rb new file mode 100644 index 000000000..4206b9d82 --- /dev/null +++ b/app/lib/activitypub/forwarder.rb @@ -0,0 +1,65 @@ +# frozen_string_literal: true + +class ActivityPub::Forwarder + def initialize(account, original_json, status) + @json = original_json + @account = account + @status = status + end + + def forwardable? + @json['signature'].present? && @status.distributable? + end + + def forward! + ActivityPub::LowPriorityDeliveryWorker.push_bulk(inboxes) do |inbox_url| + [payload, signature_account_id, inbox_url] + end + end + + private + + def payload + @payload ||= Oj.dump(@json) + end + + def reblogged_by_account_ids + @reblogged_by_account_ids ||= @status.reblogs.includes(:account).references(:account).merge(Account.local).pluck(:account_id) + end + + def signature_account_id + @signature_account_id ||= begin + if in_reply_to_local? + in_reply_to.account_id + else + reblogged_by_account_ids.first + end + end + end + + def inboxes + @inboxes ||= begin + arr = inboxes_for_followers_of_reblogged_by_accounts + arr += inboxes_for_followers_of_replied_to_account if in_reply_to_local? + arr -= [@account.preferred_inbox_url] + arr.uniq! + arr + end + end + + def inboxes_for_followers_of_reblogged_by_accounts + Account.where(id: ::Follow.where(target_account_id: reblogged_by_account_ids).select(:account_id)).inboxes + end + + def inboxes_for_followers_of_replied_to_account + in_reply_to.account.followers.inboxes + end + + def in_reply_to + @status.thread + end + + def in_reply_to_local? + @status.thread&.account&.local? + end +end -- cgit From 04c3ac896a6054b8158cba06daa9f333f890efd4 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 4 Mar 2022 01:00:13 +0100 Subject: Fix infinite re-forwarding of update activities temporarily (#17698) --- app/lib/activitypub/activity/update.rb | 5 ----- 1 file changed, 5 deletions(-) (limited to 'app/lib/activitypub') diff --git a/app/lib/activitypub/activity/update.rb b/app/lib/activitypub/activity/update.rb index 0bfead55b..36ad5f446 100644 --- a/app/lib/activitypub/activity/update.rb +++ b/app/lib/activitypub/activity/update.rb @@ -26,11 +26,6 @@ class ActivityPub::Activity::Update < ActivityPub::Activity return if @status.nil? - forwarder.forward! if forwarder.forwardable? ActivityPub::ProcessStatusUpdateService.new.call(@status, @object) end - - def forwarder - @forwarder ||= ActivityPub::Forwarder.new(@account, @json, @status) - end end -- cgit From 2ea754b8610b50cc93aeb1921ecdf7415efaf17e Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Fri, 4 Mar 2022 01:06:33 +0100 Subject: Fix duplicate notifications being possible after poll expiration (#17697) --- app/controllers/api/v1/follow_requests_controller.rb | 2 +- app/lib/activitypub/activity/announce.rb | 2 +- app/lib/activitypub/activity/follow.rb | 4 ++-- app/lib/activitypub/activity/like.rb | 2 +- app/services/bootstrap_timeline_service.rb | 2 +- app/services/favourite_service.rb | 2 +- app/workers/feed_insert_worker.rb | 2 +- app/workers/poll_expiration_notify_worker.rb | 8 +++++--- 8 files changed, 13 insertions(+), 11 deletions(-) (limited to 'app/lib/activitypub') diff --git a/app/controllers/api/v1/follow_requests_controller.rb b/app/controllers/api/v1/follow_requests_controller.rb index 8276245a3..54ff0e11d 100644 --- a/app/controllers/api/v1/follow_requests_controller.rb +++ b/app/controllers/api/v1/follow_requests_controller.rb @@ -13,7 +13,7 @@ class Api::V1::FollowRequestsController < Api::BaseController def authorize AuthorizeFollowService.new.call(account, current_account) - NotifyService.new.call(current_account, :follow, Follow.find_by(account: account, target_account: current_account)) + LocalNotificationWorker.perform_async(current_account.id, Follow.find_by(account: account, target_account: current_account).id, 'Follow', 'follow') render json: account, serializer: REST::RelationshipSerializer, relationships: relationships end diff --git a/app/lib/activitypub/activity/announce.rb b/app/lib/activitypub/activity/announce.rb index 7cd5a41e8..0674b1083 100644 --- a/app/lib/activitypub/activity/announce.rb +++ b/app/lib/activitypub/activity/announce.rb @@ -35,7 +35,7 @@ class ActivityPub::Activity::Announce < ActivityPub::Activity def distribute # Notify the author of the original status if that status is local - NotifyService.new.call(@status.reblog.account, :reblog, @status) if reblog_of_local_account?(@status) && !reblog_by_following_group_account?(@status) + LocalNotificationWorker.perform_async(@status.reblog.account_id, @status.id, 'Status', 'reblog') if reblog_of_local_account?(@status) && !reblog_by_following_group_account?(@status) # Distribute into home and list feeds ::DistributionWorker.perform_async(@status.id) if @options[:override_timestamps] || @status.within_realtime_window? diff --git a/app/lib/activitypub/activity/follow.rb b/app/lib/activitypub/activity/follow.rb index 4efb84b8c..97e41ab78 100644 --- a/app/lib/activitypub/activity/follow.rb +++ b/app/lib/activitypub/activity/follow.rb @@ -31,10 +31,10 @@ class ActivityPub::Activity::Follow < ActivityPub::Activity follow_request = FollowRequest.create!(account: @account, target_account: target_account, uri: @json['id']) if target_account.locked? || @account.silenced? - NotifyService.new.call(target_account, :follow_request, follow_request) + LocalNotificationWorker.perform_async(target_account.id, follow_request.id, 'FollowRequest', 'follow_request') else AuthorizeFollowService.new.call(@account, target_account) - NotifyService.new.call(target_account, :follow, ::Follow.find_by(account: @account, target_account: target_account)) + LocalNotificationWorker.perform_async(target_account.id, ::Follow.find_by(account: @account, target_account: target_account).id, 'Follow', 'follow') end end diff --git a/app/lib/activitypub/activity/like.rb b/app/lib/activitypub/activity/like.rb index ebbda15b9..aa1dc3040 100644 --- a/app/lib/activitypub/activity/like.rb +++ b/app/lib/activitypub/activity/like.rb @@ -8,7 +8,7 @@ class ActivityPub::Activity::Like < ActivityPub::Activity favourite = original_status.favourites.create!(account: @account) - NotifyService.new.call(original_status.account, :favourite, favourite) + LocalNotificationWorker.perform_async(original_status.account_id, favourite.id, 'Favourite', 'favourite') Trends.statuses.register(original_status) end end diff --git a/app/services/bootstrap_timeline_service.rb b/app/services/bootstrap_timeline_service.rb index 312c163e4..a02e55a6d 100644 --- a/app/services/bootstrap_timeline_service.rb +++ b/app/services/bootstrap_timeline_service.rb @@ -18,7 +18,7 @@ class BootstrapTimelineService < BaseService def notify_staff! User.staff.includes(:account).find_each do |user| - NotifyService.new.call(user.account, :'admin.sign_up', @source_account) + LocalNotificationWorker.perform_async(user.account_id, @source_account.id, 'Account', 'admin.sign_up') end end end diff --git a/app/services/favourite_service.rb b/app/services/favourite_service.rb index 0ca0081b4..dc7fe8855 100644 --- a/app/services/favourite_service.rb +++ b/app/services/favourite_service.rb @@ -31,7 +31,7 @@ class FavouriteService < BaseService status = favourite.status if status.account.local? - NotifyService.new.call(status.account, :favourite, favourite) + LocalNotificationWorker.perform_async(status.account_id, favourite.id, 'Favourite', 'favourite') elsif status.account.activitypub? ActivityPub::DeliveryWorker.perform_async(build_json(favourite), favourite.account_id, status.account.inbox_url) end diff --git a/app/workers/feed_insert_worker.rb b/app/workers/feed_insert_worker.rb index 6e3472d57..40bc9cb6e 100644 --- a/app/workers/feed_insert_worker.rb +++ b/app/workers/feed_insert_worker.rb @@ -66,7 +66,7 @@ class FeedInsertWorker end def perform_notify - NotifyService.new.call(@follower, :status, @status) + LocalNotificationWorker.perform_async(@follower.id, @status.id, 'Status', 'status') end def update? diff --git a/app/workers/poll_expiration_notify_worker.rb b/app/workers/poll_expiration_notify_worker.rb index 7613ed5f1..0e29a5f60 100644 --- a/app/workers/poll_expiration_notify_worker.rb +++ b/app/workers/poll_expiration_notify_worker.rb @@ -38,12 +38,14 @@ class PollExpirationNotifyWorker def notify_remote_voters_and_owner! ActivityPub::DistributePollUpdateWorker.perform_async(@poll.status.id) - NotifyService.new.call(@poll.account, :poll, @poll) + LocalNotificationWorker.perform_async(@poll.account_id, @poll.id, 'Poll', 'poll') end def notify_local_voters! - @poll.voters.merge(Account.local).find_each do |account| - NotifyService.new.call(account, :poll, @poll) + @poll.voters.merge(Account.local).select(:id).find_in_batches do |accounts| + LocalNotificationWorker.push_bulk(accounts) do |account| + [account.id, @poll.id, 'Poll', 'poll'] + end end end end -- cgit