summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-31 17:58:47 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-08-31 17:58:47 +0200
commit3f66c101789f9a6d065490d3e3265c764698113f (patch)
tree97b1ef821e2dd8124e5d38a822c08efe21e7eaa2
parent:sparkles: channel recipients (diff)
downloadserver-3f66c101789f9a6d065490d3e3265c764698113f.tar.xz
:bug: db query fixes
-rw-r--r--Status.ts7
-rw-r--r--api/package-lock.json18
-rw-r--r--api/package.json2
-rw-r--r--api/src/middlewares/Authentication.ts8
-rw-r--r--api/src/routes/channels/#channel_id/messages/index.ts36
-rw-r--r--api/src/routes/channels/#channel_id/pins.ts2
-rw-r--r--api/src/routes/channels/#channel_id/typing.ts2
-rw-r--r--api/src/routes/users/@me/disable.ts2
-rw-r--r--api/src/routes/users/@me/index.ts2
-rw-r--r--api/src/routes/users/@me/relationships.ts12
-rw-r--r--api/src/schema/Message.ts2
-rw-r--r--bundle/package-lock.json13
-rw-r--r--bundle/package.json1
13 files changed, 59 insertions, 48 deletions
diff --git a/Status.ts b/Status.ts
deleted file mode 100644
index c4dab586..00000000
--- a/Status.ts
+++ /dev/null
@@ -1,7 +0,0 @@
-export type Status = "idle" | "dnd" | "online" | "offline";
-
-export interface ClientStatus {
-	desktop?: string; // e.g. Windows/Linux/Mac
-	mobile?: string; // e.g. iOS/Android
-	web?: string; // e.g. browser, bot account
-}
diff --git a/api/package-lock.json b/api/package-lock.json
index 758e51c8..63724688 100644
--- a/api/package-lock.json
+++ b/api/package-lock.json
@@ -60,7 +60,7 @@
 				"saslprep": "^1.0.3",
 				"ts-node": "^9.1.1",
 				"ts-node-dev": "^1.1.6",
-				"typescript": "^4.1.2"
+				"typescript": "^4.4.2"
 			}
 		},
 		"../util": {
@@ -83,7 +83,7 @@
 				"reflect-metadata": "^0.1.13",
 				"sqlite3": "^5.0.2",
 				"typeorm": "^0.2.37",
-				"typescript": "^4.3.5",
+				"typescript": "^4.4.2",
 				"typescript-json-schema": "^0.50.1"
 			},
 			"devDependencies": {
@@ -11430,9 +11430,9 @@
 			}
 		},
 		"node_modules/typescript": {
-			"version": "4.3.5",
-			"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.3.5.tgz",
-			"integrity": "sha512-DqQgihaQ9cUrskJo9kIyW/+g0Vxsk8cDtZ52a3NGh0YNTfpUSArXSohyUGnvbPazEPLu398C0UxmKSOrPumUzA==",
+			"version": "4.4.2",
+			"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.2.tgz",
+			"integrity": "sha512-gzP+t5W4hdy4c+68bfcv0t400HVJMMd2+H9B7gae1nQlBzCqvrXX+6GL/b3GAgyTH966pzrZ70/fRjwAtZksSQ==",
 			"dev": true,
 			"bin": {
 				"tsc": "bin/tsc",
@@ -12637,7 +12637,7 @@
 				"reflect-metadata": "^0.1.13",
 				"sqlite3": "^5.0.2",
 				"typeorm": "^0.2.37",
-				"typescript": "^4.3.5",
+				"typescript": "^4.4.2",
 				"typescript-json-schema": "^0.50.1"
 			}
 		},
