From 7f7c635f62f8252c8bbfb80b5041272c9318e3a6 Mon Sep 17 00:00:00 2001 From: fef Date: Mon, 28 Nov 2022 22:23:13 +0000 Subject: [PATCH] federate emoji reactions this is kind of experimental, but it should work in theory. at least i tested it with a remove akkoma instance and it didn't crash. --- .../api/v1/statuses/reactions_controller.rb | 4 +-- app/lib/activitypub/activity.rb | 2 ++ app/lib/activitypub/activity/emoji_react.rb | 14 ++++++++ app/models/concerns/account_interactions.rb | 4 +++ app/models/status.rb | 2 +- .../activitypub/emoji_reaction_serializer.rb | 36 +++++++++++++++++++ .../undo_emoji_reaction_serializer.rb | 19 ++++++++++ .../undo_emoji_reaction_serializer.rb | 0 app/services/status_reaction_service.rb | 27 ++++++++++++++ app/services/status_unreaction_service.rb | 21 +++++++++++ 10 files changed, 126 insertions(+), 3 deletions(-) create mode 100644 app/lib/activitypub/activity/emoji_react.rb create mode 100644 app/serializers/activitypub/emoji_reaction_serializer.rb create mode 100644 app/serializers/activitypub/undo_emoji_reaction_serializer.rb create mode 100644 app/serializers/undo_emoji_reaction_serializer.rb create mode 100644 app/services/status_reaction_service.rb create mode 100644 app/services/status_unreaction_service.rb diff --git a/app/controllers/api/v1/statuses/reactions_controller.rb b/app/controllers/api/v1/statuses/reactions_controller.rb index 9a1bf5707..f7dc2f99c 100644 --- a/app/controllers/api/v1/statuses/reactions_controller.rb +++ b/app/controllers/api/v1/statuses/reactions_controller.rb @@ -8,12 +8,12 @@ class Api::V1::Statuses::ReactionsController < Api::BaseController before_action :set_reaction, except: :update def update - @status.status_reactions.create!(account: current_account, name: params[:id]) + StatusReactionService.new.call(current_account, @status, params[:id]) render_empty end def destroy - @reaction.destroy! + StatusUnreactionService.new.call(current_account, @status) render_empty end diff --git a/app/lib/activitypub/activity.rb b/app/lib/activitypub/activity.rb index 5d9596254..9baac1d01 100644 --- a/app/lib/activitypub/activity.rb +++ b/app/lib/activitypub/activity.rb @@ -39,6 +39,8 @@ class ActivityPub::Activity ActivityPub::Activity::Follow when 'Like' ActivityPub::Activity::Like + when 'EmojiReact' + ActivityPub::Activity::EmojiReact when 'Block' ActivityPub::Activity::Block when 'Update' diff --git a/app/lib/activitypub/activity/emoji_react.rb b/app/lib/activitypub/activity/emoji_react.rb new file mode 100644 index 000000000..82c098f56 --- /dev/null +++ b/app/lib/activitypub/activity/emoji_react.rb @@ -0,0 +1,14 @@ +# frozen_string_literal: true + +class ActivityPub::Activity::EmojiReact < ActivityPub::Activity + def perform + original_status = status_from_uri(object_uri) + name = @json['content'] + return if original_status.nil? || + !original_status.account.local? || + delete_arrived_first?(@json['id']) || + @account.reacted?(original_status, name) + + original_status.status_reactions.create!(account: @account, name: name) + end +end diff --git a/app/models/concerns/account_interactions.rb b/app/models/concerns/account_interactions.rb index 48ab1349d..42d582ce8 100644 --- a/app/models/concerns/account_interactions.rb +++ b/app/models/concerns/account_interactions.rb @@ -237,6 +237,10 @@ module AccountInteractions status.proper.favourites.where(account: self).exists? end + def reacted?(status, name, custom_emoji = nil) + status.proper.status_reactions.where(account: self, name: name, custom_emoji: custom_emoji).exists? + end + def bookmarked?(status) status.proper.bookmarks.where(account: self).exists? end diff --git a/app/models/status.rb b/app/models/status.rb index df450acfe..51ba22fb5 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -72,7 +72,7 @@ class Status < ApplicationRecord has_many :mentioned_accounts, through: :mentions, source: :account, class_name: 'Account' has_many :active_mentions, -> { active }, class_name: 'Mention', inverse_of: :status has_many :media_attachments, dependent: :nullify - has_many :status_reactions, dependent: :destroy + has_many :status_reactions, inverse_of: :status, dependent: :destroy has_and_belongs_to_many :tags has_and_belongs_to_many :preview_cards diff --git a/app/serializers/activitypub/emoji_reaction_serializer.rb b/app/serializers/activitypub/emoji_reaction_serializer.rb new file mode 100644 index 000000000..b4111150a --- /dev/null +++ b/app/serializers/activitypub/emoji_reaction_serializer.rb @@ -0,0 +1,36 @@ +# frozen_string_literal: true + +class ActivityPub::EmojiReactionSerializer < ActivityPub::Serializer + attributes :id, :type, :actor, :content + attribute :virtual_object, key: :object + + has_one :custom_emoji, key: :tag, serializer: ActivityPub::EmojiSerializer, unless: -> { object.custom_emoji.nil? } + + def id + [ActivityPub::TagManager.instance.uri_for(object.account), '#emoji_reactions/', object.id].join + end + + def type + 'EmojiReact' + end + + def actor + ActivityPub::TagManager.instance.uri_for(object.account) + end + + def virtual_object + ActivityPub::TagManager.instance.uri_for(object.status) + end + + def content + if object.custom_emoji.nil? + object.name + else + ":#{object.name}:" + end + end + + def reaction + content + end +end diff --git a/app/serializers/activitypub/undo_emoji_reaction_serializer.rb b/app/serializers/activitypub/undo_emoji_reaction_serializer.rb new file mode 100644 index 000000000..49f0c1c8f --- /dev/null +++ b/app/serializers/activitypub/undo_emoji_reaction_serializer.rb @@ -0,0 +1,19 @@ +# frozen_string_literal: true + +class ActivityPub::UndoEmojiReactionSerializer < ActivityPub::Serializer + attributes :id, :type, :actor + + has_one :object, serializer: ActivityPub::EmojiReactionSerializer + + def id + [ActivityPub::TagManager.instance.uri_for(object.account), '#emoji_reactions/', object.id, '/undo'].join + end + + def type + 'Undo' + end + + def actor + ActivityPub::TagManager.instance.uri_for(object.account) + end +end diff --git a/app/serializers/undo_emoji_reaction_serializer.rb b/app/serializers/undo_emoji_reaction_serializer.rb new file mode 100644 index 000000000..e69de29bb diff --git a/app/services/status_reaction_service.rb b/app/services/status_reaction_service.rb new file mode 100644 index 000000000..17acfe748 --- /dev/null +++ b/app/services/status_reaction_service.rb @@ -0,0 +1,27 @@ +# frozen_string_literal: true + +class StatusReactionService < BaseService + include Authorization + include Payloadable + + def call(account, status, emoji) + reaction = StatusReaction.find_by(account: account, status: status) + return reaction unless reaction.nil? + + name, domain = emoji.split("@") + + custom_emoji = CustomEmoji.find_by(shortcode: name, domain: domain) + 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? + ActivityPub::RawDistributionWorker.perform_async(json, status.account.id) + else + ActivityPub::DeliveryWorker.perform_async(json, reaction.account_id, status.account.inbox_url) + end + + ActivityTracker.increment('activity:interactions') + + reaction + end +end diff --git a/app/services/status_unreaction_service.rb b/app/services/status_unreaction_service.rb new file mode 100644 index 000000000..13c3c428d --- /dev/null +++ b/app/services/status_unreaction_service.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class StatusUnreactionService < BaseService + include Payloadable + + def call(account, status) + reaction = StatusReaction.find_by(account: account, status: status) + 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 + + reaction + end +end