2018-06-15 23:03:24 +08:00
|
|
|
import { ajax } from "discourse/lib/ajax";
|
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
|
|
|
import computed from "ember-addons/ember-computed-decorators";
|
2016-06-16 01:49:57 +08:00
|
|
|
|
|
|
|
export default Ember.Controller.extend({
|
2016-09-14 07:54:53 +08:00
|
|
|
pingDisabled: false,
|
|
|
|
incomingCount: Ember.computed.alias("incomingEventIds.length"),
|
|
|
|
|
2019-05-28 18:15:12 +08:00
|
|
|
init() {
|
|
|
|
this._super(...arguments);
|
|
|
|
|
|
|
|
this.incomingEventIds = [];
|
|
|
|
},
|
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
@computed("incomingCount")
|
2016-09-14 07:54:53 +08:00
|
|
|
hasIncoming(incomingCount) {
|
|
|
|
return incomingCount > 0;
|
|
|
|
},
|
|
|
|
|
|
|
|
subscribe() {
|
2018-06-15 23:03:24 +08:00
|
|
|
this.messageBus.subscribe(
|
|
|
|
`/web_hook_events/${this.get("model.extras.web_hook_id")}`,
|
|
|
|
data => {
|
|
|
|
if (data.event_type === "ping") {
|
|
|
|
this.set("pingDisabled", false);
|
|
|
|
}
|
|
|
|
this._addIncoming(data.web_hook_event_id);
|
2016-09-14 07:54:53 +08:00
|
|
|
}
|
2018-06-15 23:03:24 +08:00
|
|
|
);
|
2016-09-14 07:54:53 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
unsubscribe() {
|
2018-06-15 23:03:24 +08:00
|
|
|
this.messageBus.unsubscribe("/web_hook_events/*");
|
2016-09-14 07:54:53 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
_addIncoming(eventId) {
|
2019-05-27 16:15:39 +08:00
|
|
|
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() {
|
2019-05-27 16:15:39 +08:00
|
|
|
this.model.loadMore();
|
2016-06-16 01:49:57 +08:00
|
|
|
},
|
|
|
|
|
|
|
|
ping() {
|
2018-06-15 23:03:24 +08:00
|
|
|
this.set("pingDisabled", true);
|
2016-09-14 07:54:53 +08:00
|
|
|
|
2018-06-15 23:03:24 +08:00
|
|
|
ajax(
|
|
|
|
`/admin/api/web_hooks/${this.get("model.extras.web_hook_id")}/ping`,
|
|
|
|
{
|
|
|
|
type: "POST"
|
|
|
|
}
|
|
|
|
).catch(error => {
|
|
|
|
this.set("pingDisabled", false);
|
2016-09-14 07:54:53 +08:00
|
|
|
popupAjaxError(error);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
showInserted() {
|
2018-06-15 23:03:24 +08:00
|
|
|
const webHookId = this.get("model.extras.web_hook_id");
|
2016-09-14 07:54:53 +08:00
|
|
|
|
2016-09-19 16:43:06 +08:00
|
|
|
ajax(`/admin/api/web_hooks/${webHookId}/events/bulk`, {
|
2018-06-15 23:03:24 +08:00
|
|
|
type: "GET",
|
2019-05-27 16:15:39 +08:00
|
|
|
data: { ids: this.incomingEventIds }
|
2016-09-14 07:54:53 +08:00
|
|
|
}).then(data => {
|
2018-06-15 23:03:24 +08:00
|
|
|
const objects = data.map(event =>
|
|
|
|
this.store.createRecord("web-hook-event", event)
|
|
|
|
);
|
2019-05-27 16:15:39 +08:00
|
|
|
this.model.unshiftObjects(objects);
|
2016-09-14 07:54:53 +08:00
|
|
|
this.set("incomingEventIds", []);
|
|
|
|
});
|
2016-06-16 01:49:57 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|