diff --git a/app/lib/activitypub/activity/update.rb b/app/lib/activitypub/activity/update.rb index 973706f5957..71b6ba5ca41 100644 --- a/app/lib/activitypub/activity/update.rb +++ b/app/lib/activitypub/activity/update.rb @@ -20,7 +20,7 @@ class ActivityPub::Activity::Update < ActivityPub::Activity def update_account return reject_payload! if @account.uri != object_uri - ActivityPub::ProcessAccountService.new.call(@account.username, @account.domain, @object, signed_with_known_key: true, request_id: @options[:request_id]) + ActivityPub::ProcessAccountService.new.call(@account.username, @account.domain, @object, signed_with_known_key: true, request_id: @options[:request_id], allow_username_update: true) end def update_status diff --git a/app/services/activitypub/process_account_service.rb b/app/services/activitypub/process_account_service.rb index e5c23197284..de8c4a78ee0 100644 --- a/app/services/activitypub/process_account_service.rb +++ b/app/services/activitypub/process_account_service.rb @@ -27,7 +27,7 @@ class ActivityPub::ProcessAccountService < BaseService @options[:request_id] ||= "#{Time.now.utc.to_i}-#{username}@#{domain}" with_redis_lock("process_account:#{@uri}") do - @account = Account.remote.find_by(uri: @uri) if @options[:only_key] + @account = Account.remote.find_by(uri: @uri) if find_remote_account_by_uri? @account ||= Account.find_remote(@username, @domain) @old_public_key = @account&.public_key @old_protocol = @account&.protocol @@ -69,6 +69,10 @@ class ActivityPub::ProcessAccountService < BaseService private + def find_remote_account_by_uri? + @options[:only_key] || @options[:allow_username_update] + end + def create_account @account = Account.new @account.protocol = :activitypub @@ -131,6 +135,7 @@ class ActivityPub::ProcessAccountService < BaseService @account.indexable = @json['indexable'] || false @account.memorial = @json['memorial'] || false @account.attribution_domains = as_array(@json['attributionDomains'] || []).map { |item| value_or_id(item) } + @account.username = @json['preferredUsername'] if @options[:allow_username_update] end def set_fetchable_key! diff --git a/spec/requests/activitypub/inboxes_controller_spec.rb b/spec/requests/activitypub/inboxes_controller_spec.rb index 4dbdea36c8b..94a0280c7d1 100644 --- a/spec/requests/activitypub/inboxes_controller_spec.rb +++ b/spec/requests/activitypub/inboxes_controller_spec.rb @@ -178,10 +178,10 @@ RSpec.describe ActivityPub::InboxesController, :sidekiq_inline do end.to(not_change { Account.count }) end - it 'does not update the remote actors username' do + it 'updates the remote actors username' do post "/users/#{local_actor.username}/inbox", params: json.to_json, headers: headers expect(response).to have_http_status(202) - expect(remote_actor.reload.username).to eq(remote_actor_original_username) + expect(remote_actor.reload.username).to eq(remote_actor_new_username) end end end