Update LazyRequest.ts

This commit is contained in:
notsapinho 2021-05-27 09:56:28 -03:00 committed by GitHub
parent 910dd9a22b
commit b5bbc35905
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,5 +1,13 @@
// @ts-nocheck WIP // @ts-nocheck WIP
import { db, getPermission, MemberModel, MongooseCache, PublicUserProjection, RoleModel, toObject } from "@fosscord/server-util"; import {
db,
getPermission,
MemberModel,
MongooseCache,
PublicUserProjection,
RoleModel,
toObject,
} from "@fosscord/server-util";
import { LazyRequest } from "../schema/LazyRequest"; import { LazyRequest } from "../schema/LazyRequest";
import { OPCODES, Payload } from "../util/Constants"; import { OPCODES, Payload } from "../util/Constants";
import { Send } from "../util/Send"; import { Send } from "../util/Send";
@ -10,56 +18,58 @@ import { check } from "./instanceOf";
// TODO: config: if want to list all members (even those who are offline) sorted by role, or just those who are online // TODO: config: if want to list all members (even those who are offline) sorted by role, or just those who are online
export async function onLazyRequest(this: WebSocket, { d }: Payload) { export async function onLazyRequest(this: WebSocket, { d }: Payload) {
// TODO: check data // TODO: check data
check.call(this, LazyRequest, d); check.call(this, LazyRequest, d);
const { guild_id, typing, channels, activities } = d as LazyRequest; const { guild_id, typing, channels, activities } = d as LazyRequest;
const permissions = await getPermission(this.user_id, guild_id); const permissions = await getPermission(this.user_id, guild_id);
// MongoDB query to retrieve all hoisted roles and join them with the members and users collection // MongoDB query to retrieve all hoisted roles and join them with the members and users collection
const roles = toObject( const roles = toObject(
await db await db
.collection("roles") .collection("roles")
.aggregate([ .aggregate([
{ {
$match: { $match: {
guild_id guild_id,
// id: { $ne: guild_id } // hoist: true // TODO: also match @everyone role
// hoist: true // TODO: also match @everyone role },
} },
}, { $sort: { position: 1 } },
{ $sort: { position: 1 } }, {
{ $lookup: {
$lookup: { from: "members",
from: "members", let: { id: "$id" },
let: { id: "$id" }, pipeline: [
pipeline: [ { $match: { $expr: { $in: ["$$id", "$roles"] } } },
{ $match: { $expr: { $in: ["$$id", "$roles"] } } }, { $limit: 100 },
{ $limit: 100 }, {
{ $lookup: {
$lookup: { from: "users",
from: "users", let: { user_id: "$id" },
let: { user_id: "$id" }, pipeline: [
pipeline: [{ $match: { $expr: { $eq: ["$id", "$$user_id"] } } }, { $project: PublicUserProjection }], { $match: { $expr: { $eq: ["$id", "$$user_id"] } } },
as: "user" { $project: PublicUserProjection },
} ],
}, as: "user",
{ },
$unwind: "$user" },
} {
], $unwind: "$user",
as: "members" },
} ],
} as: "members",
]) },
.toArray() },
); ])
.toArray()
);
const groups = roles.map((x) => ({ id: x.id === guild_id ? "online" : x.id, count: x.members.length })); 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); const member_count = roles.reduce((a, b) => b.members.length + a, 0);
const items = []; const items = [];
for (const role of roles) { for (const role of roles) {
items.push({ items.push({
group: { group: {
count: role.members.length, count: role.members.length,
@ -70,25 +80,25 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) {
member.roles.remove(guild_id); member.roles.remove(guild_id);
items.push({ member }); items.push({ member });
} }
} }
return Send(this, { return Send(this, {
op: OPCODES.Dispatch, op: OPCODES.Dispatch,
s: this.sequence++, s: this.sequence++,
t: "GUILD_MEMBER_LIST_UPDATE", t: "GUILD_MEMBER_LIST_UPDATE",
d: { d: {
ops: [ ops: [
{ {
range: [0, 99], range: [0, 99],
op: "SYNC", op: "SYNC",
items items,
} },
], ],
online_count: member_count, // TODO count online count online_count: member_count, // TODO count online count
member_count, member_count,
id: "everyone", id: "everyone",
guild_id, guild_id,
groups groups,
} },
}); });
} }