From adc3474216af90df8ff54089eb6d2ad67a95b533 Mon Sep 17 00:00:00 2001 From: TomatoCake <60300461+DEVTomatoCake@users.noreply.github.com> Date: Thu, 18 Jul 2024 15:35:10 +0200 Subject: [PATCH] Consistent relations, url property, src channel --- assets/openapi.json | Bin 604136 -> 604475 bytes assets/schemas.json | Bin 22041912 -> 22078920 bytes .../routes/channels/#channel_id/webhooks.ts | 17 +++++++++++----- src/api/routes/guilds/#guild_id/webhooks.ts | 19 ++++++++++++------ .../webhooks/#webhook_id/#token/index.ts | 10 ++++++++- src/api/routes/webhooks/#webhook_id/index.ts | 11 ++++++++-- src/util/entities/Webhook.ts | 16 ++++++++++++--- 7 files changed, 56 insertions(+), 17 deletions(-) diff --git a/assets/openapi.json b/assets/openapi.json index 5cf313273185976af200242369f0c2d3c2ff14c9..be317720b104de104adb5083e0e2c4368e1b094d 100644 GIT binary patch delta 197 zcmaFyUS;hA7ZET#xk-`H=4_o0%2WpWhvkOA(rtR<8+63MtO+f#EHyGlM`52 zr`M%0Nl#z;f>C^WLkN)j?l7a@bp1wV_V$VdMj&PaV&?4?2`mQM(>q_YYbYt?7b%qF zDhA7ZEb&u4avY|{jP{cg zSXifjsALwK-oKogZMwrMM!)F^l`QP-4T+3E%ml>D+Zz&D474W~GK*{v*vBfv*sk5j T0>rF9%m&2l+qL^R1Xcq8H2o|I diff --git a/assets/schemas.json b/assets/schemas.json index cd3a018f0b34691e45472a154bb8f5e97b1bfe77..aecc76a210318ca1858f9baaa6ab1145bd4a0c5e 100644 GIT binary patch delta 12256 zcmb`NeN@xe8OP5B0wf_MfKXoKO+-Wxs)7X3La7!Z7(j+75P}fV(r#ki6xRtzr+4G1 zsWQ2vLjnQ=5dkUqpiq8R-LzI-Y*#&w^13qST7e-2bcp*UoSp6eK7Zt#gyekqexLi? z=id9=Ut8U0qPt(*66Lp$-TPR6m$MnWS+a8vo3t}EWyW@)o-DiYP&7>5G@d7H_m=|K z#osKevRzj6k%@racu#82@@I%6>-|0@9-7W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R5vQOz;&d%$!*n6x zuUzneK^tbn1V`UF3(t8~PMP3&i26Z>~=6rQc;FcM$VPhWJ{A- z_$O9Qth3(MN#Ar$P8^xtrK3(P=b6}%=7_IAj`&{+#38j!?E~A?{%YhnrU7TJLcp1S zr(t?>$$vmDxjq+3QhI&K4Y?JDG6!z8GsZ8n> zb?wftEBX{hb*8!-B`s?>Fu~+fb45<o)fWVts%){+0p0oe_6{Vk zy?gcd<{Q&Kc-^bQ2T#jsC{@5!o9qrcDp@> zZnvKUv3=9@(pFLY1S*Q3dLq)4he6Z*KQ+RQyvSL2zxd3;o46pbuD*~EuD;NMEXK&~ z9hQXK-XS2r!^ljP72z^fBC-G@HThz~Y4WXcQh7l_IOPQ!l7VRom9~TvD(y%R^0uLC zUT;sh=Jis%_o}wBgsWept07#&-bmtOmb^TyOS?6jaA~)qi1`P=-eH|F0$hApH2E5dm9DmI9>3hmVu%Ro y-T0^|)P)SZMYxcGSYp5epbL#7oG$bog1t!SuI-5@+_gOkB-5;guZJxp#s3GS7UAsx diff --git a/src/api/routes/channels/#channel_id/webhooks.ts b/src/api/routes/channels/#channel_id/webhooks.ts index 4c1ccbdf..7533fceb 100644 --- a/src/api/routes/channels/#channel_id/webhooks.ts +++ b/src/api/routes/channels/#channel_id/webhooks.ts @@ -1,17 +1,17 @@ /* Spacebar: A FOSS re-implementation and extension of the Discord.com backend. Copyright (C) 2023 Spacebar and Spacebar Contributors - + This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ @@ -53,13 +53,20 @@ router.get( where: { channel_id }, relations: [ "user", + "channel", + "source_channel", "guild", "source_guild", - "application" /*"source_channel"*/, + "application", ], }); - return res.json(webhooks); + const instanceUrl = + Config.get().api.endpointPublic || "http://localhost:3001"; + return res.json(webhooks.map(webhook => ({ + ...webhook, + url: instanceUrl + "/webhooks/" + webhook.id + "/" + webhook.token, + }))); }, ); diff --git a/src/api/routes/guilds/#guild_id/webhooks.ts b/src/api/routes/guilds/#guild_id/webhooks.ts index a2ef7d69..60c9818a 100644 --- a/src/api/routes/guilds/#guild_id/webhooks.ts +++ b/src/api/routes/guilds/#guild_id/webhooks.ts @@ -1,23 +1,23 @@ /* Spacebar: A FOSS re-implementation and extension of the Discord.com backend. Copyright (C) 2023 Spacebar and Spacebar Contributors - + This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. - + This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ import { route } from "@spacebar/api"; -import { Webhook } from "@spacebar/util"; +import { Config, Webhook } from "@spacebar/util"; import { Request, Response, Router } from "express"; const router = Router(); @@ -39,13 +39,20 @@ router.get( where: { guild_id }, relations: [ "user", + "channel", + "source_channel", "guild", "source_guild", - "application" /*"source_channel"*/, + "application", ], }); - return res.json(webhooks); + const instanceUrl = + Config.get().api.endpointPublic || "http://localhost:3001"; + return res.json(webhooks.map(webhook => ({ + ...webhook, + url: instanceUrl + "/webhooks/" + webhook.id + "/" + webhook.token, + }))); }, ); diff --git a/src/api/routes/webhooks/#webhook_id/#token/index.ts b/src/api/routes/webhooks/#webhook_id/#token/index.ts index 6d1449eb..8e0ad0dd 100644 --- a/src/api/routes/webhooks/#webhook_id/#token/index.ts +++ b/src/api/routes/webhooks/#webhook_id/#token/index.ts @@ -34,7 +34,14 @@ router.get( where: { id: webhook_id, }, - relations: ["channel", "guild", "application"], + relations: [ + "user", + "channel", + "source_channel", + "guild", + "source_guild", + "application", + ], }); if (!webhook) { @@ -65,6 +72,7 @@ const messageUpload = multer({ }); // max upload 50 mb // https://discord.com/developers/docs/resources/webhook#execute-webhook +// TODO: GitHub/Slack compatible hooks router.post( "/", messageUpload.any(), diff --git a/src/api/routes/webhooks/#webhook_id/index.ts b/src/api/routes/webhooks/#webhook_id/index.ts index 98faaac1..c10bf224 100644 --- a/src/api/routes/webhooks/#webhook_id/index.ts +++ b/src/api/routes/webhooks/#webhook_id/index.ts @@ -11,7 +11,7 @@ const router = Router(); router.get( "/", route({ - description: "Returns a webhook object for the given id.", + description: "Returns a webhook object for the given id. Requires the MANAGE_WEBHOOKS permission or to be the owner of the webhook.", responses: { 200: { body: "APIWebhook", @@ -23,7 +23,14 @@ router.get( const { webhook_id } = req.params; const webhook = await Webhook.findOneOrFail({ where: { id: webhook_id }, - relations: ["channel", "guild", "application", "user"], + relations: [ + "user", + "channel", + "source_channel", + "guild", + "source_guild", + "application", + ], }); if (webhook.guild_id) { diff --git a/src/util/entities/Webhook.ts b/src/util/entities/Webhook.ts index 8b1585ad..9539d6e8 100644 --- a/src/util/entities/Webhook.ts +++ b/src/util/entities/Webhook.ts @@ -85,13 +85,23 @@ export class Webhook extends BaseClass { @Column({ nullable: true }) @RelationId((webhook: Webhook) => webhook.guild) - source_guild_id: string; + source_guild_id?: string; @JoinColumn({ name: "source_guild_id" }) @ManyToOne(() => Guild, { onDelete: "CASCADE", }) - source_guild: Guild; + source_guild?: Guild; - url?: string; + @Column({ nullable: true }) + @RelationId((webhook: Webhook) => webhook.channel) + source_channel_id: string; + + @JoinColumn({ name: "source_channel_id" }) + @ManyToOne(() => Channel, { + onDelete: "CASCADE", + }) + source_channel: Channel; + + url: string; }