discourse/app/assets/javascripts/admin/controllers/admin-web-hooks-show.js.es6

148 lines
3.9 KiB
Plaintext
Raw Normal View History

import { inject } from '@ember/controller';
import Controller from "@ember/controller";
2018-06-15 11:03:24 -04:00
import { popupAjaxError } from "discourse/lib/ajax-error";
import { extractDomainFromUrl } from "discourse/lib/utilities";
import computed from "ember-addons/ember-computed-decorators";
import InputValidation from "discourse/models/input-validation";
2016-06-15 13:49:57 -04:00
export default Controller.extend({
adminWebHooks: inject(),
2018-06-15 11:03:24 -04:00
eventTypes: Ember.computed.alias("adminWebHooks.eventTypes"),
defaultEventTypes: Ember.computed.alias("adminWebHooks.defaultEventTypes"),
contentTypes: Ember.computed.alias("adminWebHooks.contentTypes"),
2016-06-15 13:49:57 -04:00
@computed
showTagsFilter() {
return this.siteSettings.tagging_enabled;
},
2018-06-15 11:03:24 -04:00
@computed("model.isSaving", "saved", "saveButtonDisabled")
2016-06-15 13:49:57 -04:00
savingStatus(isSaving, saved, saveButtonDisabled) {
if (isSaving) {
2018-06-15 11:03:24 -04:00
return I18n.t("saving");
2016-06-15 13:49:57 -04:00
} else if (!saveButtonDisabled && saved) {
2018-06-15 11:03:24 -04:00
return I18n.t("saved");
2016-06-15 13:49:57 -04:00
}
// Use side effect of validation to clear saved text
2018-06-15 11:03:24 -04:00
this.set("saved", false);
return "";
2016-06-15 13:49:57 -04:00
},
2018-06-15 11:03:24 -04:00
@computed("model.isNew")
2016-06-15 13:49:57 -04:00
saveButtonText(isNew) {
2018-06-15 11:03:24 -04:00
return isNew
? I18n.t("admin.web_hooks.create")
: I18n.t("admin.web_hooks.save");
2016-06-15 13:49:57 -04:00
},
2018-06-15 11:03:24 -04:00
@computed("model.secret")
2016-06-15 13:49:57 -04:00
secretValidation(secret) {
if (!Ember.isEmpty(secret)) {
2018-06-15 11:03:24 -04:00
if (secret.indexOf(" ") !== -1) {
2016-06-15 13:49:57 -04:00
return InputValidation.create({
failed: true,
2018-06-15 11:03:24 -04:00
reason: I18n.t("admin.web_hooks.secret_invalid")
2016-06-15 13:49:57 -04:00
});
}
if (secret.length < 12) {
2018-06-15 11:03:24 -04:00
return InputValidation.create({
2016-06-15 13:49:57 -04:00
failed: true,
2018-06-15 11:03:24 -04:00
reason: I18n.t("admin.web_hooks.secret_too_short")
2016-06-15 13:49:57 -04:00
});
}
}
},
2018-06-15 11:03:24 -04:00
@computed("model.wildcard_web_hook", "model.web_hook_event_types.[]")
2016-06-15 13:49:57 -04:00
eventTypeValidation(isWildcard, eventTypes) {
if (!isWildcard && Ember.isEmpty(eventTypes)) {
return InputValidation.create({
failed: true,
2018-06-15 11:03:24 -04:00
reason: I18n.t("admin.web_hooks.event_type_missing")
2016-06-15 13:49:57 -04:00
});
}
},
@computed(
"model.isSaving",
"secretValidation",
"eventTypeValidation",
"model.payload_url"
)
saveButtonDisabled(
isSaving,
secretValidation,
eventTypeValidation,
payloadUrl
) {
return isSaving
? false
: secretValidation || eventTypeValidation || Ember.isEmpty(payloadUrl);
2016-06-15 13:49:57 -04:00
},
actions: {
save() {
2018-06-15 11:03:24 -04:00
this.set("saved", false);
const url = this.get("model.payload_url");
const domain = extractDomainFromUrl(url);
const model = this.model;
2018-06-15 11:03:24 -04:00
const isNew = model.get("isNew");
2016-06-15 13:49:57 -04:00
const saveWebHook = () => {
2018-06-15 11:03:24 -04:00
return model
.save()
.then(() => {
this.set("saved", true);
this.adminWebHooks.get("model").addObject(model);
2018-06-15 11:03:24 -04:00
if (isNew) {
this.transitionToRoute("adminWebHooks.show", model.get("id"));
}
})
.catch(popupAjaxError);
2016-06-15 13:49:57 -04:00
};
2018-06-15 11:03:24 -04:00
if (
domain === "localhost" ||
domain.match(/192\.168\.\d+\.\d+/) ||
domain.match(/127\.\d+\.\d+\.\d+/) ||
url.startsWith(Discourse.BaseUrl)
2018-06-15 11:03:24 -04: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();
}
2016-06-15 13:49:57 -04:00
}
2018-06-15 11:03:24 -04:00
);
2016-06-15 13:49:57 -04:00
}
return saveWebHook();
},
destroy() {
2018-06-15 11:03:24 -04:00
return bootbox.confirm(
I18n.t("admin.web_hooks.delete_confirm"),
I18n.t("no_value"),
I18n.t("yes_value"),
result => {
if (result) {
const model = this.model;
2018-06-15 11:03:24 -04:00
model
.destroyRecord()
.then(() => {
this.adminWebHooks.get("model").removeObject(model);
2018-06-15 11:03:24 -04:00
this.transitionToRoute("adminWebHooks");
})
.catch(popupAjaxError);
}
2016-06-15 13:49:57 -04:00
}
2018-06-15 11:03:24 -04:00
);
2016-06-15 13:49:57 -04:00
}
}
});