2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-04-30 19:54:36 -04:00
|
|
|
module Jobs
|
2019-10-02 00:01:53 -04:00
|
|
|
class UpdateUsername < ::Jobs::Base
|
2018-04-30 19:54:36 -04:00
|
|
|
|
2018-05-15 15:05:51 -04:00
|
|
|
sidekiq_options queue: 'low'
|
|
|
|
|
2018-04-30 19:54:36 -04:00
|
|
|
def execute(args)
|
|
|
|
@user_id = args[:user_id]
|
2019-04-23 06:22:47 -04:00
|
|
|
@old_username = args[:old_username].unicode_normalize
|
|
|
|
@new_username = args[:new_username].unicode_normalize
|
2018-05-14 11:13:25 -04:00
|
|
|
@avatar_img = PrettyText.avatar_img(args[:avatar_template], "tiny")
|
2018-04-30 19:54:36 -04:00
|
|
|
|
2019-04-23 06:22:47 -04:00
|
|
|
@raw_mention_regex = /
|
|
|
|
(?:
|
|
|
|
(?<![\p{Alnum}\p{M}`]) # make sure there is no preceding letter, number or backtick
|
|
|
|
)
|
|
|
|
@#{@old_username}
|
|
|
|
(?:
|
|
|
|
(?![\p{Alnum}\p{M}_\-.`]) # make sure there is no trailing letter, number, underscore, dash, dot or backtick
|
|
|
|
| # or
|
|
|
|
(?=[-_.](?:\s|$)) # there is an underscore, dash or dot followed by a whitespace or end of line
|
|
|
|
)
|
|
|
|
/ix
|
|
|
|
|
2018-05-08 10:02:43 -04:00
|
|
|
@raw_quote_regex = /(\[quote\s*=\s*["'']?)#{@old_username}(\,?[^\]]*\])/i
|
2018-05-25 17:28:47 -04:00
|
|
|
|
|
|
|
cooked_username = PrettyText::Helpers.format_username(@old_username)
|
|
|
|
@cooked_mention_username_regex = /^@#{cooked_username}$/i
|
2019-12-11 21:49:21 -05:00
|
|
|
@cooked_mention_user_path_regex = /^\/u(?:sers)?\/#{UrlHelper.encode_component(cooked_username)}$/i
|
2018-05-25 17:28:47 -04:00
|
|
|
@cooked_quote_username_regex = /(?<=\s)#{cooked_username}(?=:)/i
|
2018-05-08 10:02:43 -04:00
|
|
|
|
2018-04-30 19:54:36 -04:00
|
|
|
update_posts
|
|
|
|
update_revisions
|
2018-05-08 10:02:43 -04:00
|
|
|
update_notifications
|
2018-05-15 15:05:51 -04:00
|
|
|
update_post_custom_fields
|
2020-03-30 12:02:34 -04:00
|
|
|
|
|
|
|
DiscourseEvent.trigger(:username_changed, @old_username, @new_username)
|
2020-07-02 14:44:19 -04:00
|
|
|
DiscourseEvent.trigger(:user_updated, User.find_by(id: @user_id))
|
2018-04-30 19:54:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_posts
|
2018-07-23 18:36:20 -04:00
|
|
|
updated_post_ids = Set.new
|
|
|
|
|
|
|
|
Post.with_deleted
|
|
|
|
.joins(mentioned("posts.id"))
|
|
|
|
.where("a.user_id = :user_id", user_id: @user_id)
|
|
|
|
.find_each do |post|
|
|
|
|
|
|
|
|
update_post(post)
|
|
|
|
updated_post_ids << post.id
|
2018-04-30 19:54:36 -04:00
|
|
|
end
|
2018-07-23 18:36:20 -04:00
|
|
|
|
|
|
|
Post.with_deleted
|
|
|
|
.joins(quoted("posts.id"))
|
|
|
|
.where("p.user_id = :user_id", user_id: @user_id)
|
|
|
|
.find_each { |post| update_post(post) unless updated_post_ids.include?(post.id) }
|
2018-04-30 19:54:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_revisions
|
2018-07-23 18:36:20 -04:00
|
|
|
PostRevision.joins(mentioned("post_revisions.post_id"))
|
|
|
|
.where("a.user_id = :user_id", user_id: @user_id)
|
|
|
|
.find_each { |revision| update_revision(revision) }
|
|
|
|
|
|
|
|
PostRevision.joins(quoted("post_revisions.post_id"))
|
|
|
|
.where("p.user_id = :user_id", user_id: @user_id)
|
|
|
|
.find_each { |revision| update_revision(revision) }
|
2018-04-30 19:54:36 -04:00
|
|
|
end
|
|
|
|
|
2018-05-08 10:02:43 -04:00
|
|
|
def update_notifications
|
|
|
|
params = {
|
|
|
|
user_id: @user_id,
|
|
|
|
old_username: @old_username,
|
2018-05-15 15:05:51 -04:00
|
|
|
new_username: @new_username
|
2018-05-08 10:02:43 -04:00
|
|
|
}
|
|
|
|
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec(<<~SQL, params)
|
2018-05-15 15:05:51 -04:00
|
|
|
UPDATE notifications
|
2018-05-08 10:02:43 -04:00
|
|
|
SET data = (data :: JSONB ||
|
|
|
|
jsonb_strip_nulls(
|
|
|
|
jsonb_build_object(
|
|
|
|
'original_username', CASE data :: JSONB ->> 'original_username'
|
|
|
|
WHEN :old_username
|
|
|
|
THEN :new_username
|
|
|
|
ELSE NULL END,
|
|
|
|
'display_username', CASE data :: JSONB ->> 'display_username'
|
|
|
|
WHEN :old_username
|
|
|
|
THEN :new_username
|
|
|
|
ELSE NULL END,
|
|
|
|
'username', CASE data :: JSONB ->> 'username'
|
2018-05-15 15:05:51 -04:00
|
|
|
WHEN :old_username
|
|
|
|
THEN :new_username
|
|
|
|
ELSE NULL END,
|
|
|
|
'username2', CASE data :: JSONB ->> 'username2'
|
2018-05-08 10:02:43 -04:00
|
|
|
WHEN :old_username
|
|
|
|
THEN :new_username
|
|
|
|
ELSE NULL END
|
|
|
|
)
|
|
|
|
)) :: JSON
|
2018-05-15 15:05:51 -04:00
|
|
|
WHERE data ILIKE '%' || :old_username || '%'
|
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_post_custom_fields
|
2018-06-19 02:13:14 -04:00
|
|
|
DB.exec(<<~SQL, old_username: @old_username, new_username: @new_username)
|
2018-05-15 15:05:51 -04:00
|
|
|
UPDATE post_custom_fields
|
|
|
|
SET value = :new_username
|
|
|
|
WHERE name = 'action_code_who' AND value = :old_username
|
2018-05-08 10:02:43 -04:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2018-07-23 18:36:20 -04:00
|
|
|
protected
|
|
|
|
|
|
|
|
def update_post(post)
|
|
|
|
post.raw = update_raw(post.raw)
|
|
|
|
post.cooked = update_cooked(post.cooked)
|
|
|
|
|
|
|
|
post.update_columns(raw: post.raw, cooked: post.cooked)
|
|
|
|
|
|
|
|
SearchIndexer.index(post, force: true) if post.topic
|
|
|
|
rescue => e
|
|
|
|
Discourse.warn_exception(e, message: "Failed to update post with id #{post.id}")
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_revision(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!
|
|
|
|
end
|
|
|
|
rescue => e
|
|
|
|
Discourse.warn_exception(e, message: "Failed to update post revision with id #{revision.id}")
|
|
|
|
end
|
2018-04-30 19:54:36 -04:00
|
|
|
|
2018-07-23 18:36:20 -04:00
|
|
|
def mentioned(post_id_column)
|
2018-04-30 19:54:36 -04:00
|
|
|
<<~SQL
|
2018-07-23 18:36:20 -04:00
|
|
|
JOIN user_actions AS a ON (a.target_post_id = #{post_id_column} AND
|
|
|
|
a.action_type = #{UserAction::MENTION})
|
2018-04-30 19:54:36 -04:00
|
|
|
SQL
|
|
|
|
end
|
|
|
|
|
2018-07-23 18:36:20 -04:00
|
|
|
def quoted(post_id_column)
|
|
|
|
<<~SQL
|
|
|
|
JOIN quoted_posts AS q ON (q.post_id = #{post_id_column})
|
|
|
|
JOIN posts AS p ON (q.quoted_post_id = p.id)
|
|
|
|
SQL
|
2018-04-30 19:54:36 -04:00
|
|
|
end
|
|
|
|
|
2018-05-14 07:15:31 -04:00
|
|
|
def update_raw(raw)
|
|
|
|
raw.gsub(@raw_mention_regex, "@#{@new_username}")
|
|
|
|
.gsub(@raw_quote_regex, "\\1#{@new_username}\\2")
|
2018-04-30 19:54:36 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Uses Nokogiri instead of rebake, because it works for posts and revisions
|
|
|
|
# and there is no reason to invalidate oneboxes, run the post analyzer etc.
|
|
|
|
# when only the username changes.
|
|
|
|
def update_cooked(cooked)
|
2020-05-04 23:46:57 -04:00
|
|
|
doc = Nokogiri::HTML5.fragment(cooked)
|
2018-04-30 19:54:36 -04:00
|
|
|
|
|
|
|
doc.css("a.mention").each do |a|
|
|
|
|
a.content = a.content.gsub(@cooked_mention_username_regex, "@#{@new_username}")
|
2020-05-07 21:14:59 -04:00
|
|
|
a["href"] = a["href"].gsub(@cooked_mention_user_path_regex, "/u/#{UrlHelper.encode_component(@new_username)}") if a["href"]
|
2018-04-30 19:54:36 -04:00
|
|
|
end
|
|
|
|
|
2018-05-22 07:41:32 -04:00
|
|
|
doc.css("aside.quote").each do |aside|
|
|
|
|
next unless div = aside.at_css("div.title")
|
|
|
|
|
|
|
|
username_replaced = false
|
|
|
|
|
2020-01-22 09:10:23 -05:00
|
|
|
aside["data-username"] = @new_username if aside["data-username"] == @old_username
|
|
|
|
|
2018-04-30 19:54:36 -04:00
|
|
|
div.children.each do |child|
|
2018-05-22 07:41:32 -04:00
|
|
|
if child.text?
|
|
|
|
content = child.content
|
|
|
|
username_replaced = content.gsub!(@cooked_quote_username_regex, @new_username).present?
|
|
|
|
child.content = content if username_replaced
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if username_replaced || quotes_correct_user?(aside)
|
|
|
|
div.at_css("img.avatar")&.replace(@avatar_img)
|
2018-04-30 19:54:36 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
doc.to_html
|
|
|
|
end
|
2018-05-22 07:41:32 -04:00
|
|
|
|
|
|
|
def quotes_correct_user?(aside)
|
|
|
|
Post.where(
|
|
|
|
topic_id: aside["data-topic"],
|
|
|
|
post_number: aside["data-post"]
|
2019-10-21 06:32:27 -04:00
|
|
|
).pluck_first(:user_id) == @user_id
|
2018-05-22 07:41:32 -04:00
|
|
|
end
|
2018-04-30 19:54:36 -04:00
|
|
|
end
|
|
|
|
end
|