summary refs log tree commit diff
path: root/gateway
diff options
context:
space:
mode:
Diffstat (limited to 'gateway')
-rw-r--r--gateway/src/listener/listener.ts10
-rw-r--r--gateway/src/opcodes/Identify.ts43
-rw-r--r--gateway/src/opcodes/LazyRequest.ts70
3 files changed, 52 insertions, 71 deletions
diff --git a/gateway/src/listener/listener.ts b/gateway/src/listener/listener.ts
index 51082586..633580e3 100644
--- a/gateway/src/listener/listener.ts
+++ b/gateway/src/listener/listener.ts
@@ -1,10 +1,10 @@
 import {
 	db,
 	Event,
-	UserModel,
+	User,
 	getPermission,
 	Permissions,
-	ChannelModel,
+	Channel,
 	RabbitMQ,
 	EVENT,
 	listenEvent,
@@ -27,11 +27,11 @@ import { Channel } from "amqplib";
 
 // TODO: use already queried guilds/channels of Identify and don't fetch them again
 export async function setupListener(this: WebSocket) {
-	const user = await UserModel.findOne({ id: this.user_id }, { guilds: true }).exec();
-	const channels = await ChannelModel.find(
+	const user = await User.findOneOrFail({ id: this.user_id }, { guilds: true });
+	const channels = await Channel.find(
 		{ $or: [{ recipient_ids: this.user_id }, { guild_id: { $in: user.guilds } }] },
 		{ id: true, permission_overwrites: true }
-	).exec();
+	);
 	const dm_channels = channels.filter((x) => !x.guild_id);
 	const guild_channels = channels.filter((x) => x.guild_id);
 
diff --git a/gateway/src/opcodes/Identify.ts b/gateway/src/opcodes/Identify.ts
index 91f7f675..464d584c 100644
--- a/gateway/src/opcodes/Identify.ts
+++ b/gateway/src/opcodes/Identify.ts
@@ -1,18 +1,6 @@
 import { CLOSECODES, Payload, OPCODES } from "../util/Constants";
 import WebSocket from "../util/WebSocket";
-import {
-	ChannelModel,
-	checkToken,
-	GuildModel,
-	Intents,
-	MemberDocument,
-	MemberModel,
-	ReadyEventData,
-	UserModel,
-	toObject,
-	EVENTEnum,
-	Config,
-} from "@fosscord/util";
+import { Channel, checkToken, Guild, Intents, Member, ReadyEventData, User, EVENTEnum, Config } from "@fosscord/util";
 import { setupListener } from "../listener/listener";
 import { IdentifySchema } from "../schema/Identify";
 import { Send } from "../util/Send";
@@ -54,7 +42,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
 		}
 	}
 
