discourse/app/assets/javascripts/admin/addon/controllers/admin-web-hooks-show-events.js
Jarek Radosz 057d6b406d
DEV: Extensively use includes() (#17541)
Also, the change in insert-hyperlink (from `this.linkUrl.indexOf("http") === -1` to `!this.linkUrl.startsWith("http")`) was intentional fix: we don't want to prevent users from looking up topics with http in their titles.
2022-07-17 20:48:36 +02:00

81 lines
1.9 KiB
JavaScript

import Controller from "@ember/controller";
import { ajax } from "discourse/lib/ajax";
import { alias } from "@ember/object/computed";
import discourseComputed from "discourse-common/utils/decorators";
import { popupAjaxError } from "discourse/lib/ajax-error";
export default Controller.extend({
pingDisabled: false,
incomingCount: alias("incomingEventIds.length"),
init() {
this._super(...arguments);
this.incomingEventIds = [];
},
@discourseComputed("incomingCount")
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);
}
this._addIncoming(data.web_hook_event_id);
}
);
},
unsubscribe() {
this.messageBus.unsubscribe("/web_hook_events/*");
},
_addIncoming(eventId) {
const incomingEventIds = this.incomingEventIds;
if (!incomingEventIds.includes(eventId)) {
incomingEventIds.pushObject(eventId);
}
},
actions: {
loadMore() {
this.model.loadMore();
},
ping() {
this.set("pingDisabled", true);
ajax(
`/admin/api/web_hooks/${this.get("model.extras.web_hook_id")}/ping`,
{
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`, {
type: "GET",
data: { ids: this.incomingEventIds },
}).then((data) => {
const objects = data.map((event) =>
this.store.createRecord("web-hook-event", event)
);
this.model.unshiftObjects(objects);
this.set("incomingEventIds", []);
});
},
},
});