This repository has been archived on 2023-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
osr-discourse-src/app/assets/javascripts/admin/addon/components/watched-word-form.js
Loïc Guitaut 5c37a5d0f2
FIX: Allow to add the same watched word with a different case (#17799)
Currently we can’t add a case-sensitive watched word if another one
exists with a different case. For example, the existing watched word
`Meta` has been created and is case-sensitive. Now an admin tries to add
`metA` while marking it as case-sensitive too, this won’t work and the
word won’t be added.

This patch changes this behavior by allowing to add same words that have
different cases, so the example above will now work as expected.

We still check for uniqueness but case-sensitivy is now taken
into account. It means that if the watched word `meta` already exists
and is not case-sensitive then it will not be possible to add `Meta`
(case-sensitive or not) as `meta` already matches every possible
variations of this word.
2022-08-05 12:18:17 +02:00

122 lines
3.4 KiB
JavaScript

import discourseComputed, { observes } from "discourse-common/utils/decorators";
import Component from "@ember/component";
import I18n from "I18n";
import WatchedWord from "admin/models/watched-word";
import bootbox from "bootbox";
import { equal } from "@ember/object/computed";
import { isEmpty } from "@ember/utils";
import { schedule } from "@ember/runloop";
export default Component.extend({
tagName: "form",
classNames: ["watched-word-form"],
formSubmitted: false,
actionKey: null,
showMessage: false,
selectedTags: null,
isCaseSensitive: false,
canReplace: equal("actionKey", "replace"),
canTag: equal("actionKey", "tag"),
canLink: equal("actionKey", "link"),
didInsertElement() {
this._super(...arguments);
this.set("selectedTags", []);
},
@discourseComputed("siteSettings.watched_words_regular_expressions")
placeholderKey(watchedWordsRegularExpressions) {
if (watchedWordsRegularExpressions) {
return "admin.watched_words.form.placeholder_regexp";
} else {
return "admin.watched_words.form.placeholder";
}
},
@observes("word")
removeMessage() {
if (this.showMessage && !isEmpty(this.word)) {
this.set("showMessage", false);
}
},
@discourseComputed("word")
isUniqueWord(word) {
const words = this.filteredContent || [];
const filtered = words.filter(
(content) => content.action === this.actionKey
);
return filtered.every((content) => {
if (content.case_sensitive === true) {
return content.word !== word;
}
return content.word.toLowerCase() !== word.toLowerCase();
});
},
actions: {
changeSelectedTags(tags) {
this.setProperties({
selectedTags: tags,
replacement: tags.join(","),
});
},
submit() {
if (!this.isUniqueWord) {
this.setProperties({
showMessage: true,
message: I18n.t("admin.watched_words.form.exists"),
});
return;
}
if (!this.formSubmitted) {
this.set("formSubmitted", true);
const watchedWord = WatchedWord.create({
word: this.word,
replacement:
this.canReplace || this.canTag || this.canLink
? this.replacement
: null,
action: this.actionKey,
isCaseSensitive: this.isCaseSensitive,
});
watchedWord
.save()
.then((result) => {
this.setProperties({
word: "",
replacement: "",
formSubmitted: false,
selectedTags: [],
showMessage: true,
message: I18n.t("admin.watched_words.form.success"),
isCaseSensitive: false,
});
this.action(WatchedWord.create(result));
schedule("afterRender", () =>
this.element.querySelector("input").focus()
);
})
.catch((e) => {
this.set("formSubmitted", false);
const msg = e.jqXHR.responseJSON?.errors
? I18n.t("generic_error_with_reason", {
error: e.jqXHR.responseJSON.errors.join(". "),
})
: I18n.t("generic_error");
bootbox.alert(msg, () =>
schedule("afterRender", () =>
this.element.querySelector("input").focus()
)
);
});
}
},
},
});