From 39ef8cac9a01bcca6f112f2c24a461d8f821e8f7 Mon Sep 17 00:00:00 2001 From: Essem Date: Sun, 12 Nov 2023 20:59:36 -0600 Subject: [PATCH] Refactor react services --- app/lib/potential_friendship_tracker.rb | 1 + app/services/react_service.rb | 34 +++++++++++++++++++------ app/services/unreact_service.rb | 20 +++++++++------ 3 files changed, 39 insertions(+), 16 deletions(-) diff --git a/app/lib/potential_friendship_tracker.rb b/app/lib/potential_friendship_tracker.rb index f5bc203465..ab1cfeb742 100644 --- a/app/lib/potential_friendship_tracker.rb +++ b/app/lib/potential_friendship_tracker.rb @@ -6,6 +6,7 @@ class PotentialFriendshipTracker WEIGHTS = { reply: 1, + reaction: 5, favourite: 10, reblog: 20, }.freeze diff --git a/app/services/react_service.rb b/app/services/react_service.rb index de2fd1de9c..4ac220f1e9 100644 --- a/app/services/react_service.rb +++ b/app/services/react_service.rb @@ -16,16 +16,34 @@ class ReactService < BaseService reaction = StatusReaction.create!(account: account, status: status, name: name, custom_emoji: custom_emoji) - json = Oj.dump(serialize_payload(reaction, ActivityPub::EmojiReactionSerializer)) - if status.account.local? - NotifyService.new.call(status.account, :reaction, reaction) - ActivityPub::RawDistributionWorker.perform_async(json, status.account.id) - else - ActivityPub::DeliveryWorker.perform_async(json, reaction.account_id, status.account.inbox_url) - end + Trends.statuses.register(status) - ActivityTracker.increment('activity:interactions') + create_notification(reaction) + bump_potential_friendship(account, status) reaction end + + private + + def create_notification(reaction) + status = reaction.status + + if status.account.local? + LocalNotificationWorker.perform_async(status.account_id, reaction.id, 'StatusReaction', 'reaction') + elsif status.account.activitypub? + ActivityPub::DeliveryWorker.perform_async(build_json(reaction), reaction.account_id, status.account.inbox_url) + end + end + + def bump_potential_friendship(account, status) + ActivityTracker.increment('activity:interactions') + return if account.following?(status.account_id) + + PotentialFriendshipTracker.record(account.id, status.account_id, :reaction) + end + + def build_json(reaction) + Oj.dump(serialize_payload(reaction, ActivityPub::EmojiReactionSerializer)) + end end diff --git a/app/services/unreact_service.rb b/app/services/unreact_service.rb index 7c1b32e94f..49d232e931 100644 --- a/app/services/unreact_service.rb +++ b/app/services/unreact_service.rb @@ -10,14 +10,18 @@ class UnreactService < BaseService return if reaction.nil? reaction.destroy! - - json = Oj.dump(serialize_payload(reaction, ActivityPub::UndoEmojiReactionSerializer)) - if status.account.local? - ActivityPub::RawDistributionWorker.perform_async(json, status.account.id) - else - ActivityPub::DeliveryWorker.perform_async(json, reaction.account_id, status.account.inbox_url) - end - + create_notification(reaction) if !status.account.local? && status.account.activitypub? reaction end + + private + + def create_notification(reaction) + status = reaction.status + ActivityPub::DeliveryWorker.perform_async(build_json(reaction), reaction.account_id, status.account.inbox_url) + end + + def build_json(reaction) + Oj.dump(serialize_payload(reaction, ActivityPub::UndoEmojiReactionSerializer)) + end end