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:
parent
9958664f55
commit
cb75d43185
10 changed files with 126 additions and 3 deletions
|
@ -8,12 +8,12 @@ class Api::V1::Statuses::ReactionsController < Api::BaseController
|
||||||
before_action :set_reaction, except: :update
|
before_action :set_reaction, except: :update
|
||||||
|
|
||||||
def update
|
def update
|
||||||
@status.status_reactions.create!(account: current_account, name: params[:id])
|
StatusReactionService.new.call(current_account, @status, params[:id])
|
||||||
render_empty
|
render_empty
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
@reaction.destroy!
|
StatusUnreactionService.new.call(current_account, @status)
|
||||||
render_empty
|
render_empty
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -39,6 +39,8 @@ class ActivityPub::Activity
|
||||||
ActivityPub::Activity::Follow
|
ActivityPub::Activity::Follow
|
||||||
when 'Like'
|
when 'Like'
|
||||||
ActivityPub::Activity::Like
|
ActivityPub::Activity::Like
|
||||||
|
when 'EmojiReact'
|
||||||
|
ActivityPub::Activity::EmojiReact
|
||||||
when 'Block'
|
when 'Block'
|
||||||
ActivityPub::Activity::Block
|
ActivityPub::Activity::Block
|
||||||
when 'Update'
|
when 'Update'
|
||||||
|
|
14
app/lib/activitypub/activity/emoji_react.rb
Normal file
14
app/lib/activitypub/activity/emoji_react.rb
Normal 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
|
|
@ -239,6 +239,10 @@ module AccountInteractions
|
||||||
status.proper.favourites.where(account: self).exists?
|
status.proper.favourites.where(account: self).exists?
|
||||||
end
|
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)
|
def bookmarked?(status)
|
||||||
status.proper.bookmarks.where(account: self).exists?
|
status.proper.bookmarks.where(account: self).exists?
|
||||||
end
|
end
|
||||||
|
|
|
@ -71,7 +71,7 @@ class Status < ApplicationRecord
|
||||||
has_many :mentioned_accounts, through: :mentions, source: :account, class_name: 'Account'
|
has_many :mentioned_accounts, through: :mentions, source: :account, class_name: 'Account'
|
||||||
has_many :active_mentions, -> { active }, class_name: 'Mention', inverse_of: :status
|
has_many :active_mentions, -> { active }, class_name: 'Mention', inverse_of: :status
|
||||||
has_many :media_attachments, dependent: :nullify
|
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 :tags
|
||||||
has_and_belongs_to_many :preview_cards
|
has_and_belongs_to_many :preview_cards
|
||||||
|
|
36
app/serializers/activitypub/emoji_reaction_serializer.rb
Normal file
36
app/serializers/activitypub/emoji_reaction_serializer.rb
Normal 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
|
|
@ -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
|
0
app/serializers/undo_emoji_reaction_serializer.rb
Normal file
0
app/serializers/undo_emoji_reaction_serializer.rb
Normal file
27
app/services/status_reaction_service.rb
Normal file
27
app/services/status_reaction_service.rb
Normal 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
|
21
app/services/status_unreaction_service.rb
Normal file
21
app/services/status_unreaction_service.rb
Normal 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
|
Loading…
Reference in a new issue