Skip to content

Commit

Permalink
Refactor react services
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEssem committed Nov 15, 2023
1 parent 0b980e4 commit 4d0e0e6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
1 change: 1 addition & 0 deletions app/lib/potential_friendship_tracker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class PotentialFriendshipTracker

WEIGHTS = {
reply: 1,
reaction: 5,
favourite: 10,
reblog: 20,
}.freeze
Expand Down
30 changes: 24 additions & 6 deletions app/services/react_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,34 @@ def call(account, status, emoji)

reaction = StatusReaction.create!(account: account, status: status, name: name, custom_emoji: custom_emoji)

json = Oj.dump(serialize_payload(reaction, ActivityPub::EmojiReactionSerializer))
Trends.statuses.register(status)

create_notification(reaction)
bump_potential_friendship(account, status)

reaction
end

private

def create_notification(reaction)
status = reaction.status

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)
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)

reaction
PotentialFriendshipTracker.record(account.id, status.account_id, :reaction)
end

def build_json(reaction)
Oj.dump(serialize_payload(reaction, ActivityPub::EmojiReactionSerializer))
end
end
18 changes: 11 additions & 7 deletions app/services/unreact_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ def call(account, status, emoji)
return if reaction.nil?

reaction.destroy!
create_notification(reaction) if !status.account.local? && status.account.activitypub?
reaction
end

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
private

reaction
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

0 comments on commit 4d0e0e6

Please sign in to comment.