GUILD_MEMBER_LIST_UPDATE

This commit is contained in:
honeytequila 2021-05-27 09:28:19 -03:00
parent 11d891f819
commit 910dd9a22b
3 changed files with 104 additions and 2343 deletions

2282
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -13,7 +13,7 @@
"author": "Fosscord",
"license": "ISC",
"dependencies": {
"@fosscord/server-util": "^1.3.3",
"@fosscord/server-util": "^1.3.7",
"ajv": "^8.5.0",
"dotenv": "^8.2.0",
"jsonwebtoken": "^8.5.1",

View File

@ -1,13 +1,5 @@
// @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 { OPCODES, Payload } from "../util/Constants";
import { Send } from "../util/Send";
@ -18,86 +10,85 @@ 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
export async function onLazyRequest(this: WebSocket, { d }: Payload) {
// TODO: check data
check.call(this, LazyRequest, d);
const { guild_id, typing, channels, activities } = d as LazyRequest;
// TODO: check data
check.call(this, LazyRequest, d);
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
const roles = toObject(
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",
},
],
as: "members",
},
},
])
.toArray()
);
// 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
// id: { $ne: 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"
}
],
as: "members"
}
}
])
.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);
const items = [];
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 items = [];
for (const role of roles) {
items.push({
group: {
count: role.members.length,
id: role.id,
},
});
for (const member of role.members) {
items.push({ member });
}
}
for (const role of roles) {
items.push({
group: {
count: role.members.length,
id: role.id === guild_id ? "online" : role.name
}
});
for (const member of role.members) {
member.roles.remove(guild_id);
items.push({ member });
}
}
return Send(this, {
op: OPCODES.Dispatch,
s: this.sequence++,
t: "GUILD_MEMBER_LIST_UPDATE",
d: {
ops: [
{
range: [0, 99],
op: "SYNC",
items: items,
},
],
online_count: member_count, // TODO count online count
member_count,
id: "everyone",
guild_id,
groups,
},
});
return Send(this, {
op: OPCODES.Dispatch,
s: this.sequence++,
t: "GUILD_MEMBER_LIST_UPDATE",
d: {
ops: [
{
range: [0, 99],
op: "SYNC",
items
}
],
online_count: member_count, // TODO count online count
member_count,
id: "everyone",
guild_id,
groups
}
});
}