Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: ensure it works with form templates #93

Merged
merged 2 commits into from
Nov 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 31 additions & 51 deletions assets/javascripts/initializers/discourse-perspective.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -85,22 +67,20 @@ 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;
}

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);
}
Expand Down