summary refs log tree commit diff
path: root/src/routes/users/@me/channels.ts
blob: 3b2b67813f174b32d71a6a51662804df642cafc2 (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
import { Router, Request, Response } from "express";
import {
	ChannelModel,
	ChannelCreateEvent,
	DMChannel,
	UserModel,
	toObject,
	ChannelType,
	Snowflake,
	trimSpecial,
} from "@fosscord/server-util";
import { HTTPError } from "lambert-server";
import { emitEvent } from "../../../util/Event";
import { getPublicUser } from "../../../util/User";
import { DmChannelCreateSchema } from "../../../schema/Channel";
import { check } from "../../../util/instanceOf";

const router: Router = Router();

router.get("/", async (req: Request, res: Response) => {
	var channels = await ChannelModel.find({
		$or: [
			{ recipients: req.user_id, type: ChannelType.DM },
			{ recipients: req.user_id, type: ChannelType.GROUP_DM },
		],
	}).exec();

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

router.post("/", check(DmChannelCreateSchema), async (req, res) => {
	const body = req.body as DmChannelCreateSchema;
	if (body.recipients.length === 0) throw new HTTPError("You need to specify at least one recipient");
	const type = body.recipients.length === 1 ? ChannelType.DM : ChannelType.GROUP_DM;
	const name = trimSpecial(body.name);

	const channel = {
		name,
		type,
		owner_id: req.user_id,
		id: Snowflake.generate(),
		created_at: new Date(),
	};
	await new ChannelModel(channel).save();

	/*Event({ event: "CHANNEL_CREATE", data: channel } as ChannelCreateEvent);*/

	res.json(channel);
});

export default router;