@@ -21387,9 +21387,9 @@
 			}
 		},
 		"typescript": {
-			"version": "4.3.5",
-			"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.3.5.tgz",
-			"integrity": "sha512-DqQgihaQ9cUrskJo9kIyW/+g0Vxsk8cDtZ52a3NGh0YNTfpUSArXSohyUGnvbPazEPLu398C0UxmKSOrPumUzA==",
+			"version": "4.4.2",
+			"resolved": "https://registry.npmjs.org/typescript/-/typescript-4.4.2.tgz",
+			"integrity": "sha512-gzP+t5W4hdy4c+68bfcv0t400HVJMMd2+H9B7gae1nQlBzCqvrXX+6GL/b3GAgyTH966pzrZ70/fRjwAtZksSQ==",
 			"dev": true
 		},
 		"umd": {
diff --git a/api/package.json b/api/package.json
index b8bb4d45..37db6ff3 100644
--- a/api/package.json
+++ b/api/package.json
@@ -52,7 +52,7 @@
 		"saslprep": "^1.0.3",
 		"ts-node": "^9.1.1",
 		"ts-node-dev": "^1.1.6",
-		"typescript": "^4.1.2"
+		"typescript": "^4.4.2"
 	},
 	"dependencies": {
 		"@fosscord/util": "file:../util",
diff --git a/api/src/middlewares/Authentication.ts b/api/src/middlewares/Authentication.ts
index 34a66a6b..a300c786 100644
--- a/api/src/middlewares/Authentication.ts
+++ b/api/src/middlewares/Authentication.ts
@@ -18,9 +18,9 @@ export const API_PREFIX_TRAILING_SLASH = /^\/api(\/v\d+)?\//;
 declare global {
 	namespace Express {
 		interface Request {
-			user_id: any;
+			user_id: string;
 			user_bot: boolean;
-			token: any;
+			token: string;
 		}
 	}
 }
@@ -47,7 +47,7 @@ export async function Authentication(req: Request, res: Response, next: NextFunc
 		req.user_id = decoded.id;
 		req.user_bot = user.bot;
 		return next();
-	} catch (error) {
-		return next(new HTTPError(error.toString(), 400));
+	} catch (error: any) {
+		return next(new HTTPError(error?.toString(), 400));
 	}
 }
