🚧 gateway

This commit is contained in:
Flam3rboy 2021-08-24 16:35:20 +02:00
parent a567551c8a
commit b57f8b33d4
3 changed files with 52 additions and 71 deletions

View File

@ -1,10 +1,10 @@
import { import {
db, db,
Event, Event,
UserModel, User,
getPermission, getPermission,
Permissions, Permissions,
ChannelModel, Channel,
RabbitMQ, RabbitMQ,
EVENT, EVENT,
listenEvent, listenEvent,
@ -27,11 +27,11 @@ import { Channel } from "amqplib";
// TODO: use already queried guilds/channels of Identify and don't fetch them again // TODO: use already queried guilds/channels of Identify and don't fetch them again
export async function setupListener(this: WebSocket) { export async function setupListener(this: WebSocket) {
const user = await UserModel.findOne({ id: this.user_id }, { guilds: true }).exec(); const user = await User.findOneOrFail({ id: this.user_id }, { guilds: true });
const channels = await ChannelModel.find( const channels = await Channel.find(
{ $or: [{ recipient_ids: this.user_id }, { guild_id: { $in: user.guilds } }] }, { $or: [{ recipient_ids: this.user_id }, { guild_id: { $in: user.guilds } }] },
{ id: true, permission_overwrites: true } { id: true, permission_overwrites: true }
).exec(); );
const dm_channels = channels.filter((x) => !x.guild_id); const dm_channels = channels.filter((x) => !x.guild_id);
const guild_channels = channels.filter((x) => x.guild_id); const guild_channels = channels.filter((x) => x.guild_id);

View File

@ -1,18 +1,6 @@
import { CLOSECODES, Payload, OPCODES } from "../util/Constants"; import { CLOSECODES, Payload, OPCODES } from "../util/Constants";
import WebSocket from "../util/WebSocket"; import WebSocket from "../util/WebSocket";
import { import { Channel, checkToken, Guild, Intents, Member, ReadyEventData, User, EVENTEnum, Config } from "@fosscord/util";
ChannelModel,
checkToken,
GuildModel,
Intents,
MemberDocument,
MemberModel,
ReadyEventData,
UserModel,
toObject,
EVENTEnum,
Config,
} from "@fosscord/util";
import { setupListener } from "../listener/listener"; import { setupListener } from "../listener/listener";
import { IdentifySchema } from "../schema/Identify"; import { IdentifySchema } from "../schema/Identify";
import { Send } from "../util/Send"; import { Send } from "../util/Send";
@ -54,7 +42,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
} }
} }
const members = toObject(await MemberModel.find({ id: this.user_id }).exec()); const members = await Member.find({ id: this.user_id });
const merged_members = members.map((x: any) => { const merged_members = members.map((x: any) => {
const y = { ...x, user_id: x.id }; const y = { ...x, user_id: x.id };
delete y.settings; delete y.settings;
@ -63,8 +51,8 @@ export async function onIdentify(this: WebSocket, data: Payload) {
}) as MemberDocument[][]; }) as MemberDocument[][];
const user_guild_settings_entries = members.map((x) => x.settings); const user_guild_settings_entries = members.map((x) => x.settings);
const channels = await ChannelModel.find({ recipient_ids: this.user_id }).exec(); const channels = await Channel.find({ recipient_ids: this.user_id });
const user = await UserModel.findOne({ id: this.user_id }).exec(); const user = await User.findOneOrFail({ id: this.user_id });
if (!user) return this.close(CLOSECODES.Authentication_failed); if (!user) return this.close(CLOSECODES.Authentication_failed);
const public_user = { const public_user = {
@ -76,10 +64,10 @@ export async function onIdentify(this: WebSocket, data: Payload) {
bot: user.bot, bot: user.bot,
}; };
const guilds = await GuildModel.find({ id: { $in: user.guilds } }) const guilds = await Guild.find({ id: { $in: user.guilds } }).populate({
.populate({ path: "joined_at", match: { id: this.user_id } }) path: "joined_at",
.exec(); match: { id: this.user_id },
});
const privateUser = { const privateUser = {
avatar: user.avatar, avatar: user.avatar,
mobile: user.mobile, mobile: user.mobile,
@ -104,9 +92,9 @@ export async function onIdentify(this: WebSocket, data: Payload) {
const d: ReadyEventData = { const d: ReadyEventData = {
v: 8, v: 8,
user: privateUser, user: privateUser,
user_settings: user.user_settings, user_settings: user.settings,
// @ts-ignore // @ts-ignore
guilds: toObject(guilds).map((x) => { guilds: guilds.map((x) => {
// @ts-ignore // @ts-ignore
x.guild_hashes = { x.guild_hashes = {
channels: { omitted: false, hash: "y4PV2fZ0gmo" }, channels: { omitted: false, hash: "y4PV2fZ0gmo" },
@ -131,7 +119,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
version: 642, version: 642,
}, },
// @ts-ignore // @ts-ignore
private_channels: toObject(channels).map((x: ChannelDocument) => { private_channels: channels.map((x): ChannelDocument => {
x.recipient_ids = x.recipients.map((y: any) => y.id); x.recipient_ids = x.recipients.map((y: any) => y.id);
delete x.recipients; delete x.recipients;
return x; return x;
@ -144,17 +132,12 @@ export async function onIdentify(this: WebSocket, data: Payload) {
consented: false, // TODO consented: false, // TODO
}, },
}, },
country_code: user.user_settings.locale, country_code: user.settings.locale,
friend_suggestion_count: 0, // TODO friend_suggestion_count: 0, // TODO
// @ts-ignore // @ts-ignore
experiments: experiments, // TODO experiments: experiments, // TODO
guild_join_requests: [], // TODO what is this? guild_join_requests: [], // TODO what is this?
users: [ users: [public_user, ...channels.map((x: any) => x.recipients).flat()].unique(), // TODO
public_user,
...toObject(channels)
.map((x: any) => x.recipients)
.flat(),
].unique(), // TODO
merged_members: merged_members, merged_members: merged_members,
// shard // TODO: only for bots sharding // shard // TODO: only for bots sharding
// application // TODO for applications // application // TODO for applications

View File

@ -18,45 +18,43 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) {
permissions.hasThrow("VIEW_CHANNEL"); permissions.hasThrow("VIEW_CHANNEL");
// 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 = await db
await db .collection("roles")
.collection("roles") .aggregate([
.aggregate([ {
{ $match: {
$match: { guild_id,
guild_id, // hoist: true // TODO: also match @everyone role
// hoist: true // TODO: also match @everyone role
},
}, },
{ $sort: { position: 1 } }, },
{ { $sort: { position: 1 } },
$lookup: { {
from: "members", $lookup: {
let: { id: "$id" }, from: "members",
pipeline: [ let: { id: "$id" },
{ $match: { $expr: { $in: ["$$id", "$roles"] } } }, pipeline: [
{ $limit: 100 }, { $match: { $expr: { $in: ["$$id", "$roles"] } } },
{ { $limit: 100 },
$lookup: { {
from: "users", $lookup: {
let: { user_id: "$id" }, from: "users",
pipeline: [ let: { user_id: "$id" },
{ $match: { $expr: { $eq: ["$id", "$$user_id"] } } }, pipeline: [
{ $project: PublicUserProjection }, { $match: { $expr: { $eq: ["$id", "$$user_id"] } } },
], { $project: PublicUserProjection },
as: "user", ],
}, 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);