summary refs log tree commit diff
path: root/api/src/routes/users/@me/channels.ts
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-24 10:43:43 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-24 10:43:43 +0200
commit9abb04d2e6fcc2567ddafc2dc753cbb0c79ca930 (patch)
treea9ce1cd54d5dd50023e1a1c5d0a494802f37b5e6 /api/src/routes/users/@me/channels.ts
parent:sparkles: added User flags (diff)
parent:art: remove start from setup script (diff)
downloadserver-ts-9abb04d2e6fcc2567ddafc2dc753cbb0c79ca930.tar.xz
Merge branch 'master' of https://github.com/fosscord/fosscord-server
Diffstat (limited to 'api/src/routes/users/@me/channels.ts')
-rw-r--r--api/src/routes/users/@me/channels.ts50
1 files changed, 13 insertions, 37 deletions
diff --git a/api/src/routes/users/@me/channels.ts b/api/src/routes/users/@me/channels.ts

index 6fd396b8..b5782eca 100644 --- a/api/src/routes/users/@me/channels.ts +++ b/api/src/routes/users/@me/channels.ts
@@ -1,46 +1,22 @@ -import { Router, Request, Response } from "express"; -import { Channel, ChannelCreateEvent, ChannelType, Snowflake, trimSpecial, User, emitEvent } from "@fosscord/util"; -import { HTTPError } from "lambert-server"; - -import { DmChannelCreateSchema } from "../../../schema/Channel"; -import { check } from "../../../util/instanceOf"; -import { In } from "typeorm"; -import { Recipient } from "../../../../../util/dist/entities/Recipient"; +import { Request, Response, Router } from "express"; +import { Recipient, DmChannelDTO, Channel } from "@fosscord/util"; +import { route } from "@fosscord/api"; const router: Router = Router(); -router.get("/", async (req: Request, res: Response) => { - const recipients = await Recipient.find({ where: { user_id: req.user_id }, relations: ["channel"] }); - - res.json(recipients.map((x) => x.channel)); +router.get("/", route({}), async (req: Request, res: Response) => { + const recipients = await Recipient.find({ where: { user_id: req.user_id, closed: false }, relations: ["channel", "channel.recipients"] }); + res.json(await Promise.all(recipients.map(r => DmChannelDTO.from(r.channel, [req.user_id])))); }); -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(); - - const recipients = await User.find({ id: In(body.recipients) }); +export interface DmChannelCreateSchema { + name?: string; + recipients: string[]; +} - 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: req.user_id, - created_at: new Date(), - last_message_id: null, - recipients: [...body.recipients.map((x) => new Recipient({ id: x })), new Recipient({ id: req.user_id })] - }).save(); - - await emitEvent({ event: "CHANNEL_CREATE", data: channel, user_id: req.user_id } as ChannelCreateEvent); - - res.json(channel); +router.post("/", route({ body: "DmChannelCreateSchema" }), async (req: Request, res: Response) => { + const body = req.body as DmChannelCreateSchema; + res.json(await Channel.createDMChannel(body.recipients, req.user_id, body.name)); }); export default router;