diff options
author | Madeline <46743919+MaddyUnderStars@users.noreply.github.com> | 2023-01-20 18:10:47 +1100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-20 18:10:47 +1100 |
commit | 084dc0be08555891cad4c2bb984822a62ec5ec9f (patch) | |
tree | ed2ca0fafefa2224ae32761f955f63935422a97d /src/gateway/opcodes/LazyRequest.ts | |
parent | fix: route file regex (#956) (diff) | |
download | server-084dc0be08555891cad4c2bb984822a62ec5ec9f.tar.xz |
Add ESLint (#941)
* Add eslint, switch to lint-staged for precommit * Fix all ESLint errors * Update GH workflow to check prettier and eslint
Diffstat (limited to 'src/gateway/opcodes/LazyRequest.ts')
-rw-r--r-- | src/gateway/opcodes/LazyRequest.ts | 67 |
1 files changed, 32 insertions, 35 deletions
diff --git a/src/gateway/opcodes/LazyRequest.ts b/src/gateway/opcodes/LazyRequest.ts index d4b612b8..93524058 100644 --- a/src/gateway/opcodes/LazyRequest.ts +++ b/src/gateway/opcodes/LazyRequest.ts @@ -46,24 +46,25 @@ async function getMembers(guild_id: string, range: [number, number]) { let members: Member[] = []; try { - members = await getDatabase()! - .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("user.settings") - .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(); + members = + (await getDatabase() + ?.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("user.settings") + .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); } @@ -77,7 +78,7 @@ async function getMembers(guild_id: string, range: [number, number]) { }; } - const groups = [] as any[]; + const groups = []; const items = []; const member_roles = members .map((m) => m.roles) @@ -93,10 +94,9 @@ async function getMembers(guild_id: string, range: [number, number]) { const offlineItems = []; for (const role of member_roles) { - // @ts-ignore - const [role_members, other_members]: Member[][] = partition( + const [role_members, other_members] = partition( members, - (m: Member) => m.roles.find((r) => r.id === role.id), + (m: Member) => !!m.roles.find((r) => r.id === role.id), ); const group = { count: role_members.length, @@ -126,7 +126,7 @@ async function getMembers(guild_id: string, range: [number, number]) { (a.activities.length - b.activities.length) * 2 ); }); - var session: Session | undefined = sessions.first(); + const session: Session | undefined = sessions.first(); if (session?.status == "offline") { session.status = member?.user?.settings?.status || "online"; @@ -189,7 +189,9 @@ async function getMembers(guild_id: string, range: [number, number]) { export async function onLazyRequest(this: WebSocket, { d }: Payload) { // TODO: check data check.call(this, LazyRequestSchema, d); + // eslint-disable-next-line @typescript-eslint/no-unused-vars const { guild_id, typing, channels, activities } = d as LazyRequestSchema; + if (!channels) throw new Error("Must provide channel ranges"); const channel_id = Object.keys(channels || {}).first(); if (!channel_id) return; @@ -197,7 +199,7 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) { const permissions = await getPermission(this.user_id, guild_id, channel_id); permissions.hasThrow("VIEW_CHANNEL"); - const ranges = channels![channel_id]; + const ranges = channels[channel_id]; if (!Array.isArray(ranges)) throw new Error("Not a valid Array"); const member_count = await Member.count({ where: { guild_id } }); @@ -244,15 +246,10 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) { }); } -function partition<T>(array: T[], isValid: Function) { - // @ts-ignore - return array.reduce( - // @ts-ignore - ([pass, fail], elem) => { - return isValid(elem) - ? [[...pass, elem], fail] - : [pass, [...fail, elem]]; - }, - [[], []], - ); +/* https://stackoverflow.com/a/50636286 */ +function partition<T>(array: T[], filter: (elem: T) => boolean) { + const pass: T[] = [], + fail: T[] = []; + array.forEach((e) => (filter(e) ? pass : fail).push(e)); + return [pass, fail]; } |