Skip to content

Commit

Permalink
Refactor react services
Browse files Browse the repository at this point in the history
  • Loading branch information
TheEssem committed Dec 29, 2023
1 parent 421e3ea commit 8eda40c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
27 changes: 21 additions & 6 deletions app/services/react_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,31 @@ 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)
increment_statistics

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 increment_statistics
ActivityTracker.increment('activity:interactions')
end

reaction
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 8eda40c

Please sign in to comment.