summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/routes/users/@me/channels.ts32
-rw-r--r--src/routes/users/@me/delete.ts7
-rw-r--r--src/schema/Channel.ts12
-rw-r--r--src/util/Channel.ts2
4 files changed, 29 insertions, 24 deletions
diff --git a/src/routes/users/@me/channels.ts b/src/routes/users/@me/channels.ts
index 3b2b6781..4cab869b 100644
--- a/src/routes/users/@me/channels.ts
+++ b/src/routes/users/@me/channels.ts
@@ -2,50 +2,52 @@ import { Router, Request, Response } from "express";
 import {
 	ChannelModel,
 	ChannelCreateEvent,
-	DMChannel,
-	UserModel,
 	toObject,
 	ChannelType,
 	Snowflake,
 	trimSpecial,
+	Channel,
+	DMChannel,
+	UserModel
 } 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();
+	var channels = await ChannelModel.find({ recipient_ids: req.user_id }).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");
+
+	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 = {
+	const channel = await new ChannelModel({
 		name,
 		type,
 		owner_id: req.user_id,
 		id: Snowflake.generate(),
 		created_at: new Date(),
-	};
-	await new ChannelModel(channel).save();
+		last_message_id: null,
+		recipient_ids: [...body.recipients, req.user_id]
+	}).save();
 
-	/*Event({ event: "CHANNEL_CREATE", data: channel } as ChannelCreateEvent);*/
+	await emitEvent({ event: "CHANNEL_CREATE", data: toObject(channel), user_id: req.user_id } as ChannelCreateEvent);
 
-	res.json(channel);
+	res.json(toObject(channel));
 });
 
 export default router;
diff --git a/src/routes/users/@me/delete.ts b/src/routes/users/@me/delete.ts
index aef4006c..edda8e2d 100644
--- a/src/routes/users/@me/delete.ts
+++ b/src/routes/users/@me/delete.ts
@@ -1,5 +1,5 @@
 import { Router, Request, Response } from "express";
-import { UserModel } from "@fosscord/server-util";
+import { GuildModel, MemberModel, UserModel } from "@fosscord/server-util";
 import bcrypt from "bcrypt";
 const router = Router();
 
@@ -8,7 +8,10 @@ router.post("/", async (req: Request, res: Response) => {
 
 	let correctpass = await bcrypt.compare(req.body.password, user!.user_data.hash); //Not sure if user typed right password :/
 	if (correctpass) {
-		await UserModel.deleteOne({ id: req.user_id }).exec(); //Yeetus user deletus
+		await Promise.all([
+			UserModel.deleteOne({ id: req.user_id }).exec(), //Yeetus user deletus
+			MemberModel.deleteMany({ id: req.user_id }).exec()
+		]);
 
 		res.sendStatus(204);
 	} else {
diff --git a/src/schema/Channel.ts b/src/schema/Channel.ts
index a89d5fb3..2cb7f7f4 100644
--- a/src/schema/Channel.ts
+++ b/src/schema/Channel.ts
@@ -14,16 +14,16 @@ export const ChannelModifySchema = {
 			id: String,
 			type: new Length(Number, 0, 1), // either 0 (role) or 1 (member)
 			allow: BigInt,
-			deny: BigInt,
-		},
+			deny: BigInt
+		}
 	],
 	$parent_id: String,
-	$nsfw: Boolean,
+	$nsfw: Boolean
 };
 
 export const DmChannelCreateSchema = {
 	$name: String,
-	recipients: [String],
+	recipients: new Length([String], 1, 10)
 };
 
 export interface DmChannelCreateSchema {
@@ -52,8 +52,8 @@ export interface ChannelModifySchema {
 export const ChannelGuildPositionUpdateSchema = [
 	{
 		id: String,
-		$position: Number,
-	},
+		$position: Number
+	}
 ];
 
 export type ChannelGuildPositionUpdateSchema = {
diff --git a/src/util/Channel.ts b/src/util/Channel.ts
index c8df85bc..8dfc03bc 100644
--- a/src/util/Channel.ts
+++ b/src/util/Channel.ts
@@ -45,7 +45,7 @@ export async function createChannel(channel: Partial<TextChannel | VoiceChannel>
 		id: Snowflake.generate(),
 		created_at: new Date(),
 		// @ts-ignore
-		recipients: null
+		recipient_ids: null
 	}).save();
 
 	await emitEvent({ event: "CHANNEL_CREATE", data: channel, guild_id: channel.guild_id } as ChannelCreateEvent);