discourse/app/services/username_changer.rb

50 lines
1.4 KiB
Ruby
Raw Normal View History

require_dependency 'jobs/regular/update_username'
class UsernameChanger
2017-07-27 21:20:09 -04:00
def initialize(user, new_username, actor = nil)
@user = user
@old_username = user.username
@new_username = new_username
@actor = actor
end
2017-07-27 21:20:09 -04:00
def self.change(user, new_username, actor = nil)
self.new(user, new_username, actor).change
end
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
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