2022-12-13 08:53:08 +08:00
|
|
|
import Controller, { inject as controller } from "@ember/controller";
|
|
|
|
import EmberObject, { action } from "@ember/object";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { alias } from "@ember/object/computed";
|
2024-03-07 01:05:11 +08:00
|
|
|
import { service } from "@ember/service";
|
2022-12-13 08:53:08 +08:00
|
|
|
import { isEmpty } from "@ember/utils";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2023-10-11 02:38:59 +08:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2024-11-20 04:45:18 +08:00
|
|
|
import { i18n } from "discourse-i18n";
|
2022-12-13 08:53:08 +08:00
|
|
|
|
2023-03-15 17:42:12 +08:00
|
|
|
export default class AdminWebHooksEditController extends Controller {
|
|
|
|
@service dialog;
|
2023-07-19 03:53:23 +08:00
|
|
|
@service router;
|
|
|
|
@service siteSettings;
|
|
|
|
|
2023-03-15 17:42:12 +08:00
|
|
|
@controller adminWebHooks;
|
|
|
|
|
2023-10-09 11:35:31 +08:00
|
|
|
@alias("adminWebHooks.groupedEventTypes") groupedEventTypes;
|
2023-03-15 17:42:12 +08:00
|
|
|
@alias("adminWebHooks.defaultEventTypes") defaultEventTypes;
|
|
|
|
@alias("adminWebHooks.contentTypes") contentTypes;
|
2022-12-13 08:53:08 +08:00
|
|
|
|
|
|
|
@discourseComputed
|
|
|
|
showTagsFilter() {
|
|
|
|
return this.siteSettings.tagging_enabled;
|
2023-03-15 17:42:12 +08:00
|
|
|
}
|
2022-12-13 08:53:08 +08:00
|
|
|
|
|
|
|
@discourseComputed("model.isSaving", "saved", "saveButtonDisabled")
|
|
|
|
savingStatus(isSaving, saved, saveButtonDisabled) {
|
|
|
|
if (isSaving) {
|
2024-11-20 04:45:18 +08:00
|
|
|
return i18n("saving");
|
2022-12-13 08:53:08 +08:00
|
|
|
} else if (!saveButtonDisabled && saved) {
|
2024-11-20 04:45:18 +08:00
|
|
|
return i18n("saved");
|
2022-12-13 08:53:08 +08:00
|
|
|
}
|
|
|
|
// Use side effect of validation to clear saved text
|
|
|
|
this.set("saved", false);
|
|
|
|
return "";
|
2023-03-15 17:42:12 +08:00
|
|
|
}
|
2022-12-13 08:53:08 +08:00
|
|
|
|
|
|
|
@discourseComputed("model.isNew")
|
|
|
|
saveButtonText(isNew) {
|
|
|
|
return isNew
|
2024-11-20 04:45:18 +08:00
|
|
|
? i18n("admin.web_hooks.create")
|
|
|
|
: i18n("admin.web_hooks.save");
|
2023-03-15 17:42:12 +08:00
|
|
|
}
|
2022-12-13 08:53:08 +08:00
|
|
|
|
|
|
|
@discourseComputed("model.secret")
|
|
|
|
secretValidation(secret) {
|
|
|
|
if (!isEmpty(secret)) {
|
|
|
|
if (secret.includes(" ")) {
|
|
|
|
return EmberObject.create({
|
|
|
|
failed: true,
|
2024-11-20 04:45:18 +08:00
|
|
|
reason: i18n("admin.web_hooks.secret_invalid"),
|
2022-12-13 08:53:08 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (secret.length < 12) {
|
|
|
|
return EmberObject.create({
|
|
|
|
failed: true,
|
2024-11-20 04:45:18 +08:00
|
|
|
reason: i18n("admin.web_hooks.secret_too_short"),
|
2022-12-13 08:53:08 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2023-03-15 17:42:12 +08:00
|
|
|
}
|
2022-12-13 08:53:08 +08:00
|
|
|
|
|
|
|
@discourseComputed("model.wildcard_web_hook", "model.web_hook_event_types.[]")
|
|
|
|
eventTypeValidation(isWildcard, eventTypes) {
|
|
|
|
if (!isWildcard && isEmpty(eventTypes)) {
|
|
|
|
return EmberObject.create({
|
|
|
|
failed: true,
|
2024-11-20 04:45:18 +08:00
|
|
|
reason: i18n("admin.web_hooks.event_type_missing"),
|
2022-12-13 08:53:08 +08:00
|
|
|
});
|
|
|
|
}
|
2023-03-15 17:42:12 +08:00
|
|
|
}
|
2022-12-13 08:53:08 +08:00
|
|
|
|
|
|
|
@discourseComputed(
|
|
|
|
"model.isSaving",
|
|
|
|
"secretValidation",
|
|
|
|
"eventTypeValidation",
|
|
|
|
"model.payload_url"
|
|
|
|
)
|
|
|
|
saveButtonDisabled(
|
|
|
|
isSaving,
|
|
|
|
secretValidation,
|
|
|
|
eventTypeValidation,
|
|
|
|
payloadUrl
|
|
|
|
) {
|
|
|
|
return isSaving
|
|
|
|
? false
|
|
|
|
: secretValidation || eventTypeValidation || isEmpty(payloadUrl);
|
2023-03-15 17:42:12 +08:00
|
|
|
}
|
2022-12-13 08:53:08 +08:00
|
|
|
|
|
|
|
@action
|
|
|
|
async save() {
|
|
|
|
this.set("saved", false);
|
|
|
|
|
|
|
|
try {
|
|
|
|
await this.model.save();
|
|
|
|
|
|
|
|
this.set("saved", true);
|
|
|
|
this.adminWebHooks.model.addObject(this.model);
|
2023-07-19 03:53:23 +08:00
|
|
|
this.router.transitionTo("adminWebHooks.show", this.model);
|
2022-12-13 08:53:08 +08:00
|
|
|
} catch (e) {
|
|
|
|
popupAjaxError(e);
|
|
|
|
}
|
2023-03-15 17:42:12 +08:00
|
|
|
}
|
|
|
|
}
|