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.
This commit is contained in:
fef 2022-11-28 22:23:13 +00:00 committed by neatchee
parent f152cb0e58
commit 7f7c635f62
10 changed files with 126 additions and 3 deletions

View file

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

View file

@ -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'

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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