summary refs log tree commit diff
path: root/api/src/routes/users/@me/channels.ts
diff options
context:
space:
mode:
Diffstat (limited to 'api/src/routes/users/@me/channels.ts')
-rw-r--r--api/src/routes/users/@me/channels.ts53
1 files changed, 53 insertions, 0 deletions
diff --git a/api/src/routes/users/@me/channels.ts b/api/src/routes/users/@me/channels.ts
new file mode 100644

index 00000000..a425a25f --- /dev/null +++ b/api/src/routes/users/@me/channels.ts
@@ -0,0 +1,53 @@ +import { Router, Request, Response } from "express"; +import { + ChannelModel, + ChannelCreateEvent, + toObject, + ChannelType, + Snowflake, + trimSpecial, + Channel, + DMChannel, + UserModel +} from "@fosscord/server-util"; +import { HTTPError } from "lambert-server"; +import { emitEvent } from "../../../util/Event"; +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({ recipient_ids: req.user_id }).exec(); + + res.json(toObject(channels)); +}); + +router.post("/", check(DmChannelCreateSchema), async (req: Request, res: Response) => { + const body = req.body as DmChannelCreateSchema; + + body.recipients = body.recipients.filter((x) => x !== req.user_id).unique(); + + if (!(await Promise.all(body.recipients.map((x) => UserModel.exists({ id: x })))).every((x) => x)) { + throw new HTTPError("Recipient not found"); + } + + const type = body.recipients.length === 1 ? ChannelType.DM : ChannelType.GROUP_DM; + const name = trimSpecial(body.name); + + const channel = await new ChannelModel({ + name, + type, + owner_id: req.user_id, + id: Snowflake.generate(), + created_at: new Date(), + last_message_id: null, + recipient_ids: [...body.recipients, req.user_id] + }).save(); + + await emitEvent({ event: "CHANNEL_CREATE", data: toObject(channel), user_id: req.user_id } as ChannelCreateEvent); + + res.json(toObject(channel)); +}); + +export default router;