diff --git a/app/models/account.rb b/app/models/account.rb index a6a39fe4b4..147c28a65f 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -238,7 +238,17 @@ class Account < ApplicationRecord local? ? username : "#{username}@#{domain}" end + def pretty_username + # Return special username for user-facing invalid handle accounts + return id.to_s if invalidated_username? + + username + end + def pretty_acct + # Return special handle for user-facing invalid handle accounts + return "#{id}@handle.invalid" if invalidated_username? + local? ? username : "#{username}@#{Addressable::IDNA.to_unicode(domain)}" end @@ -254,14 +264,31 @@ class Account < ApplicationRecord "acct:#{local_username_and_domain}" end + def invalidate_username! + raise ArgumentError if local? + return if invalidated_username? + + # It is very unlikely that we will allow `!` in usernames in the future, + # and we will never allow ` ` in them either, so this ensure this will never + # match a valid username on a remote server. + + # Using the local ID ensures we won't have any conflict. + + update_attribute(:username, "! #{id}") + end + + def invalidated_username? + username.start_with?('! ') + end + def possibly_stale? - last_webfingered_at.nil? || last_webfingered_at <= STALE_THRESHOLD.ago + last_webfingered_at.nil? || last_webfingered_at <= STALE_THRESHOLD.ago || invalidated_username? end def needs_background_refresh? return false if local? - return true if last_webfingered_at.blank? || last_webfingered_at <= BACKGROUND_REFRESH_INTERVAL.ago + return true if last_webfingered_at.blank? || last_webfingered_at <= BACKGROUND_REFRESH_INTERVAL.ago || invalidated_username? # TODO: Remove some time after 4.6 # This is temporary workaround to speed up account refreshs after diff --git a/app/models/concerns/account/finder_concern.rb b/app/models/concerns/account/finder_concern.rb index 249a7b5fd1..e075c2ed04 100644 --- a/app/models/concerns/account/finder_concern.rb +++ b/app/models/concerns/account/finder_concern.rb @@ -25,6 +25,8 @@ module Account::FinderConcern end def find_remote(username, domain) + return if domain == 'handle.invalid' + Account .with_username(username) .with_domain(domain) diff --git a/app/serializers/rest/account_serializer.rb b/app/serializers/rest/account_serializer.rb index 76b822e521..a3544be59c 100644 --- a/app/serializers/rest/account_serializer.rb +++ b/app/serializers/rest/account_serializer.rb @@ -24,6 +24,8 @@ class REST::AccountSerializer < ActiveModel::Serializer attribute :feature_approval attribute :email_subscriptions, if: -> { Rails.application.config.x.email_subscriptions && Setting.email_subscriptions } + attribute :invalid_handle, if: :invalid_handle? + class AccountDecorator < SimpleDelegator def self.model_name Account.model_name @@ -152,6 +154,15 @@ class REST::AccountSerializer < ActiveModel::Serializer object.memorial? end + def username + object.pretty_username + end + + def invalid_handle + object.invalidated_username? + end + alias invalid_handle? invalid_handle + def roles if object.unavailable? || object.user.nil? [] diff --git a/app/serializers/rest/announcement_serializer.rb b/app/serializers/rest/announcement_serializer.rb index d1a011425d..5735f93da0 100644 --- a/app/serializers/rest/announcement_serializer.rb +++ b/app/serializers/rest/announcement_serializer.rb @@ -41,6 +41,10 @@ class REST::AnnouncementSerializer < ActiveModel::Serializer object.id.to_s end + def username + object.pretty_username + end + def url ActivityPub::TagManager.instance.url_for(object) end diff --git a/app/serializers/rest/status_serializer.rb b/app/serializers/rest/status_serializer.rb index 8468029f7d..c06cdc1c9f 100644 --- a/app/serializers/rest/status_serializer.rb +++ b/app/serializers/rest/status_serializer.rb @@ -201,7 +201,7 @@ class REST::StatusSerializer < ActiveModel::Serializer end def username - object.account_username + object.account.pretty_username end def url diff --git a/app/services/activitypub/process_account_service.rb b/app/services/activitypub/process_account_service.rb index 3cf12d45a7..4ee6a0a8be 100644 --- a/app/services/activitypub/process_account_service.rb +++ b/app/services/activitypub/process_account_service.rb @@ -113,15 +113,23 @@ class ActivityPub::ProcessAccountService < BaseService @account.update!(username: @username, domain: @domain) rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotUnique # This account (identified by ActivityPub `id`) is being renamed to a handle that was - # previously known by this Mastodon server as a different account… ignore the renaming for now + # previously known by this Mastodon server as a different account… - # TODO: better handle this scenario, by e.g. renaming the user to something unique - # and scheduling re-discovery + rename_conflicting_account! - @account.restore_attributes([:username, :domain]) + retry end end + def rename_conflicting_account! + conflicting_account = Account.find_remote(@username, @domain) + return if conflicting_account.nil? || conflicting_account.local? || conflicting_account.uri == @account.uri + + conflicting_account.invalidate_username! + + AccountRefreshWorker.perform_async(conflicting_account.id, { 'request_id' => @request_id }) + end + def extract_username_and_domain! # FEP-2c59 defines a `webfinger` attribute that makes things more explicit and spares an extra request in some cases. # It supersedes `preferredUsername`. diff --git a/app/services/resolve_account_service.rb b/app/services/resolve_account_service.rb index cd96b55c74..da844ab149 100644 --- a/app/services/resolve_account_service.rb +++ b/app/services/resolve_account_service.rb @@ -13,16 +13,19 @@ class ResolveAccountService < BaseService # @option options [Boolean] :skip_webfinger Do not attempt any webfinger query or refreshing account data # @option options [Boolean] :skip_cache Get the latest data from origin even if cache is not due to update yet # @option options [Boolean] :suppress_errors When failing, return nil instead of raising an error + # @option options [String] :request_id Used to limit the number of HTTP requests issued from a single outside request # @return [Account] def call(uri, options = {}) return if uri.blank? process_options!(uri, options) + return ActivityPub::FetchRemoteAccountService.new.call(@account.uri, suppress_errors: @options[:suppress_errors], request_id: options[:request_id]) if @account&.remote? && @account.invalidated_username? + # First of all we want to check if we've got the account # record with the URI already, and if so, we can exit early - return if domain_not_allowed?(@domain) + return if domain_not_allowed?(@domain) || @domain == 'handle.invalid' @account ||= Account.find_remote(@username, @domain) diff --git a/app/workers/account_refresh_worker.rb b/app/workers/account_refresh_worker.rb index b129a14226..15b9ba1473 100644 --- a/app/workers/account_refresh_worker.rb +++ b/app/workers/account_refresh_worker.rb @@ -5,10 +5,10 @@ class AccountRefreshWorker sidekiq_options queue: 'pull', retry: 3, dead: false, lock: :until_executed, lock_ttl: 1.day.to_i - def perform(account_id) + def perform(account_id, options = {}) account = Account.find_by(id: account_id) return unless account&.needs_background_refresh? - ResolveAccountService.new.call(account) + ResolveAccountService.new.call(account, { request_id: options['request_id'] }) end end diff --git a/spec/services/activitypub/process_account_service_spec.rb b/spec/services/activitypub/process_account_service_spec.rb index 57c7e745ee..3fced86c96 100644 --- a/spec/services/activitypub/process_account_service_spec.rb +++ b/spec/services/activitypub/process_account_service_spec.rb @@ -496,14 +496,17 @@ RSpec.describe ActivityPub::ProcessAccountService do context 'when the destination handle is already occupied' do let!(:conflicting_account) { Fabricate(:remote_account, username: 'alice', domain: 'example.com', uri: 'https://foo.test/original_alice', inbox_url: 'https://foo.test/original_alice/inbox') } - it 'updates the profile but does not touch the usernames or call AccountMergingWorker' do + it 'updates the profile without creating a new one or calling AccountMergingWorker, renames conflicting and schedules refresh' do expect { subject.call(payload) } - .to not_change { account.reload.username } - .and not_change { account.reload.domain } + .to change { account.reload.username }.from('bob').to('alice') + .and change { account.reload.domain }.from('foo.test').to('example.com') .and not_change { account.reload.uri } - .and not_change { conflicting_account.reload.acct } + .and change { conflicting_account.reload.username }.from('alice').to("! #{conflicting_account.id}") .and(not_change { Account.count }) + expect(AccountRefreshWorker) + .to have_enqueued_sidekiq_job(conflicting_account.id, { 'request_id' => /.*-.*@.*/ }) + expect(AccountMergingWorker) .to_not have_enqueued_sidekiq_job @@ -575,14 +578,17 @@ RSpec.describe ActivityPub::ProcessAccountService do context 'when the destination handle is already occupied' do let!(:conflicting_account) { Fabricate(:remote_account, username: 'alice', domain: 'foo.test', uri: 'https://foo.test/original_alice', inbox_url: 'https://foo.test/original_alice/inbox') } - it 'updates the profile but does not touch the usernames or call AccountMergingWorker' do + it 'updates the profile without creating a new one or calling AccountMergingWorker, renames conflicting and schedules refresh' do expect { subject.call(payload) } - .to not_change { account.reload.username } + .to change { account.reload.username }.from('bob').to('alice') .and not_change { account.reload.domain } .and not_change { account.reload.uri } - .and not_change { conflicting_account.reload.acct } + .and change { conflicting_account.reload.username }.from('alice').to("! #{conflicting_account.id}") .and(not_change { Account.count }) + expect(AccountRefreshWorker) + .to have_enqueued_sidekiq_job(conflicting_account.id, { 'request_id' => /.*-.*@.*/ }) + expect(AccountMergingWorker) .to_not have_enqueued_sidekiq_job