diff --git a/assets/javascripts/initializers/discourse-perspective.js b/assets/javascripts/initializers/discourse-perspective.js index 9f3011f..bab8f4f 100644 --- a/assets/javascripts/initializers/discourse-perspective.js +++ b/assets/javascripts/initializers/discourse-perspective.js @@ -7,76 +7,58 @@ function initialize(api) { api.modifyClass("controller:composer", { pluginId: "discourse-perspective-api", - _perspective_checked: null, dialog: service(), siteSettings: service(), perspectiveSave(force) { - this.set("_perspective_checked", true); const result = this.save(force); // it's valid for save() to return null since we do that in core, // handle that here because sometimes we return a promise if (result != null && typeof result.then === "function") { - result.finally(() => { - this.set("disableSubmit", false); - this.set("_perspective_checked", false); - }); + result.finally(() => this.set("disableSubmit", false)); } else { this.set("disableSubmit", false); - this.set("_perspective_checked", false); } }, save(force) { - if (!this.siteSettings.perspective_enabled) { - return this._super(...arguments); - } - - // same validation code from controller - if (this.disableSubmit && !this._perspective_checked) { + if (this.disableSubmit) { return; } - if (!this.showWarning) { - this.set("model.isWarning", false); - } - const composer = this.model; - if (composer.cantSubmitPost) { - this.set("lastValidatedAt", Date.now()); - return; - } else { - // disable composer submit during perspective validation - this.set("disableSubmit", true); - } + const result = this._super(force); - const bypassPM = - !this.siteSettings.perspective_check_private_message && - this.get("topic.isPrivateMessage"); - const bypassSecuredCategories = - !this.siteSettings.perspective_check_secured_categories && - this.get("model.category.read_restricted"); - const bypassCheck = bypassPM || bypassSecuredCategories; + const perspectiveEnabled = this.siteSettings.perspective_enabled; + const perspectiveNotifyUser = + this.siteSettings.perspective_notify_posting_min_toxicity_enable; - if (!bypassCheck && !this._perspective_checked) { - return this.perspectiveCheckToxicity(composer, force); - } else { - this.set("disableSubmit", false); - return this._super(force); - } - }, + if (perspectiveEnabled && perspectiveNotifyUser) { + const isPM = this.get("topic.isPrivateMessage"); + const checkPM = this.siteSettings.perspective_check_private_message; - perspectiveCheckToxicity(composer, force) { - let concat = ""; + const isSecureCategory = this.get("model.category.read_restricted"); + const checkSecureCategories = + this.siteSettings.perspective_check_secured_categories; - ["title", "raw", "reply"].forEach((item) => { - const content = composer.get(item); - if (content) { - concat += `${content} `; + const check = + !isPM || checkPM || !isSecureCategory || checkSecureCategories; + + if (check) { + this.set("disableSubmit", true); + return this.perspectiveCheckToxicity(this.model, force); } - }); + } + + return result; + }, - concat.trim(); + perspectiveCheckToxicity(composer, force) { + const concat = ["title", "raw", "reply"] + .map((item) => composer.get(item)) + .filter(Boolean) + .join(" ") + .trim(); return ajax("/perspective/post_toxicity", { type: "POST", @@ -85,8 +67,11 @@ function initialize(api) { .then((response) => { if (response && response["score"] !== undefined) { this.dialog.confirm({ + message: I18n.t("perspective.perspective_message"), confirmButtonLabel: "perspective.composer_edit", confirmButtonClass: "btn-primary perspective-edit-post", + cancelButtonLabel: "perspective.composer_continue", + cancelButtonClass: "perspective-continue-post", didConfirm: () => { if (this.isDestroying || this.isDestroyed) { return; @@ -94,13 +79,8 @@ function initialize(api) { this.set("disableSubmit", false); }, - message: I18n.t("perspective.perspective_message"), - cancelButtonLabel: "perspective.composer_continue", - cancelButtonClass: "perspective-continue-post", didCancel: () => this.perspectiveSave(force), }); - - return; } else { this.perspectiveSave(force); }