discourse/app/assets/javascripts/admin/models/web-hook.js.es6

100 lines
2.8 KiB
Plaintext
Raw Normal View History

2018-06-15 11:03:24 -04:00
import RestModel from "discourse/models/rest";
import Category from "discourse/models/category";
import Group from "discourse/models/group";
import {
default as computed,
observes
} from "ember-addons/ember-computed-decorators";
2016-06-15 13:49:57 -04:00
export default RestModel.extend({
content_type: 1, // json
last_delivery_status: 1, // inactive
wildcard_web_hook: false,
verify_certificate: true,
active: false,
web_hook_event_types: null,
groupsFilterInName: null,
2018-06-15 11:03:24 -04:00
@computed("wildcard_web_hook")
2016-06-15 13:49:57 -04:00
webHookType: {
get(wildcard) {
2018-06-15 11:03:24 -04:00
return wildcard ? "wildcard" : "individual";
2016-06-15 13:49:57 -04:00
},
set(value) {
2018-06-15 11:03:24 -04:00
this.set("wildcard_web_hook", value === "wildcard");
2016-06-15 13:49:57 -04:00
}
},
2018-06-15 11:03:24 -04:00
@computed("category_ids")
categories(categoryIds) {
return Category.findByIds(categoryIds);
2016-06-15 13:49:57 -04:00
},
2018-06-15 11:03:24 -04:00
@observes("group_ids")
2016-06-15 13:49:57 -04:00
updateGroupsFilter() {
2018-06-15 11:03:24 -04:00
const groupIds = this.get("group_ids");
this.set(
"groupsFilterInName",
Discourse.Site.currentProp("groups").reduce((groupNames, g) => {
if (groupIds.includes(g.id)) {
groupNames.push(g.name);
}
return groupNames;
}, [])
);
2016-06-15 13:49:57 -04:00
},
groupFinder(term) {
return Group.findAll({ term: term, ignore_automatic: false });
2016-06-15 13:49:57 -04:00
},
2018-06-15 11:03:24 -04:00
@computed("wildcard_web_hook", "web_hook_event_types.[]")
2016-06-15 13:49:57 -04:00
description(isWildcardWebHook, types) {
2018-06-15 11:03:24 -04:00
let desc = "";
2016-06-15 13:49:57 -04:00
types.forEach(type => {
const name = `${type.name.toLowerCase()}_event`;
2018-06-15 11:03:24 -04:00
desc += desc !== "" ? `, ${name}` : name;
2016-06-15 13:49:57 -04:00
});
2018-06-15 11:03:24 -04:00
return isWildcardWebHook ? "*" : desc;
2016-06-15 13:49:57 -04:00
},
createProperties() {
2018-06-15 11:03:24 -04:00
const types = this.get("web_hook_event_types");
const categoryIds = this.get("categories").map(c => c.id);
2016-06-15 13:49:57 -04:00
// Hack as {{group-selector}} accepts a comma-separated string as data source, but
// we use an array to populate the datasource above.
2018-06-15 11:03:24 -04:00
const groupsFilter = this.get("groupsFilterInName");
const groupNames =
typeof groupsFilter === "string" ? groupsFilter.split(",") : groupsFilter;
2016-06-15 13:49:57 -04:00
return {
2018-06-15 11:03:24 -04:00
payload_url: this.get("payload_url"),
content_type: this.get("content_type"),
secret: this.get("secret"),
wildcard_web_hook: this.get("wildcard_web_hook"),
verify_certificate: this.get("verify_certificate"),
active: this.get("active"),
web_hook_event_type_ids: Ember.isEmpty(types)
? [null]
: types.map(type => type.id),
category_ids: Ember.isEmpty(categoryIds) ? [null] : categoryIds,
2018-06-15 11:03:24 -04:00
group_ids:
Ember.isEmpty(groupNames) || Ember.isEmpty(groupNames[0])
? [null]
: Discourse.Site.currentProp("groups").reduce((groupIds, g) => {
if (groupNames.includes(g.name)) {
groupIds.push(g.id);
}
return groupIds;
}, [])
2016-06-15 13:49:57 -04:00
};
},
updateProperties() {
return this.createProperties();
}
});