2024-04-12 05:11:00 +08:00
|
|
|
import { tracked } from "@glimmer/tracking";
|
2023-03-17 18:18:42 +08:00
|
|
|
import { computed } from "@ember/object";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { isEmpty } from "@ember/utils";
|
2023-03-17 18:18:42 +08:00
|
|
|
import { observes } from "@ember-decorators/object";
|
2016-06-16 01:49:57 +08:00
|
|
|
import Group from "discourse/models/group";
|
2020-01-17 01:56:53 +08:00
|
|
|
import RestModel from "discourse/models/rest";
|
2019-11-14 01:13:47 +08:00
|
|
|
import Site from "discourse/models/site";
|
2023-10-11 02:38:59 +08:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2016-06-16 01:49:57 +08:00
|
|
|
|
2024-04-12 05:11:00 +08:00
|
|
|
class WebHookExtras {
|
|
|
|
@tracked categories;
|
|
|
|
|
|
|
|
constructor(args) {
|
2024-04-25 22:43:16 +08:00
|
|
|
this.categories = args.categories || [];
|
|
|
|
this.content_types = args.content_types || [];
|
|
|
|
this.default_event_types = args.default_event_types || [];
|
|
|
|
this.delivery_statuses = args.delivery_statuses || [];
|
|
|
|
this.grouped_event_types = args.grouped_event_types || [];
|
|
|
|
}
|
|
|
|
|
|
|
|
addCategories(categories) {
|
|
|
|
this.categories = this.categories.concat(categories).uniqBy((c) => c.id);
|
2024-04-12 05:11:00 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
get categoriesById() {
|
|
|
|
if (this.categories) {
|
|
|
|
return new Map(this.categories.map((c) => [c.id, c]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
findCategoryById(id) {
|
|
|
|
return this.categoriesById?.get(id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-17 18:18:42 +08:00
|
|
|
export default class WebHook extends RestModel {
|
2024-04-12 05:11:00 +08:00
|
|
|
static ExtrasClass = WebHookExtras;
|
2023-03-17 18:18:42 +08:00
|
|
|
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;
|
2016-06-16 01:49:57 +08:00
|
|
|
|
2023-03-17 18:18:42 +08:00
|
|
|
@computed("wildcard_web_hook")
|
|
|
|
get wildcard() {
|
|
|
|
return this.wildcard_web_hook ? "wildcard" : "individual";
|
|
|
|
}
|
|
|
|
|
|
|
|
set wildcard(value) {
|
|
|
|
this.set("wildcard_web_hook", value === "wildcard");
|
|
|
|
}
|
2016-06-16 01:49:57 +08:00
|
|
|
|
2024-01-25 20:25:02 +08:00
|
|
|
@computed("category_ids")
|
|
|
|
get categories() {
|
2024-04-12 05:11:00 +08:00
|
|
|
return (this.category_ids || []).map((id) =>
|
|
|
|
this.extras.findCategoryById(id)
|
|
|
|
);
|
2024-01-25 20:25:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
set categories(value) {
|
2024-04-25 22:43:16 +08:00
|
|
|
this.extras ||= new WebHookExtras({});
|
|
|
|
this.extras.addCategories(value);
|
|
|
|
|
2024-01-25 20:25:02 +08:00
|
|
|
this.set(
|
|
|
|
"category_ids",
|
|
|
|
value.map((c) => c.id)
|
|
|
|
);
|
2023-03-17 18:18:42 +08:00
|
|
|
}
|
2016-06-16 01:49:57 +08:00
|
|
|
|
|
|
|
@observes("group_ids")
|
|
|
|
updateGroupsFilter() {
|
2019-05-27 16:15:39 +08:00
|
|
|
const groupIds = this.group_ids;
|
2016-06-16 01:49:57 +08:00
|
|
|
this.set(
|
|
|
|
"groupsFilterInName",
|
2019-11-14 01:13:47 +08:00
|
|
|
Site.currentProp("groups").reduce((groupNames, g) => {
|
2016-06-16 01:49:57 +08:00
|
|
|
if (groupIds.includes(g.id)) {
|
|
|
|
groupNames.push(g.name);
|
|
|
|
}
|
|
|
|
return groupNames;
|
|
|
|
}, [])
|
|
|
|
);
|
2023-03-17 18:18:42 +08:00
|
|
|
}
|
2016-06-16 01:49:57 +08:00
|
|
|
|
|
|
|
groupFinder(term) {
|
2021-11-10 07:31:41 +08:00
|
|
|
return Group.findAll({ term, ignore_automatic: false });
|
2023-03-17 18:18:42 +08:00
|
|
|
}
|
2016-06-16 01:49:57 +08:00
|
|
|
|
2019-11-08 05:38:28 +08:00
|
|
|
@discourseComputed("wildcard_web_hook", "web_hook_event_types.[]")
|
2016-06-16 01:49:57 +08:00
|
|
|
description(isWildcardWebHook, types) {
|
|
|
|
let desc = "";
|
|
|
|
|
|
|
|
types.forEach((type) => {
|
|
|
|
const name = `${type.name.toLowerCase()}_event`;
|
|
|
|
desc += desc !== "" ? `, ${name}` : name;
|
|
|
|
});
|
|
|
|
|
|
|
|
return isWildcardWebHook ? "*" : desc;
|
2023-03-17 18:18:42 +08:00
|
|
|
}
|
2016-06-16 01:49:57 +08:00
|
|
|
|
|
|
|
createProperties() {
|
2019-05-27 16:15:39 +08:00
|
|
|
const types = this.web_hook_event_types;
|
|
|
|
const categoryIds = this.categories.map((c) => c.id);
|
|
|
|
const tagNames = this.tag_names;
|
2017-01-09 11:22:35 +08:00
|
|
|
|
2016-06-16 01:49:57 +08:00
|
|
|
// Hack as {{group-selector}} accepts a comma-separated string as data source, but
|
|
|
|
// we use an array to populate the datasource above.
|
2019-05-27 16:15:39 +08:00
|
|
|
const groupsFilter = this.groupsFilterInName;
|
2016-06-16 01:49:57 +08:00
|
|
|
const groupNames =
|
|
|
|
typeof groupsFilter === "string" ? groupsFilter.split(",") : groupsFilter;
|
|
|
|
|
|
|
|
return {
|
2019-05-27 16:15:39 +08:00
|
|
|
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,
|
2019-11-01 01:37:24 +08:00
|
|
|
web_hook_event_type_ids: isEmpty(types)
|
2016-06-16 01:49:57 +08:00
|
|
|
? [null]
|
|
|
|
: types.map((type) => type.id),
|
2019-11-01 01:37:24 +08:00
|
|
|
category_ids: isEmpty(categoryIds) ? [null] : categoryIds,
|
|
|
|
tag_names: isEmpty(tagNames) ? [null] : tagNames,
|
2016-06-16 01:49:57 +08:00
|
|
|
group_ids:
|
2019-11-01 01:37:24 +08:00
|
|
|
isEmpty(groupNames) || isEmpty(groupNames[0])
|
2016-06-16 01:49:57 +08:00
|
|
|
? [null]
|
2019-11-14 01:13:47 +08:00
|
|
|
: Site.currentProp("groups").reduce((groupIds, g) => {
|
2016-06-16 01:49:57 +08:00
|
|
|
if (groupNames.includes(g.name)) {
|
|
|
|
groupIds.push(g.id);
|
|
|
|
}
|
|
|
|
return groupIds;
|
|
|
|
}, []),
|
|
|
|
};
|
2023-03-17 18:18:42 +08:00
|
|
|
}
|
2016-06-16 01:49:57 +08:00
|
|
|
|
|
|
|
updateProperties() {
|
|
|
|
return this.createProperties();
|
2023-03-17 18:18:42 +08:00
|
|
|
}
|
|
|
|
}
|