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:
parent
e3f8e9c0fb
commit
277496b6e0
|
@ -5,5 +5,5 @@ export function createWatchedWordRegExp(word) {
|
||||||
|
|
||||||
export function toWatchedWord(regexp) {
|
export function toWatchedWord(regexp) {
|
||||||
const [[regexpString, options]] = Object.entries(regexp);
|
const [[regexpString, options]] = Object.entries(regexp);
|
||||||
return { regexp: regexpString, ...options };
|
return { ...options, regexp: regexpString };
|
||||||
}
|
}
|
||||||
|
|
|
@ -61,7 +61,7 @@ export function setup(helper) {
|
||||||
const word = toWatchedWord({ [regexpString]: options });
|
const word = toWatchedWord({ [regexpString]: options });
|
||||||
|
|
||||||
matchers.push({
|
matchers.push({
|
||||||
word: new RegExp(options.word, options.case_sensitive ? "" : "i"),
|
word: new RegExp(options.regexp, options.case_sensitive ? "" : "i"),
|
||||||
pattern: createWatchedWordRegExp(word),
|
pattern: createWatchedWordRegExp(word),
|
||||||
replacement: options.replacement,
|
replacement: options.replacement,
|
||||||
link: false,
|
link: false,
|
||||||
|
@ -76,7 +76,7 @@ export function setup(helper) {
|
||||||
const word = toWatchedWord({ [regexpString]: options });
|
const word = toWatchedWord({ [regexpString]: options });
|
||||||
|
|
||||||
matchers.push({
|
matchers.push({
|
||||||
word: new RegExp(options.word, options.case_sensitive ? "" : "i"),
|
word: new RegExp(options.regexp, options.case_sensitive ? "" : "i"),
|
||||||
pattern: createWatchedWordRegExp(word),
|
pattern: createWatchedWordRegExp(word),
|
||||||
replacement: options.replacement,
|
replacement: options.replacement,
|
||||||
link: true,
|
link: true,
|
||||||
|
|
|
@ -32,7 +32,12 @@ class WordWatcher
|
||||||
.limit(WatchedWord::MAX_WORDS_PER_ACTION)
|
.limit(WatchedWord::MAX_WORDS_PER_ACTION)
|
||||||
.order(:id)
|
.order(:id)
|
||||||
.pluck(:word, :replacement, :case_sensitive)
|
.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
|
end
|
||||||
|
|
||||||
def self.words_for_action_exist?(action)
|
def self.words_for_action_exist?(action)
|
||||||
|
@ -50,8 +55,8 @@ class WordWatcher
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.regexps_for_action(action, engine: :ruby)
|
def self.regexps_for_action(action, engine: :ruby)
|
||||||
cached_words_for_action(action)&.to_h do |word, attrs|
|
cached_words_for_action(action)&.to_h do |_, attrs|
|
||||||
[word_to_regexp(word, engine: engine), attrs]
|
[word_to_regexp(attrs[:word], engine: engine), attrs]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -191,8 +191,8 @@ class TopicCreator
|
||||||
if watched_words.present?
|
if watched_words.present?
|
||||||
word_watcher = WordWatcher.new("#{@opts[:title]} #{@opts[:raw]}")
|
word_watcher = WordWatcher.new("#{@opts[:title]} #{@opts[:raw]}")
|
||||||
word_watcher_tags = topic.tags.map(&:name)
|
word_watcher_tags = topic.tags.map(&:name)
|
||||||
watched_words.each do |word, opts|
|
watched_words.each do |_, opts|
|
||||||
if word_watcher.word_matches?(word, case_sensitive: opts[:case_sensitive])
|
if word_watcher.word_matches?(opts[:word], case_sensitive: opts[:case_sensitive])
|
||||||
word_watcher_tags += opts[:replacement].split(",")
|
word_watcher_tags += opts[:replacement].split(",")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1981,6 +1981,19 @@ HTML
|
||||||
HTML
|
HTML
|
||||||
end
|
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
|
it "replaces words with links" do
|
||||||
Fabricate(
|
Fabricate(
|
||||||
:watched_word,
|
:watched_word,
|
||||||
|
|
Loading…
Reference in New Issue