2020-05-14 04:23:41 +08:00
|
|
|
import I18n from "I18n";
|
2019-11-08 05:38:28 +08:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2019-11-01 01:37:24 +08:00
|
|
|
import { isEmpty } from "@ember/utils";
|
2019-10-31 04:28:29 +08:00
|
|
|
import { alias } from "@ember/object/computed";
|
2020-03-07 00:41:41 +08:00
|
|
|
import Controller, { inject as controller } from "@ember/controller";
|
2016-06-16 01:49:57 +08:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
import { extractDomainFromUrl } from "discourse/lib/utilities";
|
2019-11-12 04:48:33 +08:00
|
|
|
import EmberObject from "@ember/object";
|
2020-06-04 00:45:26 +08:00
|
|
|
import { isAbsoluteURL } from "discourse-common/lib/get-url";
|
2016-06-16 01:49:57 +08:00
|
|
|
|
2019-10-24 01:06:54 +08:00
|
|
|
export default Controller.extend({
|
2020-03-07 00:41:41 +08:00
|
|
|
adminWebHooks: controller(),
|
2019-10-31 04:28:29 +08:00
|
|
|
eventTypes: alias("adminWebHooks.eventTypes"),
|
|
|
|
defaultEventTypes: alias("adminWebHooks.defaultEventTypes"),
|
|
|
|
contentTypes: alias("adminWebHooks.contentTypes"),
|
2016-06-16 01:49:57 +08:00
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed
|
2018-12-05 17:14:06 +08:00
|
|
|
showTagsFilter() {
|
|
|
|
return this.siteSettings.tagging_enabled;
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("model.isSaving", "saved", "saveButtonDisabled")
|
2016-06-16 01:49:57 +08: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-08 05:38:28 +08:00
|
|
|
@discourseComputed("model.isNew")
|
2016-06-16 01:49:57 +08:00
|
|
|
saveButtonText(isNew) {
|
|
|
|
return isNew
|
|
|
|
? I18n.t("admin.web_hooks.create")
|
|
|
|
: I18n.t("admin.web_hooks.save");
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("model.secret")
|
2016-06-16 01:49:57 +08:00
|
|
|
secretValidation(secret) {
|
2019-11-01 01:37:24 +08:00
|
|
|
if (!isEmpty(secret)) {
|
2016-06-16 01:49:57 +08:00
|
|
|
if (secret.indexOf(" ") !== -1) {
|
2019-11-12 04:48:33 +08:00
|
|
|
return EmberObject.create({
|
2016-06-16 01:49:57 +08:00
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("admin.web_hooks.secret_invalid")
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (secret.length < 12) {
|
2019-11-12 04:48:33 +08:00
|
|
|
return EmberObject.create({
|
2016-06-16 01:49:57 +08:00
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("admin.web_hooks.secret_too_short")
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("model.wildcard_web_hook", "model.web_hook_event_types.[]")
|
2016-06-16 01:49:57 +08:00
|
|
|
eventTypeValidation(isWildcard, eventTypes) {
|
2019-11-01 01:37:24 +08:00
|
|
|
if (!isWildcard && isEmpty(eventTypes)) {
|
2019-11-12 04:48:33 +08:00
|
|
|
return EmberObject.create({
|
2016-06-16 01:49:57 +08:00
|
|
|
failed: true,
|
|
|
|
reason: I18n.t("admin.web_hooks.event_type_missing")
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed(
|
2019-01-10 18:06:01 +08:00
|
|
|
"model.isSaving",
|
|
|
|
"secretValidation",
|
|
|
|
"eventTypeValidation",
|
|
|
|
"model.payload_url"
|
|
|
|
)
|
|
|
|
saveButtonDisabled(
|
|
|
|
isSaving,
|
|
|
|
secretValidation,
|
|
|
|
eventTypeValidation,
|
|
|
|
payloadUrl
|
|
|
|
) {
|
|
|
|
return isSaving
|
|
|
|
? false
|
2019-11-01 01:37:24 +08:00
|
|
|
: secretValidation || eventTypeValidation || isEmpty(payloadUrl);
|
2016-06-16 01:49:57 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
actions: {
|
|
|
|
save() {
|
|
|
|
this.set("saved", false);
|
2019-05-07 12:36:40 +08:00
|
|
|
const url = this.get("model.payload_url");
|
|
|
|
const domain = extractDomainFromUrl(url);
|
2019-05-27 16:15:39 +08:00
|
|
|
const model = this.model;
|
2017-01-09 11:22:35 +08:00
|
|
|
const isNew = model.get("isNew");
|
|
|
|
|
2016-06-16 01:49:57 +08:00
|
|
|
const saveWebHook = () => {
|
|
|
|
return model
|
|
|
|
.save()
|
|
|
|
.then(() => {
|
|
|
|
this.set("saved", true);
|
2019-05-27 16:42:53 +08:00
|
|
|
this.adminWebHooks.get("model").addObject(model);
|
2017-01-09 11:22:35 +08:00
|
|
|
|
|
|
|
if (isNew) {
|
|
|
|
this.transitionToRoute("adminWebHooks.show", model.get("id"));
|
|
|
|
}
|
2016-06-16 01:49:57 +08:00
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (
|
2019-05-07 12:36:40 +08:00
|
|
|
domain === "localhost" ||
|
|
|
|
domain.match(/192\.168\.\d+\.\d+/) ||
|
|
|
|
domain.match(/127\.\d+\.\d+\.\d+/) ||
|
2020-06-04 00:45:26 +08:00
|
|
|
isAbsoluteURL(url)
|
2016-06-16 01:49:57 +08:00
|
|
|
) {
|
|
|
|
return bootbox.confirm(
|
|
|
|
I18n.t("admin.web_hooks.warn_local_payload_url"),
|
|
|
|
I18n.t("no_value"),
|
|
|
|
I18n.t("yes_value"),
|
|
|
|
result => {
|
|
|
|
if (result) {
|
|
|
|
return saveWebHook();
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2016-06-16 01:49:57 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return saveWebHook();
|
|
|
|
},
|
|
|
|
|
|
|
|
destroy() {
|
|
|
|
return bootbox.confirm(
|
|
|
|
I18n.t("admin.web_hooks.delete_confirm"),
|
|
|
|
I18n.t("no_value"),
|
|
|
|
I18n.t("yes_value"),
|
|
|
|
result => {
|
|
|
|
if (result) {
|
2019-05-27 16:15:39 +08:00
|
|
|
const model = this.model;
|
2016-06-16 01:49:57 +08:00
|
|
|
model
|
|
|
|
.destroyRecord()
|
|
|
|
.then(() => {
|
2019-05-27 16:42:53 +08:00
|
|
|
this.adminWebHooks.get("model").removeObject(model);
|
2016-06-16 01:49:57 +08:00
|
|
|
this.transitionToRoute("adminWebHooks");
|
|
|
|
})
|
|
|
|
.catch(popupAjaxError);
|
2018-06-15 23:03:24 +08:00
|
|
|
}
|
2016-06-16 01:49:57 +08:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|