diff --git a/app/services/user_anonymizer.rb b/app/services/user_anonymizer.rb index f78c9e6497d..35e77ea88d0 100644 --- a/app/services/user_anonymizer.rb +++ b/app/services/user_anonymizer.rb @@ -20,8 +20,9 @@ class UserAnonymizer @prev_email = @user.email @prev_username = @user.username - @user.update_attribute(:uploaded_avatar_id, nil) - raise "Failed to change username" unless UsernameChanger.change(@user, make_anon_username) + unless UsernameChanger.new(@user, make_anon_username).change(run_update_job: false) + raise "Failed to change username" + end @user.reload @user.password = SecureRandom.hex @@ -29,6 +30,7 @@ class UserAnonymizer @user.name = SiteSetting.full_name_required ? @user.username : nil @user.date_of_birth = nil @user.title = nil + @user.uploaded_avatar_id = nil if @opts.has_key?(:anonymize_ip) @user.ip_address = @opts[:anonymize_ip] @@ -63,6 +65,11 @@ class UserAnonymizer @user_history = log_action end + UsernameChanger.update_username(user_id: @user.id, + old_username: @prev_username, + new_username: @user.username, + avatar_template: @user.avatar_template) + Jobs.enqueue(:anonymize_user, user_id: @user.id, prev_email: @prev_email, diff --git a/app/services/user_merger.rb b/app/services/user_merger.rb index c8e8cc530ec..5f28717dede 100644 --- a/app/services/user_merger.rb +++ b/app/services/user_merger.rb @@ -24,10 +24,11 @@ class UserMerger protected def update_username - Jobs::UpdateUsername.new.execute(user_id: @source_user.id, - old_username: @source_user.username, - new_username: @target_user.username, - avatar_template: @target_user.avatar_template) + UsernameChanger.update_username(user_id: @source_user.id, + old_username: @source_user.username, + new_username: @target_user.username, + avatar_template: @target_user.avatar_template, + asynchronous: false) end def move_posts diff --git a/app/services/username_changer.rb b/app/services/username_changer.rb index e5df339c12e..70ce9caabb5 100644 --- a/app/services/username_changer.rb +++ b/app/services/username_changer.rb @@ -13,31 +13,37 @@ class UsernameChanger self.new(user, new_username, actor).change end - def change(asynchronous: true) + def change(asynchronous: true, run_update_job: true) if @actor && @old_username != @new_username StaffActionLogger.new(@actor).log_username_change(@user, @old_username, @new_username) end @user.username = @new_username + if @user.save - - args = { - user_id: @user.id, - old_username: @old_username, - new_username: @new_username, - avatar_template: @user.avatar_template - } - - if asynchronous - Jobs.enqueue(:update_username, args) - else - Jobs::UpdateUsername.new.execute(args) - end - + UsernameChanger.update_username(user_id: @user.id, + old_username: @old_username, + new_username: @new_username, + avatar_template: @user.avatar_template, + asynchronous: asynchronous) if run_update_job return true end false end + def self.update_username(user_id:, old_username:, new_username:, avatar_template:, asynchronous: true) + args = { + user_id: user_id, + old_username: old_username, + new_username: new_username, + avatar_template: avatar_template + } + + if asynchronous + Jobs.enqueue(:update_username, args) + else + Jobs::UpdateUsername.new.execute(args) + end + end end diff --git a/lib/tasks/users.rake b/lib/tasks/users.rake index 877d270f68e..83c3cf28c03 100644 --- a/lib/tasks/users.rake +++ b/lib/tasks/users.rake @@ -73,11 +73,7 @@ task "users:update_posts", [:old_username, :current_username] => [:environment] end user = find_user(current_username) - Jobs::UpdateUsername.new.execute( - user_id: user.id, - old_username: old_username, - new_username: user.username, - avatar_template: user.avatar_template) + UsernameChanger.update_username(user.id, old_username, user.username, user.avatar_template) puts "", "Username updated!", "" end