summary refs log tree commit diff
path: root/src/api
diff options
context:
space:
mode:
authorMathMan05 <mathmanrm@gmail.com>2026-02-05 10:50:48 -0600
committerMathMan05 <mathmanrm@gmail.com>2026-02-05 10:50:48 -0600
commitc26b0f3a4c248b249cee04a68d65d1198ff7adcd (patch)
treefbf4fd5ed6c2d2c321f2ee38f75f14d3f80b0f60 /src/api
parentarray init (diff)
downloadserver-ts-c26b0f3a4c248b249cee04a68d65d1198ff7adcd.tar.xz
create tags
Diffstat (limited to 'src/api')
-rw-r--r--src/api/routes/channels/#channel_id/tags.ts66
1 files changed, 66 insertions, 0 deletions
diff --git a/src/api/routes/channels/#channel_id/tags.ts b/src/api/routes/channels/#channel_id/tags.ts
new file mode 100644

index 00000000..34b49156 --- /dev/null +++ b/src/api/routes/channels/#channel_id/tags.ts
@@ -0,0 +1,66 @@ +/* + 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 <https://www.gnu.org/licenses/>. +*/ + +import { route } from "@spacebar/api"; +import { Channel, ChannelUpdateEvent, emitEvent, Tag } from "@spacebar/util"; +import { Request, Response, Router } from "express"; +import { TagCreateSchema } from "@spacebar/schemas"; + +const router: Router = Router({ mergeParams: true }); + +router.post( + "/", + route({ + requestBody: "TagCreateSchema", + permission: "MANAGE_CHANNELS", + responses: { + 201: {}, + 404: {}, + }, + }), + async (req: Request, res: Response) => { + const body = req.body as TagCreateSchema; + const { channel_id } = req.params as Record<string, string>; + + const channel = await Channel.findOneOrFail({ + where: { id: channel_id }, + relations: ["available_tags"], + }); + + if (!channel.isForum()) throw new Error("is not thread only channel"); + + const tag = Tag.create({ + channel, + ...body, + }); + channel.available_tags?.push(tag); + + await Promise.all([ + tag.save(), + emitEvent({ + event: "CHANNEL_UPDATE", + data: channel.toJSON(), + channel_id, + } as ChannelUpdateEvent), + ]); + + res.json(channel.toJSON()); + }, +); + +export default router;