FIX: Replace watched words with wildcards (#24279)

These have been broken since fd07c943ad
because watched words were not correctly transformed to regexps.
This partially reverts the changes.
This commit is contained in:
Bianca Nenciu 2023-11-08 18:51:11 +02:00 committed by GitHub
parent e3f8e9c0fb
commit 277496b6e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 26 additions and 8 deletions

View File

@ -5,5 +5,5 @@ export function createWatchedWordRegExp(word) {
export function toWatchedWord(regexp) {
const [[regexpString, options]] = Object.entries(regexp);
return { regexp: regexpString, ...options };
return { ...options, regexp: regexpString };
}

View File

@ -61,7 +61,7 @@ export function setup(helper) {
const word = toWatchedWord({ [regexpString]: options });
matchers.push({
word: new RegExp(options.word, options.case_sensitive ? "" : "i"),
word: new RegExp(options.regexp, options.case_sensitive ? "" : "i"),
pattern: createWatchedWordRegExp(word),
replacement: options.replacement,
link: false,
@ -76,7 +76,7 @@ export function setup(helper) {
const word = toWatchedWord({ [regexpString]: options });
matchers.push({
word: new RegExp(options.word, options.case_sensitive ? "" : "i"),
word: new RegExp(options.regexp, options.case_sensitive ? "" : "i"),
pattern: createWatchedWordRegExp(word),
replacement: options.replacement,
link: true,

View File

@ -32,7 +32,12 @@ class WordWatcher
.limit(WatchedWord::MAX_WORDS_PER_ACTION)
.order(:id)
.pluck(:word, :replacement, :case_sensitive)
.to_h { |w, r, c| [w, { word: w, replacement: r, case_sensitive: c }.compact] }
.to_h do |w, r, c|
[
word_to_regexp(w, match_word: false),
{ word: w, replacement: r, case_sensitive: c }.compact,
]
end
end
def self.words_for_action_exist?(action)
@ -50,8 +55,8 @@ class WordWatcher
end
def self.regexps_for_action(action, engine: :ruby)
cached_words_for_action(action)&.to_h do |word, attrs|
[word_to_regexp(word, engine: engine), attrs]
cached_words_for_action(action)&.to_h do |_, attrs|
[word_to_regexp(attrs[:word], engine: engine), attrs]
end
end

View File

@ -191,8 +191,8 @@ class TopicCreator
if watched_words.present?
word_watcher = WordWatcher.new("#{@opts[:title]} #{@opts[:raw]}")
word_watcher_tags = topic.tags.map(&:name)
watched_words.each do |word, opts|
if word_watcher.word_matches?(word, case_sensitive: opts[:case_sensitive])
watched_words.each do |_, opts|
if word_watcher.word_matches?(opts[:word], case_sensitive: opts[:case_sensitive])
word_watcher_tags += opts[:replacement].split(",")
end
end

View File

@ -1981,6 +1981,19 @@ HTML
HTML
end
it "replaces words with wildcards" do
Fabricate(
:watched_word,
action: WatchedWord.actions[:replace],
word: "*dolor*",
replacement: "something else",
)
expect(PrettyText.cook("Lorem ipsum xdolorx sit amet")).to match_html(<<~HTML)
<p>Lorem ipsum something else sit amet</p>
HTML
end
it "replaces words with links" do
Fabricate(
:watched_word,