1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-10 12:42:44 +01:00

🚧 gateway

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

View File

@ -1,10 +1,10 @@
import {
db,
Event,
UserModel,
User,
getPermission,
Permissions,
ChannelModel,
Channel,
RabbitMQ,
EVENT,
listenEvent,
@ -27,11 +27,11 @@ import { Channel } from "amqplib";
// TODO: use already queried guilds/channels of Identify and don't fetch them again
export async function setupListener(this: WebSocket) {
const user = await UserModel.findOne({ id: this.user_id }, { guilds: true }).exec();
const channels = await ChannelModel.find(
const user = await User.findOneOrFail({ id: this.user_id }, { guilds: true });
const channels = await Channel.find(
{ $or: [{ recipient_ids: this.user_id }, { guild_id: { $in: user.guilds } }] },
{ id: true, permission_overwrites: true }
).exec();
);
const dm_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 WebSocket from "../util/WebSocket";
import {
ChannelModel,
checkToken,
GuildModel,
Intents,
MemberDocument,
MemberModel,
ReadyEventData,
UserModel,
toObject,
EVENTEnum,
Config,
} from "@fosscord/util";
import { Channel, checkToken, Guild, Intents, Member, ReadyEventData, User, EVENTEnum, Config } from "@fosscord/util";
import { setupListener } from "../listener/listener";
import { IdentifySchema } from "../schema/Identify";
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 y = { ...x, user_id: x.id };
delete y.settings;
@ -63,8 +51,8 @@ export async function onIdentify(this: WebSocket, data: Payload) {
}) as MemberDocument[][];
const user_guild_settings_entries = members.map((x) => x.settings);
const channels = await ChannelModel.find({ recipient_ids: this.user_id }).exec();
const user = await UserModel.findOne({ id: this.user_id }).exec();
const channels = await Channel.find({ recipient_ids: this.user_id });
const user = await User.findOneOrFail({ id: this.user_id });
if (!user) return this.close(CLOSECODES.Authentication_failed);
const public_user = {
@ -76,10 +64,10 @@ export async function onIdentify(this: WebSocket, data: Payload) {
bot: user.bot,
};
const guilds = await GuildModel.find({ id: { $in: user.guilds } })
.populate({ path: "joined_at", match: { id: this.user_id } })
.exec();
const guilds = await Guild.find({ id: { $in: user.guilds } }).populate({
path: "joined_at",
match: { id: this.user_id },
});
const privateUser = {
avatar: user.avatar,
mobile: user.mobile,
@ -104,9 +92,9 @@ export async function onIdentify(this: WebSocket, data: Payload) {
const d: ReadyEventData = {
v: 8,
user: privateUser,
user_settings: user.user_settings,
user_settings: user.settings,
// @ts-ignore
guilds: toObject(guilds).map((x) => {
guilds: guilds.map((x) => {
// @ts-ignore
x.guild_hashes = {
channels: { omitted: false, hash: "y4PV2fZ0gmo" },
@ -131,7 +119,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
version: 642,
},
// @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);
delete x.recipients;
return x;
@ -144,17 +132,12 @@ export async function onIdentify(this: WebSocket, data: Payload) {
consented: false, // TODO
},
},
country_code: user.user_settings.locale,
country_code: user.settings.locale,
friend_suggestion_count: 0, // TODO
// @ts-ignore
experiments: experiments, // TODO
guild_join_requests: [], // TODO what is this?
users: [
public_user,
...toObject(channels)
.map((x: any) => x.recipients)
.flat(),
].unique(), // TODO
users: [public_user, ...channels.map((x: any) => x.recipients).flat()].unique(), // TODO
merged_members: merged_members,
// shard // TODO: only for bots sharding
// application // TODO for applications

View File

@ -18,45 +18,43 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) {
permissions.hasThrow("VIEW_CHANNEL");
// 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
},
const roles = 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",
},
},
{ $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",
},
},
{
$unwind: "$user",
},
],
as: "members",
},
])
.toArray()
);
},
])
.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);