summary refs log tree commit diff
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-08-02 23:18:11 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-08-02 23:18:11 +1000
commit62bad5a60d97ab14527ccaa5f59c7af292d51b3c (patch)
treede17103cd719a772ca9e0ce66483657d4b9673aa
parentFix before and after queries doing gt and lt comparisions incorrectly (diff)
downloadserver-62bad5a60d97ab14527ccaa5f59c7af292d51b3c.tar.xz
Fix LazyRequest throwing when no offline group exists
Wrap getRepository in try catch
-rw-r--r--gateway/src/opcodes/LazyRequest.ts48
1 files changed, 30 insertions, 18 deletions
diff --git a/gateway/src/opcodes/LazyRequest.ts b/gateway/src/opcodes/LazyRequest.ts
index 01276738..0db35286 100644
--- a/gateway/src/opcodes/LazyRequest.ts
+++ b/gateway/src/opcodes/LazyRequest.ts
@@ -18,22 +18,34 @@ async function getMembers(guild_id: string, range: [number, number]) {
 	}
 	// TODO: wait for typeorm to implement ordering for .find queries https://github.com/typeorm/typeorm/issues/2620
 
-	let members = await getRepository(Member)
-		.createQueryBuilder("member")
-		.where("member.guild_id = :guild_id", { guild_id })
-		.leftJoinAndSelect("member.roles", "role")
-		.leftJoinAndSelect("member.user", "user")
-		.leftJoinAndSelect("user.sessions", "session")
-		.addSelect(
-			"CASE WHEN session.status = 'offline' THEN 0 ELSE 1 END",
-			"_status"
-		)
-		.orderBy("role.position", "DESC")
-		.addOrderBy("_status", "DESC")
-		.addOrderBy("user.username", "ASC")
-		.offset(Number(range[0]) || 0)
-		.limit(Number(range[1]) || 100)
-		.getMany();
+	let members;
+	try {
+		members = await getRepository(Member)
+			.createQueryBuilder("member")
+			.where("member.guild_id = :guild_id", { guild_id })
+			.leftJoinAndSelect("member.roles", "role")
+			.leftJoinAndSelect("member.user", "user")
+			.leftJoinAndSelect("user.sessions", "session")
+			.addSelect(
+				"CASE WHEN session.status = 'offline' THEN 0 ELSE 1 END",
+				"_status"
+			)
+			.orderBy("role.position", "DESC")
+			.addOrderBy("_status", "DESC")
+			.addOrderBy("user.username", "ASC")
+			.offset(Number(range[0]) || 0)
+			.limit(Number(range[1]) || 100)
+			.getMany();
+	}
+	catch (e) {
+		console.error(`LazyRequest`, e);
+		return {
+			items: [],
+			groups: [],
+			range: [],
+			members: [],
+		}
+	}		
 
 	const groups = [] as any[];
 	const items = [];
@@ -79,7 +91,7 @@ async function getMembers(guild_id: string, range: [number, number]) {
 			};
 
 			if (!session || session.status == "invisible") {
-				item.member.presence.status = "offline"; 
+				item.member.presence.status = "offline";
 				offlineItems.push(item);
 				group.count--;
 				continue;
@@ -155,7 +167,7 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) {
 				op: "SYNC",
 				range: x.range,
 			})),
-			online_count: member_count - groups.find(x => x.id == "offline").count,
+			online_count: member_count - (groups.find(x => x.id == "offline")?.count ?? 0),
 			member_count,
 			id: "everyone",
 			guild_id,