1
0
Fork 0
forked from gitea/glitch-bird

Refactor emoji reactions

Instead of processing tag and then look for the custom emoji, let the processing return an emoji.

Add `name` to `process_emoji_tags` to check if it matches the shortcode.

Removed `process_single_emoji` and added its code to `process_emoji_tags`

Removed arg from `maybe_process_misskey_reaction`.
Ideally, `original_status` should be a global object, but I wanted to modify vanilla code as little as possible.

Signed-off-by: Plastikmensch <plastikmensch@users.noreply.github.com>
This commit is contained in:
Plastikmensch 2023-05-17 14:53:11 +02:00 committed by Jeremy Kescher
parent 4e15a89b39
commit 3a91f535fa
No known key found for this signature in database
GPG key ID: 80A419A7A613DFA4
4 changed files with 20 additions and 30 deletions

View file

@ -182,21 +182,17 @@ class ActivityPub::Activity
# Ensure emoji declared in the activity's tags are
# present in the database and downloaded to the local cache.
# Required by EmojiReact and Like for emoji reactions.
def process_emoji_tags(tags)
emoji_tag = as_array(tags).find { |tag| tag['type'] == 'Emoji' }
return if emoji_tag.nil?
def process_emoji_tags(name, tags)
tag = as_array(tags).find { |item| item['type'] == 'Emoji' }
return if tag.nil?
process_single_emoji emoji_tag
end
def process_single_emoji(tag)
custom_emoji_parser = ActivityPub::Parser::CustomEmojiParser.new(tag)
return if custom_emoji_parser.shortcode.blank? || custom_emoji_parser.image_remote_url.blank?
return if custom_emoji_parser.shortcode.blank? || custom_emoji_parser.image_remote_url.blank? || !name.eql?(custom_emoji_parser.shortcode)
emoji = CustomEmoji.find_by(shortcode: custom_emoji_parser.shortcode, domain: @account.domain)
return unless emoji.nil? ||
custom_emoji_parser.image_remote_url != emoji.image_remote_url ||
(custom_emoji_parser.updated_at && custom_emoji_parser.updated_at >= emoji.updated_at)
return emoji unless emoji.nil? ||
custom_emoji_parser.image_remote_url != emoji.image_remote_url ||
(custom_emoji_parser.updated_at && custom_emoji_parser.updated_at >= emoji.updated_at)
begin
emoji ||= CustomEmoji.new(domain: @account.domain,
@ -206,6 +202,8 @@ class ActivityPub::Activity
emoji.save
rescue Seahorse::Client::NetworkingError => e
Rails.logger.warn "Error fetching emoji: #{e}"
return
end
emoji
end
end

View file

@ -8,12 +8,10 @@ class ActivityPub::Activity::EmojiReact < ActivityPub::Activity
!original_status.account.local? ||
delete_arrived_first?(@json['id'])
custom_emoji = nil
if /^:.*:$/.match?(name)
process_emoji_tags(@json['tag'])
name.delete! ':'
custom_emoji = CustomEmoji.find_by(shortcode: name, domain: @account.domain)
custom_emoji = process_emoji_tags(@json['tag'])
return if custom_emoji.nil?
end

View file

@ -5,7 +5,7 @@ class ActivityPub::Activity::Like < ActivityPub::Activity
original_status = status_from_uri(object_uri)
return if original_status.nil? || !original_status.account.local? || delete_arrived_first?(@json['id'])
return if maybe_process_misskey_reaction(original_status)
return if maybe_process_misskey_reaction
return if @account.favourited?(original_status)
@ -17,16 +17,15 @@ class ActivityPub::Activity::Like < ActivityPub::Activity
# Misskey delivers reactions as likes with the emoji in _misskey_reaction
# see https://misskey-hub.net/ns.html#misskey-reaction for details
def maybe_process_misskey_reaction(original_status)
def maybe_process_misskey_reaction
original_status = status_from_uri(object_uri)
name = @json['_misskey_reaction']
return false if name.nil?
custom_emoji = nil
if /^:.*:$/.match?(name)
process_emoji_tags(@json['tag'])
name.delete! ':'
custom_emoji = CustomEmoji.find_by(shortcode: name, domain: @account.domain)
custom_emoji = process_emoji_tags(@json['tag'])
return false if custom_emoji.nil? # invalid custom emoji, treat it as a regular like
end
return true if @account.reacted?(original_status, name, custom_emoji)

View file

@ -119,22 +119,17 @@ class ActivityPub::Activity::Undo < ActivityPub::Activity
def undo_emoji_react
name = @object['content'] || @object['_misskey_reaction']
tags = @object['tag']
return if name.nil?
status = status_from_uri(target_uri)
name.delete! ':'
return if status.nil? || !status.account.local?
custom_emoji = nil
emoji_tag = as_array(tags).find { |tag| tag['type'] == 'Emoji' }
if /^:.*:$/.match?(name)
name.delete! ':'
custom_emoji = process_emoji_tags(name, @object['tag'])
if emoji_tag
custom_emoji_parser = ActivityPub::Parser::CustomEmojiParser.new(emoji_tag)
return if custom_emoji_parser.shortcode.blank? || custom_emoji_parser.image_remote_url.blank?
custom_emoji = CustomEmoji.find_by(shortcode: custom_emoji_parser.shortcode, domain: @account.domain)
return if custom_emoji.nil?
end
if @account.reacted?(status, name, custom_emoji)