2019-10-23 13:06:54 -04:00
|
|
|
import Controller from "@ember/controller";
|
2016-06-15 13:49:57 -04:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2019-10-30 16:28:29 -04:00
|
|
|
import { alias } from "@ember/object/computed";
|
2019-11-07 16:38:28 -05:00
|
|
|
import discourseComputed from "discourse-common/utils/decorators";
|
2016-06-15 13:49:57 -04:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
|
2019-10-23 13:06:54 -04:00
|
|
|
export default Controller.extend({
|
2016-09-13 19:54:53 -04:00
|
|
|
pingDisabled: false,
|
2019-10-30 16:28:29 -04:00
|
|
|
incomingCount: alias("incomingEventIds.length"),
|
2016-09-13 19:54:53 -04:00
|
|
|
|
2019-05-28 06:15:12 -04:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
this.incomingEventIds = [];
|
|
|
|
},
|
|
|
|
|
2019-11-07 16:38:28 -05:00
|
|
|
@discourseComputed("incomingCount")
|
2016-09-13 19:54:53 -04:00
|
|
|
hasIncoming(incomingCount) {
|
|
|
|
return incomingCount > 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
subscribe() {
|
|
|
|
this.messageBus.subscribe(
|
|
|
|
`/web_hook_events/${this.get("model.extras.web_hook_id")}`,
|
|
|
|
(data) => {
|
|
|
|
if (data.event_type === "ping") {
|
|
|
|
this.set("pingDisabled", false);
|
2018-06-15 11:03:24 -04:00
|
|
|
}
|
2016-09-13 19:54:53 -04:00
|
|
|
this._addIncoming(data.web_hook_event_id);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
},
|
|
|
|
|
|
|
|
unsubscribe() {
|
|
|
|
this.messageBus.unsubscribe("/web_hook_events/*");
|
|
|
|
},
|
|
|
|
|
|
|
|
_addIncoming(eventId) {
|
2019-05-27 04:15:39 -04:00
|
|
|
const incomingEventIds = this.incomingEventIds;
|
2016-09-13 19:54:53 -04:00
|
|
|
|
2022-07-17 14:48:36 -04:00
|
|
|
if (!incomingEventIds.includes(eventId)) {
|
2016-09-13 19:54:53 -04:00
|
|
|
incomingEventIds.pushObject(eventId);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2016-06-15 13:49:57 -04:00
|
|
|
actions: {
|
|
|
|
loadMore() {
|
2019-05-27 04:15:39 -04:00
|
|
|
this.model.loadMore();
|
2016-06-15 13:49:57 -04:00
|
|
|
},
|
|
|
|
|
|
|
|
ping() {
|
2016-09-13 19:54:53 -04:00
|
|
|
this.set("pingDisabled", true);
|
|
|
|
|
2016-09-19 04:43:06 -04:00
|
|
|
ajax(
|
|
|
|
`/admin/api/web_hooks/${this.get("model.extras.web_hook_id")}/ping`,
|
|
|
|
{
|
2016-09-13 19:54:53 -04:00
|
|
|
type: "POST",
|
|
|
|
}
|
|
|
|
).catch((error) => {
|
|
|
|
this.set("pingDisabled", false);
|
|
|
|
popupAjaxError(error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
showInserted() {
|
|
|
|
const webHookId = this.get("model.extras.web_hook_id");
|
|
|
|
|
2016-09-19 04:43:06 -04:00
|
|
|
ajax(`/admin/api/web_hooks/${webHookId}/events/bulk`, {
|
2016-09-13 19:54:53 -04:00
|
|
|
type: "GET",
|
2019-05-27 04:15:39 -04:00
|
|
|
data: { ids: this.incomingEventIds },
|
2016-09-13 19:54:53 -04:00
|
|
|
}).then((data) => {
|
|
|
|
const objects = data.map((event) =>
|
|
|
|
this.store.createRecord("web-hook-event", event)
|
|
|
|
);
|
2019-05-27 04:15:39 -04:00
|
|
|
this.model.unshiftObjects(objects);
|
2016-09-13 19:54:53 -04:00
|
|
|
this.set("incomingEventIds", []);
|
|
|
|
});
|
2016-06-15 13:49:57 -04:00
|
|
|
},
|
|
|
|
},
|
|
|
|
});
|