diff options
author | AlTech98 <altech123159@gmail.com> | 2021-09-16 21:33:36 +0200 |
---|---|---|
committer | AlTech98 <altech123159@gmail.com> | 2021-09-16 21:33:36 +0200 |
commit | 4f87fd742f25a25782afb20131200bcff81b50f7 (patch) | |
tree | 9588f6319e6b21eb4415e796a690a57b6e7ecb50 /api/src/routes/users/@me/channels.ts | |
parent | Dummy sticker-packs routes (diff) | |
download | server-4f87fd742f25a25782afb20131200bcff81b50f7.tar.xz |
Implemented DMs and group DMs
Diffstat (limited to 'api/src/routes/users/@me/channels.ts')
-rw-r--r-- | api/src/routes/users/@me/channels.ts | 43 |
1 files changed, 13 insertions, 30 deletions
diff --git a/api/src/routes/users/@me/channels.ts b/api/src/routes/users/@me/channels.ts index da33f204..bd7af18c 100644 --- a/api/src/routes/users/@me/channels.ts +++ b/api/src/routes/users/@me/channels.ts @@ -1,15 +1,21 @@ -import { Router, Request, Response } from "express"; -import { Channel, ChannelCreateEvent, ChannelType, Snowflake, trimSpecial, User, emitEvent, Recipient } from "@fosscord/util"; -import { HTTPError } from "lambert-server"; +import { Request, Response, Router } from "express"; +import { PublicUserProjection, Recipient, User, ChannelService } from "@fosscord/util"; import { route } from "@fosscord/api"; -import { In } from "typeorm"; const router: Router = Router(); router.get("/", route({}), async (req: Request, res: Response) => { - const recipients = await Recipient.find({ where: { user_id: req.user_id }, relations: ["channel"] }); + const recipients = await Recipient.find({ where: { user_id: req.user_id }, relations: ["channel", "user"] }); - res.json(recipients.map((x) => x.channel)); + //TODO check if this is right + const aa = await Promise.all(recipients.map(async (x) => { + return { + ...(x.channel), + recipients: await User.findOneOrFail({ where: { id: x.user_id }, select: PublicUserProjection }), + } + })) + + res.json(aa); }); export interface DmChannelCreateSchema { @@ -19,30 +25,7 @@ export interface DmChannelCreateSchema { router.post("/", route({ body: "DmChannelCreateSchema" }), async (req: Request, res: Response) => { const body = req.body as DmChannelCreateSchema; - - body.recipients = body.recipients.filter((x) => x !== req.user_id).unique(); - - const recipients = await User.find({ where: body.recipients.map((x) => ({ id: x })) }); - - if (recipients.length !== body.recipients.length) { - throw new HTTPError("Recipient/s not found"); - } - - const type = body.recipients.length === 1 ? ChannelType.DM : ChannelType.GROUP_DM; - const name = trimSpecial(body.name); - - const channel = await new Channel({ - name, - type, - // owner_id only for group dm channels - created_at: new Date(), - last_message_id: null, - recipients: [...body.recipients.map((x) => new Recipient({ user_id: x })), new Recipient({ user_id: req.user_id })] - }).save(); - - await emitEvent({ event: "CHANNEL_CREATE", data: channel, user_id: req.user_id } as ChannelCreateEvent); - - res.json(channel); + res.json(await ChannelService.createDMChannel(body.recipients, req.user_id, body.name)); }); export default router; |