summaryrefslogtreecommitdiff
path: root/packages/backend/src/misc/webhook-cache.ts
diff options
context:
space:
mode:
authorsyuilo <Syuilotan@yahoo.co.jp>2022-04-02 15:28:49 +0900
committerGitHub <noreply@github.com>2022-04-02 15:28:49 +0900
commit8e5f2690f29b7e6bee95e54a8bb647ff1ff4b94a (patch)
tree78740472dc48e4fec6986056f548e4ee7743cc29 /packages/backend/src/misc/webhook-cache.ts
parentUpdate CHANGELOG.md (diff)
downloadsharkey-8e5f2690f29b7e6bee95e54a8bb647ff1ff4b94a.tar.gz
sharkey-8e5f2690f29b7e6bee95e54a8bb647ff1ff4b94a.tar.bz2
sharkey-8e5f2690f29b7e6bee95e54a8bb647ff1ff4b94a.zip
feat: Webhook (#8457)
* feat: introduce webhook * wip * wip * wip * Update CHANGELOG.md
Diffstat (limited to 'packages/backend/src/misc/webhook-cache.ts')
-rw-r--r--packages/backend/src/misc/webhook-cache.ts49
1 files changed, 49 insertions, 0 deletions
diff --git a/packages/backend/src/misc/webhook-cache.ts b/packages/backend/src/misc/webhook-cache.ts
new file mode 100644
index 0000000000..4bd2333661
--- /dev/null
+++ b/packages/backend/src/misc/webhook-cache.ts
@@ -0,0 +1,49 @@
+import { Webhooks } from '@/models/index.js';
+import { Webhook } from '@/models/entities/webhook.js';
+import { subsdcriber } from '../db/redis.js';
+
+let webhooksFetched = false;
+let webhooks: Webhook[] = [];
+
+export async function getActiveWebhooks() {
+ if (!webhooksFetched) {
+ webhooks = await Webhooks.findBy({
+ active: true,
+ });
+ webhooksFetched = true;
+ }
+
+ return webhooks;
+}
+
+subsdcriber.on('message', async (_, data) => {
+ const obj = JSON.parse(data);
+
+ if (obj.channel === 'internal') {
+ const { type, body } = obj.message;
+ switch (type) {
+ case 'webhookCreated':
+ if (body.active) {
+ webhooks.push(body);
+ }
+ break;
+ case 'webhookUpdated':
+ if (body.active) {
+ const i = webhooks.findIndex(a => a.id === body.id);
+ if (i > -1) {
+ webhooks[i] = body;
+ } else {
+ webhooks.push(body);
+ }
+ } else {
+ webhooks = webhooks.filter(a => a.id !== body.id);
+ }
+ break;
+ case 'webhookDeleted':
+ webhooks = webhooks.filter(a => a.id !== body.id);
+ break;
+ default:
+ break;
+ }
+ }
+});