-	const members = toObject(await MemberModel.find({ id: this.user_id }).exec());
+	const members = await Member.find({ id: this.user_id });
 	const merged_members = members.map((x: any) => {
 		const y = { ...x, user_id: x.id };
 		delete y.settings;
@@ -63,8 +51,8 @@ export async function onIdentify(this: WebSocket, data: Payload) {
 	}) as MemberDocument[][];
 	const user_guild_settings_entries = members.map((x) => x.settings);
 
-	const channels = await ChannelModel.find({ recipient_ids: this.user_id }).exec();
-	const user = await UserModel.findOne({ id: this.user_id }).exec();
+	const channels = await Channel.find({ recipient_ids: this.user_id });
+	const user = await User.findOneOrFail({ id: this.user_id });
 	if (!user) return this.close(CLOSECODES.Authentication_failed);
 
 	const public_user = {
@@ -76,10 +64,10 @@ export async function onIdentify(this: WebSocket, data: Payload) {
 		bot: user.bot,
 	};
 
-	const guilds = await GuildModel.find({ id: { $in: user.guilds } })
-		.populate({ path: "joined_at", match: { id: this.user_id } })
-		.exec();
-
+	const guilds = await Guild.find({ id: { $in: user.guilds } }).populate({
+		path: "joined_at",
+		match: { id: this.user_id },
+	});
 	const privateUser = {
 		avatar: user.avatar,
 		mobile: user.mobile,
@@ -104,9 +92,9 @@ export async function onIdentify(this: WebSocket, data: Payload) {
 	const d: ReadyEventData = {
 		v: 8,
 		user: privateUser,
-		user_settings: user.user_settings,
+		user_settings: user.settings,
 		// @ts-ignore
-		guilds: toObject(guilds).map((x) => {
+		guilds: guilds.map((x) => {
 			// @ts-ignore
 			x.guild_hashes = {
 				channels: { omitted: false, hash: "y4PV2fZ0gmo" },
@@ -131,7 +119,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
 			version: 642,
 		},
 		// @ts-ignore
-		private_channels: toObject(channels).map((x: ChannelDocument) => {
+		private_channels: channels.map((x): ChannelDocument => {
 			x.recipient_ids = x.recipients.map((y: any) => y.id);
 			delete x.recipients;
 			return x;
@@ -144,17 +132,12 @@ export async function onIdentify(this: WebSocket, data: Payload) {
 				consented: false, // TODO
 			},
 		},
-		country_code: user.user_settings.locale,
+		country_code: user.settings.locale,
 		friend_suggestion_count: 0, // TODO
 		// @ts-ignore
 		experiments: experiments, // TODO
 		guild_join_requests: [], // TODO what is this?
-		users: [
-			public_user,
-			...toObject(channels)
-				.map((x: any) => x.recipients)
-				.flat(),
-		].unique(), // TODO
+		users: [public_user, ...channels.map((x: any) => x.recipients).flat()].unique(), // TODO
 		merged_members: merged_members,
 		// shard // TODO: only for bots sharding
 		// application // TODO for applications
diff --git a/gateway/src/opcodes/LazyRequest.ts b/gateway/src/opcodes/LazyRequest.ts
index 63075e5a..9f514f5f 100644
--- a/gateway/src/opcodes/LazyRequest.ts
+++ b/gateway/src/opcodes/LazyRequest.ts
@@ -18,45 +18,43 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) {
 	permissions.hasThrow("VIEW_CHANNEL");
 
 	// MongoDB query to retrieve all hoisted roles and join them with the members and users collection
-	const roles = toObject(
-		await db
-			.collection("roles")
-			.aggregate([
-				{
-					$match: {
-						guild_id,
-						// hoist: true // TODO: also match @everyone role
-					},
+	const roles = await db
+		.collection("roles")
+		.aggregate([
+			{
+				$match: {
+					guild_id,
+					// hoist: true // TODO: also match @everyone role
 				},
-				{ $sort: { position: 1 } },
-				{
-					$lookup: {
-						from: "members",
-						let: { id: "$id" },
-						pipeline: [
-							{ $match: { $expr: { $in: ["$$id", "$roles"] } } },
-							{ $limit: 100 },
-							{
-								$lookup: {
-									from: "users",
-									let: { user_id: "$id" },
-									pipeline: [
-										{ $match: { $expr: { $eq: ["$id", "$$user_id"] } } },
-										{ $project: PublicUserProjection },
-									],
-									as: "user",
-								},
-							},
-							{
-								$unwind: "$user",
+			},
+			{ $sort: { position: 1 } },
+			{
+				$lookup: {
+					from: "members",
+					let: { id: "$id" },
+					pipeline: [
+						{ $match: { $expr: { $in: ["$$id", "$roles"] } } },
+						{ $limit: 100 },
+						{
+							$lookup: {
+								from: "users",
+								let: { user_id: "$id" },
+								pipeline: [
+									{ $match: { $expr: { $eq: ["$id", "$$user_id"] } } },
+									{ $project: PublicUserProjection },
+								],
+								as: "user",
 							},
-						],
-						as: "members",
-					},
+						},
+						{
+							$unwind: "$user",
+						},
+					],
+					as: "members",
 				},
-			])
-			.toArray()
-	);
+			},
+		])
+		.toArray();
 
 	const groups = roles.map((x) => ({ id: x.id === guild_id ? "online" : x.id, count: x.members.length }));
 	const member_count = roles.reduce((a, b) => b.members.length + a, 0);