2023-11-08 02:43:47 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
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?
|
|
|
|
|
2024-01-14 02:16:57 +02:00
|
|
|
normalized = "#{name}\uFE0F"
|
|
|
|
name = normalized if StatusReactionValidator::SUPPORTED_EMOJIS.include?(normalized)
|
|
|
|
|
2023-11-08 02:43:47 +02:00
|
|
|
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 04:59:36 +02:00
|
|
|
Trends.statuses.register(status)
|
|
|
|
|
|
|
|
create_notification(reaction)
|
|
|
|
increment_statistics
|
|
|
|
|
|
|
|
reaction
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def create_notification(reaction)
|
|
|
|
status = reaction.status
|
|
|
|
|
2023-11-08 02:43:47 +02:00
|
|
|
if status.account.local?
|
2023-11-13 04:59:36 +02: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)
|
2023-11-08 02:43:47 +02:00
|
|
|
end
|
2023-11-13 04:59:36 +02:00
|
|
|
end
|
2023-11-08 02:43:47 +02:00
|
|
|
|
2023-11-13 04:59:36 +02:00
|
|
|
def increment_statistics
|
2023-11-08 02:43:47 +02:00
|
|
|
ActivityTracker.increment('activity:interactions')
|
2023-11-13 04:59:36 +02:00
|
|
|
end
|
2023-11-08 02:43:47 +02:00
|
|
|
|
2023-11-13 04:59:36 +02:00
|
|
|
def build_json(reaction)
|
|
|
|
Oj.dump(serialize_payload(reaction, ActivityPub::EmojiReactionSerializer))
|
2023-11-08 02:43:47 +02:00
|
|
|
end
|
|
|
|
end
|