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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

81 lines
1.9 KiB
JavaScript
Raw Normal View History

import Controller from "@ember/controller";
2016-06-16 01:49:57 +08:00
import { ajax } from "discourse/lib/ajax";
import { alias } from "@ember/object/computed";
import discourseComputed from "discourse-common/utils/decorators";
2016-06-16 01:49:57 +08:00
import { popupAjaxError } from "discourse/lib/ajax-error";
export default Controller.extend({
2016-09-14 07:54:53 +08:00
pingDisabled: false,
incomingCount: alias("incomingEventIds.length"),
2016-09-14 07:54:53 +08:00
init() {
this._super(...arguments);
this.incomingEventIds = [];
},
@discourseComputed("incomingCount")
2016-09-14 07:54:53 +08: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 23:03:24 +08:00
}
2016-09-14 07:54:53 +08:00
this._addIncoming(data.web_hook_event_id);
}
);
},
unsubscribe() {
this.messageBus.unsubscribe("/web_hook_events/*");
},
_addIncoming(eventId) {
const incomingEventIds = this.incomingEventIds;
2016-09-14 07:54:53 +08:00
if (incomingEventIds.indexOf(eventId) === -1) {
incomingEventIds.pushObject(eventId);
}
},
2016-06-16 01:49:57 +08:00
actions: {
loadMore() {
this.model.loadMore();
2016-06-16 01:49:57 +08:00
},
ping() {
2016-09-14 07:54:53 +08:00
this.set("pingDisabled", true);
ajax(
`/admin/api/web_hooks/${this.get("model.extras.web_hook_id")}/ping`,
{
2016-09-14 07:54:53 +08:00
type: "POST",
}
).catch((error) => {
this.set("pingDisabled", false);
popupAjaxError(error);
});
},
showInserted() {
const webHookId = this.get("model.extras.web_hook_id");
ajax(`/admin/api/web_hooks/${webHookId}/events/bulk`, {
2016-09-14 07:54:53 +08:00
type: "GET",
data: { ids: this.incomingEventIds },
2016-09-14 07:54:53 +08:00
}).then((data) => {
const objects = data.map((event) =>
this.store.createRecord("web-hook-event", event)
);
this.model.unshiftObjects(objects);
2016-09-14 07:54:53 +08:00
this.set("incomingEventIds", []);
});
2016-06-16 01:49:57 +08:00
},
},
});