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

Failed to ignore revisions in .git-blame-ignore-revs.

101 lines
2.9 KiB
JavaScript
Raw Normal View History

import { isEmpty } from "@ember/utils";
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 discourseComputed, { observes } from "discourse-common/utils/decorators";
import Site from "discourse/models/site";
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,
@discourseComputed("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
}
},
@discourseComputed("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() {
const groupIds = this.group_ids;
2018-06-15 11:03:24 -04:00
this.set(
"groupsFilterInName",
Site.currentProp("groups").reduce((groupNames, g) => {
2018-06-15 11:03:24 -04:00
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
},
@discourseComputed("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() {
const types = this.web_hook_event_types;
const categoryIds = this.categories.map(c => c.id);
const tagNames = this.tag_names;
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.
const groupsFilter = this.groupsFilterInName;
2018-06-15 11:03:24 -04:00
const groupNames =
typeof groupsFilter === "string" ? groupsFilter.split(",") : groupsFilter;
2016-06-15 13:49:57 -04:00
return {
payload_url: this.payload_url,
content_type: this.content_type,
secret: this.secret,
wildcard_web_hook: this.wildcard_web_hook,
verify_certificate: this.verify_certificate,
active: this.active,
web_hook_event_type_ids: isEmpty(types)
2018-06-15 11:03:24 -04:00
? [null]
: types.map(type => type.id),
category_ids: isEmpty(categoryIds) ? [null] : categoryIds,
tag_names: isEmpty(tagNames) ? [null] : tagNames,
2018-06-15 11:03:24 -04:00
group_ids:
isEmpty(groupNames) || isEmpty(groupNames[0])
2018-06-15 11:03:24 -04:00
? [null]
: Site.currentProp("groups").reduce((groupIds, g) => {
2018-06-15 11:03:24 -04:00
if (groupNames.includes(g.name)) {
groupIds.push(g.id);
}
return groupIds;
}, [])
2016-06-15 13:49:57 -04:00
};
},
updateProperties() {
return this.createProperties();
}
});