chuckya/app/services/react_service.rb

50 lines
1.5 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2022-11-29 17:15:52 +09:00
class ReactService < BaseService
include Authorization
include Payloadable
def call(account, status, emoji)
authorize_with account, status, :react?
name, domain = emoji.split('@')
return unless domain.nil? || status.local?
custom_emoji = CustomEmoji.find_by(shortcode: name, domain: domain)
reaction = StatusReaction.find_by(account: account, status: status, name: name, custom_emoji: custom_emoji)
return reaction unless reaction.nil?
reaction = StatusReaction.create!(account: account, status: status, name: name, custom_emoji: custom_emoji)
2023-11-13 11:59:36 +09:00
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?
2023-11-13 11:59:36 +09:00
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
2023-11-13 11:59:36 +09:00
end
2023-11-13 11:59:36 +09:00
def bump_potential_friendship(account, status)
ActivityTracker.increment('activity:interactions')
2023-11-13 11:59:36 +09:00
return if account.following?(status.account_id)
2023-11-13 11:59:36 +09:00
PotentialFriendshipTracker.record(account.id, status.account_id, :reaction)
end
def build_json(reaction)
Oj.dump(serialize_payload(reaction, ActivityPub::EmojiReactionSerializer))
end
end