From 77ecf04abc77e3c013d1667673db2df2092c052a Mon Sep 17 00:00:00 2001 From: Jeremy Kescher Date: Sat, 27 May 2023 12:19:29 +0200 Subject: [PATCH] Apply https://github.com/CatCatNya/catstodon/pull/4 to dev branch --- app/lib/activitypub/activity.rb | 21 ++++++++++----------- app/lib/activitypub/activity/emoji_react.rb | 13 +++++++------ app/lib/activitypub/activity/like.rb | 16 +++++++++------- app/lib/activitypub/activity/undo.rb | 13 +++++++++++-- app/models/concerns/account_interactions.rb | 4 ++-- app/models/status.rb | 2 +- app/models/status_reaction.rb | 3 ++- app/services/react_service.rb | 2 ++ app/validators/status_reaction_validator.rb | 2 +- package.json | 1 - 10 files changed, 45 insertions(+), 32 deletions(-) diff --git a/app/lib/activitypub/activity.rb b/app/lib/activitypub/activity.rb index 00d724a282..572ad907e2 100644 --- a/app/lib/activitypub/activity.rb +++ b/app/lib/activitypub/activity.rb @@ -179,23 +179,20 @@ class ActivityPub::Activity nil end - # Ensure all emojis declared in the activity's tags are + # 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) - as_array(tags).each do |tag| - process_single_emoji tag if tag['type'] == 'Emoji' - end - end + def process_emoji_tags(name, tags) + tag = as_array(tags).find { |item| item['type'] == 'Emoji' } + return if tag.nil? - 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, @@ -205,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 526f22803d..f5b5a8076b 100644 --- a/app/lib/activitypub/activity/emoji_react.rb +++ b/app/lib/activitypub/activity/emoji_react.rb @@ -6,20 +6,21 @@ class ActivityPub::Activity::EmojiReact < ActivityPub::Activity name = @json['content'] return if original_status.nil? || !original_status.account.local? || - delete_arrived_first?(@json['id']) || - @account.reacted?(original_status, name) + 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 + return if @account.reacted?(original_status, name, custom_emoji) + reaction = original_status.status_reactions.create!(account: @account, name: name, custom_emoji: custom_emoji) LocalNotificationWorker.perform_async(original_status.account_id, reaction.id, 'StatusReaction', 'reaction') + rescue ActiveRecord::RecordInvalid + nil end end diff --git a/app/lib/activitypub/activity/like.rb b/app/lib/activitypub/activity/like.rb index 8acc99b9db..7ea571d03e 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,22 +17,24 @@ 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) + return true if @account.reacted?(original_status, name, custom_emoji) reaction = original_status.status_reactions.create!(account: @account, name: name, custom_emoji: custom_emoji) LocalNotificationWorker.perform_async(original_status.account_id, reaction.id, 'StatusReaction', 'reaction') true + # account tried to react with disabled custom emoji. Returning true to discard activity. + rescue ActiveRecord::RecordInvalid + true end end diff --git a/app/lib/activitypub/activity/undo.rb b/app/lib/activitypub/activity/undo.rb index 54cda1947f..5efcfdc99f 100644 --- a/app/lib/activitypub/activity/undo.rb +++ b/app/lib/activitypub/activity/undo.rb @@ -110,20 +110,29 @@ class ActivityPub::Activity::Undo < ActivityPub::Activity if @account.favourited?(status) favourite = status.favourites.where(account: @account).first favourite&.destroy + elsif @object['_misskey_reaction'].present? + undo_emoji_react else delete_later!(object_uri) end end def undo_emoji_react - name = @object['content'] + name = @object['content'] || @object['_misskey_reaction'] return if name.nil? status = status_from_uri(target_uri) return if status.nil? || !status.account.local? - if @account.reacted?(status, name.delete(':')) + if /^:.*:$/.match?(name) + name.delete! ':' + custom_emoji = process_emoji_tags(name, @object['tag']) + + return if custom_emoji.nil? + end + + if @account.reacted?(status, name, custom_emoji) reaction = status.status_reactions.where(account: @account, name: name).first reaction&.destroy else diff --git a/app/models/concerns/account_interactions.rb b/app/models/concerns/account_interactions.rb index 89edd50ed2..e3d2449733 100644 --- a/app/models/concerns/account_interactions.rb +++ b/app/models/concerns/account_interactions.rb @@ -243,8 +243,8 @@ module AccountInteractions status.proper.favourites.where(account: self).exists? end - def reacted?(status, name) - status.proper.status_reactions.where(account: self, name: name).exists? + 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) diff --git a/app/models/status.rb b/app/models/status.rb index a8d600ef1a..8a70fa7322 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -300,7 +300,7 @@ class Status < ApplicationRecord if account.nil? scope.select('name, custom_emoji_id, count(*) as count, false as me') else - scope.select("name, custom_emoji_id, count(*) as count, exists(select 1 from status_reactions r where r.account_id = #{account.id} and r.status_id = status_reactions.status_id and r.name = status_reactions.name) as me") + scope.select("name, custom_emoji_id, count(*) as count, exists(select 1 from status_reactions r where r.account_id = #{account.id} and r.status_id = status_reactions.status_id and r.name = status_reactions.name and (r.custom_emoji_id = status_reactions.custom_emoji_id or r.custom_emoji_id is null and status_reactions.custom_emoji_id is null)) as me") end end diff --git a/app/models/status_reaction.rb b/app/models/status_reaction.rb index a54d03a309..bc6eda2a1c 100644 --- a/app/models/status_reaction.rb +++ b/app/models/status_reaction.rb @@ -26,7 +26,8 @@ class StatusReaction < ApplicationRecord private + # Sets custom_emoji to nil when disabled def set_custom_emoji - self.custom_emoji = CustomEmoji.find_by(shortcode: name, domain: account.domain) if name.blank? + self.custom_emoji = CustomEmoji.find_by(disabled: false, shortcode: name, domain: custom_emoji.domain) if name.present? && custom_emoji.present? end end diff --git a/app/services/react_service.rb b/app/services/react_service.rb index 79d1eaaf30..de2fd1de9c 100644 --- a/app/services/react_service.rb +++ b/app/services/react_service.rb @@ -8,6 +8,8 @@ class ReactService < BaseService authorize_with account, status, :react? name, domain = emoji.split('@') + return unless domain.nil? || status.local? + custom_emoji = CustomEmoji.find_by(shortcode: name, domain: domain) reaction = StatusReaction.find_by(account: account, status: status, name: name, custom_emoji: custom_emoji) return reaction unless reaction.nil? diff --git a/app/validators/status_reaction_validator.rb b/app/validators/status_reaction_validator.rb index 0338bf531a..046476de9d 100644 --- a/app/validators/status_reaction_validator.rb +++ b/app/validators/status_reaction_validator.rb @@ -19,7 +19,7 @@ class StatusReactionValidator < ActiveModel::Validator end def new_reaction?(reaction) - !reaction.status.status_reactions.exists?(status: reaction.status, account: reaction.account, name: reaction.name) + !reaction.status.status_reactions.exists?(status: reaction.status, account: reaction.account, name: reaction.name, custom_emoji: reaction.custom_emoji) end def limit_reached?(reaction) diff --git a/package.json b/package.json index f5db9e94f2..e65bdbe753 100644 --- a/package.json +++ b/package.json @@ -216,7 +216,6 @@ }, "lint-staged": { "*": "prettier --ignore-unknown --write", - "Capfile|Gemfile|*.{rb,ruby,ru,rake}": "bundle exec rubocop -a", "*.{js,jsx,ts,tsx}": "eslint --fix", "*.{css,scss}": "stylelint --fix" }