A platform for community discussion. Free, open, simple.
Improvements to user renaming
Gerhard Schlager74c4af279a75
diff --git a/app/jobs/regular/update_username.rb b/app/jobs/regular/update_username.rbindex d074bdd9299..e8a45d9366c 100644--- a/app/jobs/regular/update_username.rb+++ b/app/jobs/regular/update_username.rb@@ -1,6 +1,8 @@module Jobsclass UpdateUsername < Jobs::Base+ sidekiq_options queue: 'low'+def execute(args)@user_id = args[:user_id]@old_username = args[:old_username]@@ -16,26 +18,35 @@ module Jobsupdate_postsupdate_revisionsupdate_notifications+ update_post_custom_fieldsenddef update_postsPost.with_deleted.where(post_conditions("posts.id"), post_condition_args).find_each do |post|- post.raw = update_raw(post.raw)- post.cooked = update_cooked(post.cooked)+ begin+ post.raw = update_raw(post.raw)+ post.cooked = update_cooked(post.cooked)- # update without running validations and hooks- post.update_columns(raw: post.raw, cooked: post.cooked)+ # update without running validations and hooks+ post.update_columns(raw: post.raw, cooked: post.cooked)- SearchIndexer.index(post, force: true)+ SearchIndexer.index(post, force: true) if post.topic+ rescue => e+ Discourse.warn_exception(e, message: "Failed to update post with id #{post.id}")+ endendenddef update_revisionsPostRevision.where(post_conditions("post_revisions.post_id"), post_condition_args).find_each do |revision|- if revision.modifications.key?("raw") || revision.modifications.key?("cooked")- revision.modifications["raw"]&.map! { |raw| update_raw(raw) }- revision.modifications["cooked"]&.map! { |cooked| update_cooked(cooked) }- revision.save!+ begin+ if revision.modifications.key?("raw") || revision.modifications.key?("cooked")+ revision.modifications["raw"]&.map! { |raw| update_raw(raw) }+ revision.modifications["cooked"]&.map! { |cooked| update_cooked(cooked) }+ revision.save!+ end+ rescue => e+ Discourse.warn_exception(e, message: "Failed to update post revision with id #{revision.id}")endendend@@ -44,16 +55,11 @@ module Jobsparams = {user_id: @user_id,old_username: @old_username,- new_username: @new_username,- notification_types_with_correct_user_id: [- Notification.types[:granted_badge],- Notification.types[:group_message_summary]- ],- invitee_accepted_notification_type: Notification.types[:invitee_accepted]+ new_username: @new_username}Notification.exec_sql(<<~SQL, params)- UPDATE notifications AS n+ UPDATE notificationsSET data = (data :: JSONB ||jsonb_strip_nulls(jsonb_build_object(@@ -66,25 +72,24 @@ module JobsTHEN :new_usernameELSE NULL END,'username', CASE data :: JSONB ->> 'username'+ WHEN :old_username+ THEN :new_username+ ELSE NULL END,+ 'username2', CASE data :: JSONB ->> 'username2'WHEN :old_usernameTHEN :new_usernameELSE NULL END))) :: JSON- WHERE EXISTS(- SELECT 1- FROM posts AS p- WHERE p.topic_id = n.topic_id- AND p.post_number = n.post_number- AND p.user_id = :user_id)- OR (n.notification_type IN (:notification_types_with_correct_user_id) AND n.user_id = :user_id)- OR (n.notification_type = :invitee_accepted_notification_type- AND EXISTS(- SELECT 1- FROM invites i- WHERE i.user_id = :user_id AND n.user_id = i.invited_by_id- )- )+ WHERE data ILIKE '%' || :old_username || '%'+ SQL+ end++ def update_post_custom_fields+ PostCustomField.exec_sql(<<~SQL, old_username: @old_username, new_username: @new_username)+ UPDATE post_custom_fields+ SET value = :new_username+ WHERE name = 'action_code_who' AND value = :old_usernameSQLend@@ -125,7 +130,7 @@ module Jobsdoc.css("a.mention").each do |a|a.content = a.content.gsub(@cooked_mention_username_regex, "@#{@new_username}")- a["href"] = a["href"].gsub(@cooked_mention_user_path_regex, "/u/#{@new_username}")+ a["href"] = a["href"].gsub(@cooked_mention_user_path_regex, "/u/#{@new_username}") if a["href"]enddoc.css("aside.quote > div.title").each do |div|diff --git a/spec/services/username_changer_spec.rb b/spec/services/username_changer_spec.rbindex c02ea460aff..01d788bc683 100644--- a/spec/services/username_changer_spec.rb+++ b/spec/services/username_changer_spec.rb@@ -95,8 +95,14 @@ describe UsernameChanger dolet(:user) { Fabricate(:user, username: 'foo') }let(:topic) { Fabricate(:topic, user: user) }- before { UserActionCreator.enable }- after { UserActionCreator.disable }+ before do+ UserActionCreator.enable+ Discourse.expects(:warn_exception).never+ end++ after do+ UserActionCreator.disable+ enddef create_post_and_change_username(args = {}, &block)post = create_post(args.merge(topic_id: topic.id))@@ -256,6 +262,13 @@ describe UsernameChanger doexpect(post.revisions[0].modifications["raw"][0]).to eq("Hello @bar")expect(post.revisions[0].modifications["cooked"][0]).to eq(%Q(<p>Hello <a class="mention" href="/u/bar">@bar</a></p>))end++ it 'works when users are mentioned with HTML' do+ post = create_post_and_change_username(raw: '<a class="mention">@foo</a> and <a class="mention">@someuser</a>')++ expect(post.raw).to eq('<a class="mention">@bar</a> and <a class="mention">@someuser</a>')+ expect(post.cooked).to match_html('<p><a class="mention">@bar</a> and <a class="mention">@someuser</a></p>')+ endendcontext 'quotes' do@@ -427,6 +440,16 @@ describe UsernameChanger doHTMLendend++ it 'updates username in small action posts' do+ invited_by = Fabricate(:user)+ p1 = topic.add_small_action(invited_by, 'invited_user', 'foo')+ p2 = topic.add_small_action(invited_by, 'invited_user', 'foobar')+ UsernameChanger.change(user, 'bar')++ expect(p1.reload.custom_fields['action_code_who']).to eq('bar')+ expect(p2.reload.custom_fields['action_code_who']).to eq('foobar')+ endendcontext 'notifications' do