summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-18 22:04:29 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-18 22:04:29 +0200
commitda08497a19ab0635a8bd78e499cfc61e54352dcc (patch)
tree7fcda3b1ebca8e8a902c066945e02206b089fe51 /src
parentMerge branch 'master' of https://github.com/discord-open-source/discord-gateway (diff)
downloadserver-da08497a19ab0635a8bd78e499cfc61e54352dcc.tar.xz
:construction: WIP member list + voice
Diffstat (limited to 'src')
-rw-r--r--src/listener/listener.ts10
-rw-r--r--src/opcodes/LazyRequest.ts47
-rw-r--r--src/opcodes/VoiceStateUpdate.ts10
-rw-r--r--src/schema/LazyRequest.ts15
-rw-r--r--src/schema/VoiceStateUpdate.ts.ts13
-rw-r--r--src/util/Config.ts4
6 files changed, 92 insertions, 7 deletions
diff --git a/src/listener/listener.ts b/src/listener/listener.ts

index e3516993..54cdb47d 100644 --- a/src/listener/listener.ts +++ b/src/listener/listener.ts
@@ -17,7 +17,7 @@ export interface DispatchOpts { guilds: Array<string>; } -function getPipeline(this: WebSocket, guilds: string[]) { +function getPipeline(this: WebSocket, guilds: string[], channels: string[]) { if (this.shard_count) { guilds = guilds.filter((x) => (BigInt(x) >> 22n) % this.shard_count === this.shard_id); } @@ -25,7 +25,11 @@ function getPipeline(this: WebSocket, guilds: string[]) { return [ { $match: { - $or: [{ "fullDocument.guild_id": { $in: guilds } }, { "fullDocument.user_id": this.user_id }], + $or: [ + { "fullDocument.guild_id": { $in: guilds } }, + { "fullDocument.user_id": this.user_id }, + { "fullDocument.channel_id": { $in: channels } }, + ], }, }, ]; @@ -45,6 +49,8 @@ export async function setupListener(this: WebSocket) { this.once("close", () => eventStream.destroy()); } +// TODO: cache permission + export async function dispatch(this: WebSocket, document: Event, { eventStream, guilds }: DispatchOpts) { var permission = new Permissions("ADMINISTRATOR"); // default permission for dms console.log("event", document); diff --git a/src/opcodes/LazyRequest.ts b/src/opcodes/LazyRequest.ts
index accff8de..ba7278f0 100644 --- a/src/opcodes/LazyRequest.ts +++ b/src/opcodes/LazyRequest.ts
@@ -1,10 +1,51 @@ -import { CLOSECODES, OPCODES, Payload } from "../util/Constants"; +// @ts-nocheck WIP +import { db, getPermission, MemberModel, MongooseCache, PublicUserProjection, RoleModel } from "@fosscord/server-util"; +import { LazyRequest } from "../schema/LazyRequest"; +import { OPCODES, Payload } from "../util/Constants"; import { Send } from "../util/Send"; import WebSocket from "../util/WebSocket"; +import { check } from "./instanceOf"; -export function onLazyRequest(this: WebSocket, { d }: Payload) { +// 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) { + return; // WIP // TODO: check data - const { guild_id, typing, channels, activities } = d; + check.call(this, LazyRequest, d); + const { guild_id, typing, channels, activities } = d as LazyRequest; + + 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 = await db + .collection("roles") + .aggregate([ + { $match: { guild_id, hoist: true } }, + { $sort: { position: 1 } }, + { + $lookup: { + from: "members", + let: { id: "$id" }, + pipeline: [ + { $match: { $expr: { $in: ["$$id", "$roles"] } } }, + { $limit: 1 }, + { + $lookup: { + from: "users", + let: { user_id: "$id" }, + pipeline: [ + { $match: { $expr: { $eq: ["$id", "$$user_id"] } } }, + { $project: PublicUserProjection }, + ], + as: "user", + }, + }, + ], + as: "members", + }, + }, + ]) + .toArray(); Send(this, { op: OPCODES.Dispatch, diff --git a/src/opcodes/VoiceStateUpdate.ts b/src/opcodes/VoiceStateUpdate.ts
index 584cbfd0..f2230cd9 100644 --- a/src/opcodes/VoiceStateUpdate.ts +++ b/src/opcodes/VoiceStateUpdate.ts
@@ -1,5 +1,13 @@ +import { VoiceStateUpdateSchema } from "../schema/VoiceStateUpdate.ts"; import { CLOSECODES, Payload } from "../util/Constants"; import WebSocket from "../util/WebSocket"; +import { check } from "./instanceOf"; +// TODO: implementation +// TODO: check if a voice server is setup +// TODO: save voice servers in database and retrieve them +// Notice: Bot users respect the voice channel's user limit, if set. When the voice channel is full, you will not receive the Voice State Update or Voice Server Update events in response to your own Voice State Update. Having MANAGE_CHANNELS permission bypasses this limit and allows you to join regardless of the channel being full or not. -export function onVoiceStateUpdate(this: WebSocket, data: Payload) {} +export function onVoiceStateUpdate(this: WebSocket, data: Payload) { + check.call(this, VoiceStateUpdateSchema, data.d); +} diff --git a/src/schema/LazyRequest.ts b/src/schema/LazyRequest.ts new file mode 100644
index 00000000..b8c51431 --- /dev/null +++ b/src/schema/LazyRequest.ts
@@ -0,0 +1,15 @@ +export interface LazyRequest { + activities: boolean; + channels: Record<string, [number, number]>; + guild_id: string; + threads: boolean; + typing: true; +} + +export const LazyRequest = { + activities: Boolean, + channels: Object, + guild_id: String, + threads: Boolean, + typing: Boolean, +}; diff --git a/src/schema/VoiceStateUpdate.ts.ts b/src/schema/VoiceStateUpdate.ts.ts new file mode 100644
index 00000000..5e3e35e0 --- /dev/null +++ b/src/schema/VoiceStateUpdate.ts.ts
@@ -0,0 +1,13 @@ +export const VoiceStateUpdateSchema = { + guild_id: String, + channel_id: String, + self_mute: Boolean, + self_deaf: Boolean, +}; + +export interface VoiceStateUpdateSchema { + guild_id: string; + channel_id: string; + self_mute: boolean; + self_deaf: boolean; +} diff --git a/src/util/Config.ts b/src/util/Config.ts
index e94d73fe..eee20e1d 100644 --- a/src/util/Config.ts +++ b/src/util/Config.ts
@@ -14,6 +14,8 @@ export default { setAll: Config.setAll, }; -export interface DefaultOptions {} +export interface DefaultOptions { + endpoint?: string; +} export const DefaultOptions: DefaultOptions = {};