1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-23 10:52:30 +01:00

Fix LazyRequest throwing when no offline group exists

Wrap getRepository in try catch
This commit is contained in:
Madeline 2022-08-02 23:18:11 +10:00
parent e83786a5b6
commit 62bad5a60d
No known key found for this signature in database
GPG Key ID: 1958E017C36F2E47

View File

@ -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 = [];
@ -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,