2020-03-06 11:41:41 -05:00
|
|
|
import Controller, { inject as controller } from "@ember/controller";
|
2019-11-11 15:48:33 -05:00
|
|
|
import EmberObject from "@ember/object";
|
2020-05-13 16:23:41 -04:00
|
|
|
import I18n from "I18n";
|
2019-10-30 16:28:29 -04:00
|
|
|
import { alias } from "@ember/object/computed";
|
2016-06-15 13:49:57 -04:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2020-08-26 12:57:13 -04:00
|
|
|
import { isEmpty } from "@ember/utils";
|
2016-06-15 13:49:57 -04:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2022-09-14 11:06:56 -04:00
|
|
|
import { inject as service } from "@ember/service";
|
2016-06-15 13:49:57 -04:00
|
|
|
|
2019-10-23 13:06:54 -04:00
|
|
|
export default Controller.extend({
|
2020-03-06 11:41:41 -05:00
|
|
|
adminWebHooks: controller(),
|
2022-09-14 11:06:56 -04:00
|
|
|
dialog: service(),
|
2019-10-30 16:28:29 -04:00
|
|
|
eventTypes: alias("adminWebHooks.eventTypes"),
|
|
|
|
defaultEventTypes: alias("adminWebHooks.defaultEventTypes"),
|
|
|
|
contentTypes: alias("adminWebHooks.contentTypes"),
|
2016-06-15 13:49:57 -04:00
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed
|
2018-12-05 04:14:06 -05:00
|
|
|
showTagsFilter() {
|
|
|
|
return this.siteSettings.tagging_enabled;
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("model.isSaving", "saved", "saveButtonDisabled")
|
2016-06-15 13:49:57 -04:00
|
|
|
savingStatus(isSaving, saved, saveButtonDisabled) {
|
|
|
|
if (isSaving) {
|
|
|
|
return I18n.t("saving");
|
|
|
|
} else if (!saveButtonDisabled && saved) {
|
|
|
|
return I18n.t("saved");
|
|
|
|
}
|
|
|
|
// Use side effect of validation to clear saved text
|
|
|
|
this.set("saved", false);
|
|
|
|
return "";
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("model.isNew")
|
2016-06-15 13:49:57 -04:00
|
|
|
saveButtonText(isNew) {
|
|
|
|
return isNew
|
|
|
|
? I18n.t("admin.web_hooks.create")
|
|
|
|
: I18n.t("admin.web_hooks.save");
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("model.secret")
|
2016-06-15 13:49:57 -04:00
|
|
|
secretValidation(secret) {
|
2019-10-31 13:37:24 -04:00
|
|
|
if (!isEmpty(secret)) {
|
2022-07-17 14:48:36 -04:00
|
|
|
if (secret.includes(" ")) {
|
2019-11-11 15:48:33 -05:00
|
|
|
return EmberObject.create({
|
2016-06-15 13:49:57 -04:00
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("admin.web_hooks.secret_invalid"),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (secret.length < 12) {
|
2019-11-11 15:48:33 -05:00
|
|
|
return EmberObject.create({
|
2016-06-15 13:49:57 -04:00
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("admin.web_hooks.secret_too_short"),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("model.wildcard_web_hook", "model.web_hook_event_types.[]")
|
2016-06-15 13:49:57 -04:00
|
|
|
eventTypeValidation(isWildcard, eventTypes) {
|
2019-10-31 13:37:24 -04:00
|
|
|
if (!isWildcard && isEmpty(eventTypes)) {
|
2019-11-11 15:48:33 -05:00
|
|
|
return EmberObject.create({
|
2016-06-15 13:49:57 -04:00
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("admin.web_hooks.event_type_missing"),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed(
|
2019-01-10 05:06:01 -05:00
|
|
|
"model.isSaving",
|
|
|
|
"secretValidation",
|
|
|
|
"eventTypeValidation",
|
|
|
|
"model.payload_url"
|
|
|
|
)
|
|
|
|
saveButtonDisabled(
|
|
|
|
isSaving,
|
|
|
|
secretValidation,
|
|
|
|
eventTypeValidation,
|
|
|
|
payloadUrl
|
|
|
|
) {
|
|
|
|
return isSaving
|
|
|
|
? false
|
2019-10-31 13:37:24 -04:00
|
|
|
: secretValidation || eventTypeValidation || isEmpty(payloadUrl);
|
2016-06-15 13:49:57 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
save() {
|
|
|
|
this.set("saved", false);
|
2019-05-27 04:15:39 -04:00
|
|
|
const model = this.model;
|
2017-01-08 22:22:35 -05:00
|
|
|
const isNew = model.get("isNew");
|
|
|
|
|
2022-11-01 12:33:17 -04:00
|
|
|
return model
|
|
|
|
.save()
|
|
|
|
.then(() => {
|
|
|
|
this.set("saved", true);
|
|
|
|
this.adminWebHooks.get("model").addObject(model);
|
2017-01-08 22:22:35 -05:00
|
|
|
|
2022-11-01 12:33:17 -04:00
|
|
|
if (isNew) {
|
|
|
|
this.transitionToRoute("adminWebHooks.show", model.get("id"));
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
2016-06-15 13:49:57 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
destroy() {
|
2022-09-14 11:06:56 -04:00
|
|
|
return this.dialog.yesNoConfirm({
|
|
|
|
message: I18n.t("admin.web_hooks.delete_confirm"),
|
|
|
|
didConfirm: () => {
|
|
|
|
this.model
|
|
|
|
.destroyRecord()
|
|
|
|
.then(() => {
|
|
|
|
this.adminWebHooks.get("model").removeObject(this.model);
|
|
|
|
this.transitionToRoute("adminWebHooks");
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
|
|
|
},
|
|
|
|
});
|
2016-06-15 13:49:57 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|