FIX: Make word watcher work with nil strings (#17830)
Censoring or replacing nil strings raised an error.
This commit is contained in:
parent
9645cbea26
commit
d5dc4ca0e9
|
@ -124,14 +124,16 @@ class WordWatcher
|
|||
end
|
||||
|
||||
def self.censor_text(text)
|
||||
return text if text.blank?
|
||||
|
||||
regexps = word_matcher_regexp_list(:censor)
|
||||
return text if regexps.blank?
|
||||
|
||||
regexps.inject(text) { |txt, regexp| censor_text_with_regexp(txt, regexp) }
|
||||
end
|
||||
|
||||
def self.apply_to_text(text)
|
||||
text = censor_text(text)
|
||||
def self.replace_text(text)
|
||||
return text if text.blank?
|
||||
|
||||
%i[replace link]
|
||||
.flat_map { |type| word_matcher_regexps(type).to_a }
|
||||
|
@ -141,6 +143,12 @@ class WordWatcher
|
|||
end
|
||||
end
|
||||
|
||||
def self.apply_to_text(text)
|
||||
text = censor_text(text)
|
||||
text = replace_text(text)
|
||||
text
|
||||
end
|
||||
|
||||
def self.clear_cache!
|
||||
WatchedWord.actions.each do |a, i|
|
||||
Discourse.cache.delete word_matcher_regexp_key(a)
|
||||
|
|
|
@ -148,13 +148,12 @@ RSpec.describe User do
|
|||
|
||||
describe "#user_fields" do
|
||||
fab!(:user_field) { Fabricate(:user_field, show_on_profile: true) }
|
||||
let(:user_field_value) { user.reload.user_fields[user_field.id.to_s] }
|
||||
fab!(:watched_word) { Fabricate(:watched_word, word: "bad") }
|
||||
|
||||
before { user.set_user_field(user_field.id, value) }
|
||||
|
||||
context "when user fields contain watched words" do
|
||||
let(:user_field_value) { user.reload.user_fields[user_field.id.to_s] }
|
||||
|
||||
context "when watched words are of type 'Block'" do
|
||||
let(:value) { "bad user field value" }
|
||||
|
||||
|
@ -226,7 +225,6 @@ RSpec.describe User do
|
|||
|
||||
context "when user fields contain URL" do
|
||||
let(:value) { "https://discourse.org" }
|
||||
let(:user_field_value) { user.reload.user_fields[user_field.id.to_s] }
|
||||
|
||||
it "is not cooked" do
|
||||
user.save!
|
||||
|
@ -268,6 +266,16 @@ RSpec.describe User do
|
|||
end
|
||||
|
||||
end
|
||||
|
||||
context "when reseting user fields" do
|
||||
let!(:censored_word) { Fabricate(:watched_word, word: "censored", action: WatchedWord.actions[:censor]) }
|
||||
let(:value) { nil }
|
||||
|
||||
it "works" do
|
||||
user.save!
|
||||
expect(user_field_value).to eq nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in New Issue