summary refs log tree commit diff
path: root/src/routes/guilds/#guild_id/channels.ts
blob: 9d8a95b0b5bc9b5e31b683af9cbc1a757865b2bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { Router } from "express";
import {
	ChannelCreateEvent,
	ChannelModel,
	ChannelType,
	GuildModel,
	Snowflake,
	toObject,
	ChannelUpdateEvent,
	AnyChannel
} from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import { ChannelModifySchema } from "../../../schema/Channel";
import { emitEvent } from "../../../util/Event";
import { check } from "../../../util/instanceOf";
import { createChannel } from "../../../util/Channel";
const router = Router();

router.get("/", async (req, res) => {
	const { guild_id } = req.params;
	const channels = await ChannelModel.find({ guild_id }).exec();

	res.json(toObject(channels));
});

router.post("/", check(ChannelModifySchema), async (req, res) => {
	const { guild_id } = req.params;
	const body = req.body as ChannelModifySchema;

	const channel = await createChannel({ ...body, guild_id }, req.user_id);

	res.json(channel);
});

router.patch("/", check(ChannelModifySchema), async (req, res) => {
	const { guild_id } = req.params;
	const body = req.body as ChannelModifySchema;

	const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
	if (!guild) throw new HTTPError("Guild not found", 404);

	const channel = {
		...body
	};
	const channelm = await ChannelModel.find({ guild_id }).exec();
	if (!channelm) throw new HTTPError("Channel not found", 404);

	await new ChannelModel(channel).save();

	await emitEvent({ event: "CHANNEL_UPDATE", data: channel } as ChannelUpdateEvent);

	res.json(channel);
});

export default router;