2022-12-13 08:53:08 +08:00
|
|
|
import Component from "@glimmer/component";
|
|
|
|
import { tracked } from "@glimmer/tracking";
|
|
|
|
import { action } from "@ember/object";
|
|
|
|
import { gt, readOnly } from "@ember/object/computed";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { inject as service } from "@ember/service";
|
|
|
|
import { ajax } from "discourse/lib/ajax";
|
2022-12-13 08:53:08 +08:00
|
|
|
import { popupAjaxError } from "discourse/lib/ajax-error";
|
2023-10-11 02:38:59 +08:00
|
|
|
import { bind } from "discourse-common/utils/decorators";
|
2022-12-13 08:53:08 +08:00
|
|
|
|
|
|
|
export default class WebhookEvents extends Component {
|
|
|
|
@service messageBus;
|
|
|
|
@service store;
|
|
|
|
|
|
|
|
@tracked pingEnabled = true;
|
|
|
|
@tracked events = [];
|
|
|
|
@tracked incomingEventIds = [];
|
|
|
|
|
|
|
|
@readOnly("incomingEventIds.length") incomingCount;
|
|
|
|
@gt("incomingCount", 0) hasIncoming;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
super(...arguments);
|
|
|
|
this.loadEvents();
|
|
|
|
}
|
|
|
|
|
|
|
|
async loadEvents() {
|
|
|
|
this.events = await this.store.findAll(
|
|
|
|
"web-hook-event",
|
|
|
|
this.args.webhookId
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
@bind
|
|
|
|
subscribe() {
|
|
|
|
const channel = `/web_hook_events/${this.args.webhookId}`;
|
|
|
|
this.messageBus.subscribe(channel, this._addIncoming);
|
|
|
|
}
|
|
|
|
|
|
|
|
@bind
|
|
|
|
unsubscribe() {
|
|
|
|
this.messageBus.unsubscribe("/web_hook_events/*", this._addIncoming);
|
|
|
|
}
|
|
|
|
|
|
|
|
@bind
|
|
|
|
_addIncoming(data) {
|
|
|
|
if (data.event_type === "ping") {
|
|
|
|
this.pingEnabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!this.incomingEventIds.includes(data.web_hook_event_id)) {
|
|
|
|
this.incomingEventIds.pushObject(data.web_hook_event_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
async showInserted(event) {
|
|
|
|
event?.preventDefault();
|
|
|
|
|
|
|
|
const path = `/admin/api/web_hooks/${this.args.webhookId}/events/bulk`;
|
|
|
|
const data = await ajax(path, {
|
|
|
|
data: { ids: this.incomingEventIds },
|
|
|
|
});
|
|
|
|
|
2022-12-13 20:32:34 +08:00
|
|
|
const objects = data.map((webhookEvent) =>
|
|
|
|
this.store.createRecord("web-hook-event", webhookEvent)
|
2022-12-13 08:53:08 +08:00
|
|
|
);
|
|
|
|
this.events.unshiftObjects(objects);
|
|
|
|
this.incomingEventIds = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
loadMore() {
|
|
|
|
this.events.loadMore();
|
|
|
|
}
|
|
|
|
|
|
|
|
@action
|
|
|
|
async ping() {
|
|
|
|
this.pingEnabled = false;
|
|
|
|
|
|
|
|
try {
|
|
|
|
await ajax(`/admin/api/web_hooks/${this.args.webhookId}/ping`, {
|
|
|
|
type: "POST",
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
this.pingEnabled = true;
|
|
|
|
popupAjaxError(error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|