From 1a42ad69f7b80625ac508cbccf44134f2b9b76b6 Mon Sep 17 00:00:00 2001 From: Plastikmensch Date: Wed, 17 May 2023 14:53:11 +0200 Subject: [PATCH] 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 --- app/lib/activitypub/activity.rb | 20 +++++++++----------- app/lib/activitypub/activity/emoji_react.rb | 6 ++---- app/lib/activitypub/activity/like.rb | 11 +++++------ app/lib/activitypub/activity/undo.rb | 13 ++++--------- 4 files changed, 20 insertions(+), 30 deletions(-) diff --git a/app/lib/activitypub/activity.rb b/app/lib/activitypub/activity.rb index 0fc6d3ef7..572ad907e 100644 --- a/app/lib/activitypub/activity.rb +++ b/app/lib/activitypub/activity.rb @@ -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 diff --git a/app/lib/activitypub/activity/emoji_react.rb b/app/lib/activitypub/activity/emoji_react.rb index 9f28064cc..f5b5a8076 100644 --- a/app/lib/activitypub/activity/emoji_react.rb +++ b/app/lib/activitypub/activity/emoji_react.rb @@ -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 diff --git a/app/lib/activitypub/activity/like.rb b/app/lib/activitypub/activity/like.rb index 1ded77f4a..7ea571d03 100644 --- a/app/lib/activitypub/activity/like.rb +++ b/app/lib/activitypub/activity/like.rb @@ -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) diff --git a/app/lib/activitypub/activity/undo.rb b/app/lib/activitypub/activity/undo.rb index fd836b972..5efcfdc99 100644 --- a/app/lib/activitypub/activity/undo.rb +++ b/app/lib/activitypub/activity/undo.rb @@ -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)