diff --git a/api/src/routes/channels/#channel_id/messages/index.ts b/api/src/routes/channels/#channel_id/messages/index.ts
index 17944548..86de6de8 100644
--- a/api/src/routes/channels/#channel_id/messages/index.ts
+++ b/api/src/routes/channels/#channel_id/messages/index.ts
@@ -31,10 +31,7 @@ export function isTextChannel(type: ChannelType): boolean {
 // get messages
 router.get("/", async (req: Request, res: Response) => {
 	const channel_id = req.params.channel_id;
-	const channel = await Channel.findOneOrFail(
-		{ id: channel_id },
-		{ select: ["guild_id", "type", "permission_overwrites", "recipient_ids", "owner_id"] }
-	); // lean is needed, because we don't want to populate .recipients that also auto deletes .recipient_ids
+	const channel = await Channel.findOneOrFail({ id: channel_id });
 	if (!channel) throw new HTTPError("Channel not found", 404);
 
 	isTextChannel(channel.type);
@@ -56,7 +53,12 @@ router.get("/", async (req: Request, res: Response) => {
 	permissions.hasThrow("VIEW_CHANNEL");
 	if (!permissions.has("READ_MESSAGE_HISTORY")) return res.json([]);
 
-	var query: FindManyOptions<Message> & { where: { id?: any } } = { order: { id: "DESC" }, take: limit, where: { channel_id } };
+	var query: FindManyOptions<Message> & { where: { id?: any } } = {
+		order: { id: "DESC" },
+		take: limit,
+		where: { channel_id },
+		relations: ["author", "webhook", "application", "mentions", "mention_roles", "mention_channels", "sticker_items", "attachments"]
+	};
 
 	if (after) query.where.id = MoreThan(after);
 	else if (before) query.where.id = LessThan(before);
@@ -69,18 +71,20 @@ router.get("/", async (req: Request, res: Response) => {
 
 	const messages = await Message.find(query);
 
-	return res.json(messages).map((x) => {
-		(x.reactions || []).forEach((x: any) => {
+	return res.json(
+		messages.map((x) => {
+			(x.reactions || []).forEach((x: any) => {
+				// @ts-ignore
+				if ((x.user_ids || []).includes(req.user_id)) x.me = true;
+				// @ts-ignore
+				delete x.user_ids;
+			});
 			// @ts-ignore
-			if ((x.user_ids || []).includes(req.user_id)) x.me = true;
-			// @ts-ignore
-			delete x.user_ids;
-		});
-		// @ts-ignore
-		if (!x.author) x.author = { discriminator: "0000", username: "Deleted User", public_flags: "0", avatar: null };
+			if (!x.author) x.author = { discriminator: "0000", username: "Deleted User", public_flags: "0", avatar: null };
 
-		return x;
-	});
+			return x;
+		})
+	);
 });
 
 // TODO: config max upload size
@@ -136,5 +140,5 @@ router.post("/", messageUpload.single("file"), async (req: Request, res: Respons
 		edited_timestamp: undefined
 	});
 
-	return res.send(data);
+	return res.json(data);
 });
diff --git a/api/src/routes/channels/#channel_id/pins.ts b/api/src/routes/channels/#channel_id/pins.ts
index d83e36ed..96a3fdbf 100644
--- a/api/src/routes/channels/#channel_id/pins.ts
+++ b/api/src/routes/channels/#channel_id/pins.ts
@@ -14,7 +14,7 @@ router.put("/:message_id", async (req: Request, res: Response) => {
 	// * in dm channels anyone can pin messages -> only check for guilds
 	if (message.guild_id) permission.hasThrow("MANAGE_MESSAGES");
 
-	const pinned_count = await Message.count({ channel_id, pinned: true });
+	const pinned_count = await Message.count({ channel: { id: channel_id }, pinned: true });
 	const { maxPins } = Config.get().limits.channel;
 	if (pinned_count >= maxPins) throw new HTTPError("Max pin count reached: " + maxPins);
 
diff --git a/api/src/routes/channels/#channel_id/typing.ts b/api/src/routes/channels/#channel_id/typing.ts
index 2305e8e8..f1fb3c86 100644
--- a/api/src/routes/channels/#channel_id/typing.ts
+++ b/api/src/routes/channels/#channel_id/typing.ts
@@ -17,7 +17,7 @@ router.post("/", async (req: Request, res: Response) => {
 		channel_id: channel_id,
 		data: {
 			// this is the paylod
-			member: { ...member, roles: member.role_ids },
+			member: { ...member, roles: member.roles.map((x) => x.id) },
 			channel_id,
 			timestamp,
 			user_id,
diff --git a/api/src/routes/users/@me/disable.ts b/api/src/routes/users/@me/disable.ts
index ed1dedcc..7b8a130c 100644
--- a/api/src/routes/users/@me/disable.ts
+++ b/api/src/routes/users/@me/disable.ts
@@ -5,7 +5,7 @@ import bcrypt from "bcrypt";
 const router = Router();
 
 router.post("/", async (req: Request, res: Response) => {
-	const user = await User.findOneOrFail(req.user_id); //User object
+	const user = await User.findOneOrFail({ id: req.user_id }); //User object
 	let correctpass = true;
 
 	if (user.data.hash) {
diff --git a/api/src/routes/users/@me/index.ts b/api/src/routes/users/@me/index.ts
index 274cfb24..d5a5723c 100644
--- a/api/src/routes/users/@me/index.ts
+++ b/api/src/routes/users/@me/index.ts
@@ -37,7 +37,7 @@ router.patch("/", check(UserModifySchema), async (req: Request, res: Response) =
 	if (body.avatar) body.avatar = await handleFile(`/avatars/${req.user_id}`, body.avatar as string);
 	if (body.banner) body.banner = await handleFile(`/banners/${req.user_id}`, body.banner as string);
 
-	const user = await new User({ ...body }, { id: req.user_id }).save();
+	const user = await new User({ ...body, id: req.user_id }).save();
 	// TODO: dispatch user update event
 
 	res.json(user);
diff --git a/api/src/routes/users/@me/relationships.ts b/api/src/routes/users/@me/relationships.ts
index 1a89b110..0b864d88 100644
--- a/api/src/routes/users/@me/relationships.ts
+++ b/api/src/routes/users/@me/relationships.ts
@@ -26,7 +26,7 @@ async function updateRelationship(req: Request, res: Response, friend: User, typ
 	const id = friend.id;
 	if (id === req.user_id) throw new HTTPError("You can't add yourself as a friend");
 
-	const user = await User.findOneOrFail(req.user_id, { relations: ["relationships"], select: userProjection });
+	const user = await User.findOneOrFail({ id: req.user_id }, { relations: ["relationships"], select: userProjection });
 
 	var relationship = user.relationships.find((x) => x.id === id);
 	const friendRequest = friend.relationships.find((x) => x.id === req.user_id);
@@ -67,8 +67,8 @@ async function updateRelationship(req: Request, res: Response, friend: User, typ
 		return res.sendStatus(204);
 	}
 
-	var incoming_relationship = new Relationship({ nickname: undefined, type: RelationshipType.incoming }, { id: req.user_id });
-	var outgoing_relationship = new Relationship({ nickname: undefined, type: RelationshipType.outgoing }, { id });
+	var incoming_relationship = new Relationship({ nickname: undefined, type: RelationshipType.incoming, id: req.user_id });
+	var outgoing_relationship = new Relationship({ nickname: undefined, type: RelationshipType.outgoing, id });
 
 	if (friendRequest) {
 		if (friendRequest.type === RelationshipType.blocked) throw new HTTPError("The user blocked you");
@@ -113,7 +113,7 @@ router.put("/:id", check({ $type: new Length(Number, 1, 4) }), async (req: Reque
 	return await updateRelationship(
 		req,
 		res,
-		await User.findOneOrFail(req.params.id, { relations: ["relationships"], select: userProjection }),
+		await User.findOneOrFail({ id: req.params.id }, { relations: ["relationships"], select: userProjection }),
 		req.body.type
 	);
 });
@@ -135,8 +135,8 @@ router.delete("/:id", async (req: Request, res: Response) => {
 	const { id } = req.params;
 	if (id === req.user_id) throw new HTTPError("You can't remove yourself as a friend");
 
-	const user = await User.findOneOrFail(req.user_id, { select: userProjection, relations: ["relationships"] });
-	const friend = await User.findOneOrFail(id, { select: userProjection, relations: ["relationships"] });
+	const user = await User.findOneOrFail({ id: req.user_id }, { select: userProjection, relations: ["relationships"] });
+	const friend = await User.findOneOrFail({ id: id }, { select: userProjection, relations: ["relationships"] });
 
 	const relationship = user.relationships.find((x) => x.id === id);
 	const friendRequest = friend.relationships.find((x) => x.id === req.user_id);
diff --git a/api/src/schema/Message.ts b/api/src/schema/Message.ts
index bf10c037..742542df 100644
--- a/api/src/schema/Message.ts
+++ b/api/src/schema/Message.ts
@@ -81,7 +81,7 @@ export interface MessageCreateSchema {
 		message_id: string;
 		channel_id: string;
 		guild_id?: string;
-		fail_if_not_exists: boolean;
+		fail_if_not_exists?: boolean;
 	};
 	payload_json?: string;
 	file?: any;
diff --git a/bundle/package-lock.json b/bundle/package-lock.json
index 9fcb5490..59fd96ee 100644
--- a/bundle/package-lock.json
+++ b/bundle/package-lock.json
@@ -16,6 +16,7 @@
 				"@fosscord/util": "file:../util",
 				"async-exit-hook": "^2.0.1",
 				"express": "^4.17.1",
+				"missing-native-js-functions": "^1.2.13",
 				"mongodb-memory-server": "^7.3.6",
 				"node-os-utils": "^1.3.5"
 			},
@@ -70,6 +71,7 @@
 				"mongoose-long": "^0.3.2",
 				"multer": "^1.4.2",
 				"node-fetch": "^2.6.1",
+				"supertest": "^6.1.6",
 				"typeorm": "^0.2.37"
 			},
 			"devDependencies": {
@@ -1224,6 +1226,11 @@
 				"node": "*"
 			}
 		},
+		"node_modules/missing-native-js-functions": {
+			"version": "1.2.13",
+			"resolved": "https://registry.npmjs.org/missing-native-js-functions/-/missing-native-js-functions-1.2.13.tgz",
+			"integrity": "sha512-1RAArfUkrGkj5N3xJVW251F2PvfP2ozAcxsLLDR6uiiAixTP5Abh8zzGMadepbqgiHC0FGlTSAUNbh9abN4Osg=="
+		},
 		"node_modules/mkdirp": {
 			"version": "1.0.4",
 			"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz",
@@ -1916,6 +1923,7 @@
 				"multer": "^1.4.2",
 				"node-fetch": "^2.6.1",
 				"saslprep": "^1.0.3",
+				"supertest": "^6.1.6",
 				"ts-node": "^9.1.1",
 				"ts-node-dev": "^1.1.6",
 				"typeorm": "^0.2.37",
@@ -2800,6 +2808,11 @@
 				"brace-expansion": "^1.1.7"
 			}
 		},
+		"missing-native-js-functions": {
+			"version": "1.2.13",
+			"resolved": "https://registry.npmjs.org/missing-native-js-functions/-/missing-native-js-functions-1.2.13.tgz",
+			"integrity": "sha512-1RAArfUkrGkj5N3xJVW251F2PvfP2ozAcxsLLDR6uiiAixTP5Abh8zzGMadepbqgiHC0FGlTSAUNbh9abN4Osg=="
+		},
 		"mkdirp": {
 			"version": "1.0.4",
 			"resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-1.0.4.tgz",
diff --git a/bundle/package.json b/bundle/package.json
index 71d7efea..63945135 100644
--- a/bundle/package.json
+++ b/bundle/package.json
@@ -51,6 +51,7 @@
 		"@fosscord/util": "file:../util",
 		"async-exit-hook": "^2.0.1",
 		"express": "^4.17.1",
+		"missing-native-js-functions": "^1.2.13",
 		"mongodb-memory-server": "^7.3.6",
 		"node-os-utils": "^1.3.5"
 	}