2023-06-14 04:14:11 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class QuoteRewriter
|
2023-06-25 23:01:59 -04:00
|
|
|
def initialize(user_id)
|
2023-06-14 04:14:11 -04:00
|
|
|
@user_id = user_id
|
|
|
|
end
|
|
|
|
|
2023-06-25 23:01:59 -04:00
|
|
|
def rewrite_raw_username(raw, old_username, new_username)
|
2023-10-16 08:06:02 -04:00
|
|
|
escaped_old_username = Regexp.escape(old_username)
|
2023-06-14 04:14:11 -04:00
|
|
|
pattern =
|
|
|
|
Regexp.union(
|
2023-10-16 08:06:02 -04:00
|
|
|
/(?<pre>\[quote\s*=\s*["'']?.*username:)#{escaped_old_username}(?<post>\,?[^\]]*\])/i,
|
|
|
|
/(?<pre>\[quote\s*=\s*["'']?)#{escaped_old_username}(?<post>\,?[^\]]*\])/i,
|
2023-06-14 04:14:11 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
raw.gsub(pattern, "\\k<pre>#{new_username}\\k<post>")
|
|
|
|
end
|
|
|
|
|
2023-06-25 23:01:59 -04:00
|
|
|
def rewrite_cooked_username(cooked, old_username, new_username, avatar_img)
|
2023-10-16 08:06:02 -04:00
|
|
|
formatted_old_username = PrettyText::Helpers.format_username(old_username)
|
|
|
|
escaped_old_username = Regexp.escape(formatted_old_username)
|
|
|
|
pattern = /(?<=\s)#{escaped_old_username}(?=:)/i
|
2023-06-14 04:14:11 -04:00
|
|
|
|
|
|
|
cooked
|
|
|
|
.css("aside.quote")
|
|
|
|
.each do |aside|
|
|
|
|
next unless div = aside.at_css("div.title")
|
|
|
|
|
|
|
|
username_replaced = false
|
|
|
|
|
|
|
|
aside["data-username"] = new_username if aside["data-username"] == old_username
|
|
|
|
|
|
|
|
div.children.each do |child|
|
|
|
|
if child.text?
|
|
|
|
content = child.content
|
|
|
|
username_replaced = content.gsub!(pattern, 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)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-25 23:01:59 -04:00
|
|
|
def rewrite_raw_display_name(raw, old_display_name, new_display_name)
|
2023-10-16 08:06:02 -04:00
|
|
|
escaped_old_display_name = Regexp.escape(old_display_name)
|
|
|
|
pattern =
|
|
|
|
/(?<pre>\[quote\s*=\s*["'']?)#{escaped_old_display_name}(?<post>\,[^\]]*username[^\]]*\])/i
|
2023-06-25 23:01:59 -04:00
|
|
|
|
|
|
|
raw.gsub(pattern, "\\k<pre>#{new_display_name}\\k<post>")
|
|
|
|
end
|
|
|
|
|
|
|
|
def rewrite_cooked_display_name(cooked, old_display_name, new_display_name)
|
2023-10-16 08:06:02 -04:00
|
|
|
formatted_old_display_name = PrettyText::Helpers.format_username(old_display_name)
|
|
|
|
escaped_old_display_name = Regexp.escape(formatted_old_display_name)
|
|
|
|
pattern = /(?<=\s)#{escaped_old_display_name}(?=:)/i
|
2023-06-25 23:01:59 -04:00
|
|
|
|
|
|
|
cooked
|
|
|
|
.css("aside.quote")
|
|
|
|
.each do |aside|
|
|
|
|
next unless div = aside.at_css("div.title")
|
|
|
|
|
|
|
|
div.children.each do |child|
|
|
|
|
if child.text?
|
|
|
|
content = child.content
|
|
|
|
display_name_replaced = content.gsub!(pattern, new_display_name).present?
|
|
|
|
child.content = content if display_name_replaced
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-06-14 04:14:11 -04:00
|
|
|
private
|
|
|
|
|
2023-06-25 23:01:59 -04:00
|
|
|
attr_reader :user_id
|
2023-06-14 04:14:11 -04:00
|
|
|
|
|
|
|
def quotes_correct_user?(aside)
|
|
|
|
Post.exists?(topic_id: aside["data-topic"], post_number: aside["data-post"], user_id: user_id)
|
|
|
|
end
|
|
|
|
end
|