mirror of
https://github.com/spacebarchat/server.git
synced 2026-07-31 13:39:31 +00:00
allow tag modification
This commit is contained in:
@@ -20,6 +20,7 @@ import { route } from "@spacebar/api";
|
||||
import { Channel, ChannelUpdateEvent, emitEvent, Tag } from "@spacebar/util";
|
||||
import { Request, Response, Router } from "express";
|
||||
import { TagCreateSchema } from "@spacebar/schemas";
|
||||
import { HTTPError } from "#util/util/lambert-server";
|
||||
|
||||
const router: Router = Router({ mergeParams: true });
|
||||
|
||||
@@ -68,6 +69,47 @@ router.post(
|
||||
},
|
||||
);
|
||||
|
||||
router.put(
|
||||
"/:tag_id",
|
||||
route({
|
||||
requestBody: "TagCreateSchema",
|
||||
permission: "MANAGE_CHANNELS",
|
||||
responses: {
|
||||
200: {
|
||||
body: "Channel",
|
||||
},
|
||||
404: {},
|
||||
},
|
||||
}),
|
||||
async (req: Request, res: Response) => {
|
||||
const body = req.body as TagCreateSchema;
|
||||
const { channel_id, tag_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 = channel.available_tags?.find((tag) => tag.id == tag_id);
|
||||
//TODO better error
|
||||
if (!tag) throw new HTTPError("Tag not found");
|
||||
tag.assign(body);
|
||||
|
||||
await Promise.all([
|
||||
tag.save(),
|
||||
emitEvent({
|
||||
event: "CHANNEL_UPDATE",
|
||||
data: channel.toJSON(),
|
||||
channel_id,
|
||||
} as ChannelUpdateEvent),
|
||||
]);
|
||||
|
||||
res.json(channel.toJSON());
|
||||
},
|
||||
);
|
||||
|
||||
router.delete(
|
||||
"/:tag_id",
|
||||
route({
|
||||
|
||||
Reference in New Issue
Block a user