1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
/*
Spacebar: A FOSS re-implementation and extension of the Discord.com backend.
Copyright (C) 2023 Spacebar and Spacebar Contributors
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { OPCODES, Payload, Send, WebSocket } from "@spacebar/gateway";
import { Member, RequestGuildMembersSchema, getDatabase } from "@spacebar/util";
import { check } from "./instanceOf";
function partition(members: Member[], size: number) {
const chunks = [];
for (let i = 0; i < members.length; i += size) {
chunks.push(members.slice(i, i + size));
}
return chunks;
}
export async function onRequestGuildMembers(this: WebSocket, { d }: Payload) {
check.call(this, RequestGuildMembersSchema, d);
const { guild_id, query, limit, presences, user_ids, nonce } =
d as RequestGuildMembersSchema;
if (query && user_ids) {
throw new Error("Cannot query and provide user ids");
}
if (query && !limit) {
throw new Error("Must provide limit when querying");
}
const takeLimit =
(query && query !== "") || user_ids
? limit && limit !== 0 && limit <= 1000
? limit
: 100
: undefined;
const member_count = await Member.count({
where: {
guild_id,
},
});
// TODO: if member count is >75k, only return members in voice plus the connecting users member object
// TODO: if member count is >large_threshold, send members who are online, have a role, have a nickname, or are in a voice channel
// TODO: if member count is <large_threshold, send all members
let members: Member[] = [];
if (member_count > 75000) {
// since we dont have voice channels yet, just return the connecting users member object
members = await Member.find({
where: {
guild_id,
user: {
id: this.user_id,
},
},
relations: ["user", "roles"],
});
} else if (member_count > this.large_threshold) {
try {
// find all members who are online, have a role, have a nickname, or are in a voice channel, as well as respecting the query and user_ids
const db = getDatabase();
if (!db) throw new Error("Database not initialized");
const repo = db.getRepository(Member);
const q = repo
.createQueryBuilder("member")
.where("member.guild_id = :guild_id", { guild_id })
.leftJoinAndSelect("member.roles", "role")
.leftJoinAndSelect("member.user", "user")
.leftJoinAndSelect("user.sessions", "session")
.andWhere(
`',' || member.roles || ',' NOT LIKE :everyoneRoleIdList`,
{ everyoneRoleIdList: `%,${guild_id},%` },
)
.andWhere("session.status != 'offline'")
.addOrderBy("user.username", "ASC")
.limit(takeLimit);
if (query && query !== "") {
q.andWhere(`user.username ILIKE :query`, {
query: `${query}%`,
});
} else if (user_ids) {
q.andWhere(`user.id IN (:...user_ids)`, { user_ids });
}
members = await q.getMany();
} catch (e) {
console.error(`request guild members`, e);
}
} else {
members = await Member.find({
where: {
guild_id,
...(user_ids && { user_id: user_ids }),
...(query && { username: { startsWith: query } }),
},
take: takeLimit,
relations: ["user", "roles"],
});
}
const chunks = partition(members, 1000);
for (const [i, chunk] of chunks.entries()) {
await Send(this, {
op: OPCODES.Dispatch,
s: this.sequence++,
t: "GUILD_MEMBERS_CHUNK",
d: {
guild_id,
members: chunk.map((member) => ({
...member,
roles: member.roles.map((role) => role.id),
user: member.user.toPublicUser(),
})),
chunk_index: i + 1,
chunk_count: chunks.length,
// not_found: []
// presences: []
nonce,
},
});
}
}
|