Skip to content

Commit

Permalink
#842 カスタム絵文字を利用した絵文字リアクションの他サーバーからの受け入れにおいて、カスタム絵文字のuriだけ指定されたActivit…
Browse files Browse the repository at this point in the history
…yに対応する
  • Loading branch information
kmycode committed Sep 12, 2024
1 parent 3277819 commit fe0a995
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 19 deletions.
65 changes: 47 additions & 18 deletions app/lib/activitypub/activity/like.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ def shortcode
end

def process_emoji(tag)
return process_emoji_by_uri(as_array(tag)[0]) if tag.is_a?(String) || tag.is_a?(Array)

custom_emoji_parser = ActivityPub::Parser::CustomEmojiParser.new(tag)

return if custom_emoji_parser.shortcode.blank? || custom_emoji_parser.image_remote_url.blank?
Expand All @@ -100,35 +102,62 @@ def process_emoji(tag)
custom_emoji_parser = original_emoji_parser(custom_emoji_parser) if @account.domain != domain
return if custom_emoji_parser.nil?

begin
emoji ||= CustomEmoji.new(
domain: domain,
shortcode: custom_emoji_parser.shortcode,
uri: custom_emoji_parser.uri
)
emoji.image_remote_url = custom_emoji_parser.image_remote_url
emoji.license = custom_emoji_parser.license
emoji.is_sensitive = custom_emoji_parser.is_sensitive
emoji.aliases = custom_emoji_parser.aliases
emoji.save
rescue Seahorse::Client::NetworkingError => e
Rails.logger.warn "Error storing emoji: #{e}"
end

emoji
update_custom_emoji!(emoji, custom_emoji_parser, domain)
end

def original_emoji_parser(custom_emoji_parser)
uri = custom_emoji_parser.uri
fetch_original_emoji_parser(custom_emoji_parser.uri, custom_emoji_parser.shortcode || '')
end

def fetch_original_emoji_parser(uri, shortcode = nil)
emoji = fetch_resource_without_id_validation(uri)
return nil unless emoji

parser = ActivityPub::Parser::CustomEmojiParser.new(emoji)
return nil unless parser.uri == uri && custom_emoji_parser.shortcode == parser.shortcode
return nil unless parser.uri == uri
return nil if shortcode.present? && shortcode != parser.shortcode

parser
end

def process_emoji_by_uri(uri)
return if uri.blank?

domain = URI.split(uri)[2] || @account.domain

if domain == Rails.configuration.x.local_domain || domain == Rails.configuration.x.web_domain
# Block overwriting remote-but-local data
return CustomEmoji.find_by(id: ActivityPub::TagManager.instance.uri_to_local_id)
end

return if domain.present? && skip_download?(domain)

custom_emoji_parser = nil
custom_emoji_parser = fetch_original_emoji_parser(uri) if @account.domain != domain
custom_emoji_parser ||= CustomEmoji.find_by(uri: uri)
return if custom_emoji_parser.nil?

update_custom_emoji!(CustomEmoji.find_by(uri: uri), custom_emoji_parser, domain)
end

def update_custom_emoji!(emoji, custom_emoji_parser, domain)
emoji ||= CustomEmoji.new(
domain: domain,
shortcode: custom_emoji_parser.shortcode,
uri: custom_emoji_parser.uri
)
emoji.image_remote_url = custom_emoji_parser.image_remote_url
emoji.license = custom_emoji_parser.license
emoji.is_sensitive = custom_emoji_parser.is_sensitive
emoji.aliases = custom_emoji_parser.aliases
emoji.save

emoji
rescue Seahorse::Client::NetworkingError => e
Rails.logger.warn "Error storing emoji: #{e}"
emoji
end

def skip_download?(domain)
return true if DomainBlock.reject_media?(domain)
return false if @account.domain == domain
Expand Down
27 changes: 27 additions & 0 deletions db/migrate/20240912234211_add_custom_emoji_index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

class AddCustomEmojiIndex < ActiveRecord::Migration[7.1]
disable_ddl_transaction!

class CustomEmoji < ApplicationRecord
end

def up
duplications = CustomEmoji.where('uri IN (SELECT uri FROM custom_emojis GROUP BY uri HAVING COUNT(*) > 1)')
.to_a.group_by(&:uri).to_h

if duplications.any?
CustomEmoji.transaction do
duplications.each do |h|
h[1].drop(1).each(&:destroy)
end
end
end

add_index :custom_emojis, :uri, unique: true, algorithm: :concurrently
end

def down
remove_index :custom_emojis, :uri
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.1].define(version: 2024_08_28_123604) do
ActiveRecord::Schema[7.1].define(version: 2024_09_12_234211) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -515,6 +515,7 @@
t.string "license"
t.integer "image_file_size"
t.index ["shortcode", "domain"], name: "index_custom_emojis_on_shortcode_and_domain", unique: true
t.index ["uri"], name: "index_custom_emojis_on_uri", unique: true
end

create_table "custom_filter_keywords", force: :cascade do |t|
Expand Down
2 changes: 2 additions & 0 deletions lib/tasks/dangerous.rake
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace :dangerous do
end

target_migrations = %w(
20240912234211
20240828123604
20240709063700
20240426233435
Expand Down Expand Up @@ -189,6 +190,7 @@ namespace :dangerous do
index_statuses_on_conversation_id
index_preview_cards_vacuum
index_media_attachments_vacuum
index_custom_emojis_on_uri
)

prompt.say 'Processing...'
Expand Down
14 changes: 14 additions & 0 deletions spec/lib/activitypub/activity/like_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,20 @@
expect(subject.first.custom_emoji.domain).to eq 'example.com'
expect(sender.favourited?(status)).to be false
end

context 'without tag info' do
let(:tag) { 'https://example.com/aaa' }

it 'create emoji reaction' do
expect(subject.count).to eq 1
expect(subject.first.name).to eq 'tinking'
expect(subject.first.account).to eq sender
expect(subject.first.custom_emoji).to_not be_nil
expect(subject.first.custom_emoji.shortcode).to eq 'tinking'
expect(subject.first.custom_emoji.domain).to eq 'example.com'
expect(sender.favourited?(status)).to be false
end
end
end

context 'with custom emoji and update license from non-original server account' do
Expand Down

0 comments on commit fe0a995

Please sign in to comment.