summary refs log tree commit diff
path: root/src/gateway
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-09-26 22:08:14 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-09-26 22:08:14 +1000
commit1f416b972c6aa0cd4fc43a5790da56f8da21e82c (patch)
tree8222a04cc63c595bc8b236ceeec89ed2fc2b7580 /src/gateway
parentFix scripts/benchmark/connections.js (diff)
downloadserver-1f416b972c6aa0cd4fc43a5790da56f8da21e82c.tar.xz
Move schemas to /src/util/schemas
Diffstat (limited to 'src/gateway')
-rw-r--r--src/gateway/events/Message.ts20
-rw-r--r--src/gateway/opcodes/Identify.ts2
-rw-r--r--src/gateway/opcodes/LazyRequest.ts7
-rw-r--r--src/gateway/opcodes/PresenceUpdate.ts3
-rw-r--r--src/gateway/opcodes/VoiceStateUpdate.ts2
-rw-r--r--src/gateway/schema/Activity.ts60
-rw-r--r--src/gateway/schema/Identify.ts106
-rw-r--r--src/gateway/schema/LazyRequest.ts19
-rw-r--r--src/gateway/schema/VoiceStateUpdateSchema.ts17
9 files changed, 16 insertions, 220 deletions
diff --git a/src/gateway/events/Message.ts b/src/gateway/events/Message.ts
index db7dbad2..4699f1af 100644
--- a/src/gateway/events/Message.ts
+++ b/src/gateway/events/Message.ts
@@ -36,7 +36,7 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
 		data = bigIntJson.parse(buffer as string);
 	}
 	else if (typeof buffer == "string") {
-		data = bigIntJson.parse(buffer as string)
+		data = bigIntJson.parse(buffer as string);
 	}
 	else return;
 
@@ -51,14 +51,14 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
 		return;
 	}
 
-	// const transaction = Sentry.startTransaction({
-	// 	op: OPCODES[data.op],
-	// 	name: `GATEWAY ${OPCODES[data.op]}`,
-	// 	data: {
-	// 		...data.d,
-	// 		token: data?.d?.token ? "[Redacted]" : undefined,
-	// 	},
-	// });
+	const transaction = Sentry.startTransaction({
+		op: OPCODES[data.op],
+		name: `GATEWAY ${OPCODES[data.op]}`,
+		data: {
+			...data.d,
+			token: data?.d?.token ? "[Redacted]" : undefined,
+		},
+	});
 
 	try {
 		var ret = await OPCodeHandler.call(this, data);
@@ -66,7 +66,7 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
 		return ret;
 	} catch (error) {
 		Sentry.captureException(error);
-		// transaction.finish();
+		transaction.finish();
 		console.error(`Error: Op ${data.op}`, error);
 		// if (!this.CLOSED && this.CLOSING)
 		return this.close(CLOSECODES.Unknown_error);
diff --git a/src/gateway/opcodes/Identify.ts b/src/gateway/opcodes/Identify.ts
index 3c40962c..b4b36075 100644
--- a/src/gateway/opcodes/Identify.ts
+++ b/src/gateway/opcodes/Identify.ts
@@ -20,12 +20,12 @@ import {
 	PresenceUpdateEvent,
 	DefaultUserGuildSettings,
 	UserGuildSettings,
+	IdentifySchema,
 } from "@fosscord/util";
 import { Send } from "../util/Send";
 import { CLOSECODES, OPCODES } from "../util/Constants";
 import { genSessionId } from "../util/SessionUtils";
 import { setupListener } from "../listener/listener";
-import { IdentifySchema } from "../schema/Identify";
 // import experiments from "./experiments.json";
 const experiments: any = [];
 import { check } from "./instanceOf";
diff --git a/src/gateway/opcodes/LazyRequest.ts b/src/gateway/opcodes/LazyRequest.ts
index 82342224..0f21d087 100644
--- a/src/gateway/opcodes/LazyRequest.ts
+++ b/src/gateway/opcodes/LazyRequest.ts
@@ -1,6 +1,5 @@
-import { getDatabase, getPermission, listenEvent, Member, Role, Session } from "@fosscord/util";
+import { getDatabase, getPermission, listenEvent, Member, Role, Session, LazyRequestSchema } from "@fosscord/util";
 import { WebSocket, Payload, handlePresenceUpdate, OPCODES, Send } from "@fosscord/gateway";
-import { LazyRequest } from "../schema/LazyRequest";
 import { check } from "./instanceOf";
 
 // TODO: only show roles/members that have access to this channel
@@ -137,8 +136,8 @@ async function getMembers(guild_id: string, range: [number, number]) {
 
 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;
+	check.call(this, LazyRequestSchema, d);
+	const { guild_id, typing, channels, activities } = d as LazyRequestSchema;
 
 	const channel_id = Object.keys(channels || {}).first();
 	if (!channel_id) return;
diff --git a/src/gateway/opcodes/PresenceUpdate.ts b/src/gateway/opcodes/PresenceUpdate.ts
index 415df6ee..d17b7dd7 100644
--- a/src/gateway/opcodes/PresenceUpdate.ts
+++ b/src/gateway/opcodes/PresenceUpdate.ts
@@ -1,6 +1,5 @@
 import { WebSocket, Payload } from "@fosscord/gateway";
-import { emitEvent, PresenceUpdateEvent, Session, User } from "@fosscord/util";
-import { ActivitySchema } from "../schema/Activity";
+import { emitEvent, PresenceUpdateEvent, Session, User, ActivitySchema } from "@fosscord/util";
 import { check } from "./instanceOf";
 
 export async function onPresenceUpdate(this: WebSocket, { d }: Payload) {
diff --git a/src/gateway/opcodes/VoiceStateUpdate.ts b/src/gateway/opcodes/VoiceStateUpdate.ts
index fa63f7fc..8e1585ec 100644
--- a/src/gateway/opcodes/VoiceStateUpdate.ts
+++ b/src/gateway/opcodes/VoiceStateUpdate.ts
@@ -1,4 +1,3 @@
-import { VoiceStateUpdateSchema } from "../schema/VoiceStateUpdateSchema";
 import { Payload, WebSocket } from "@fosscord/gateway";
 import { genVoiceToken } from "../util/SessionUtils";
 import { check } from "./instanceOf";
@@ -11,6 +10,7 @@ import {
 	VoiceServerUpdateEvent,
 	VoiceState,
 	VoiceStateUpdateEvent,
+	VoiceStateUpdateSchema,
 } from "@fosscord/util";
 // TODO: check if a voice server is setup
 // 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.
diff --git a/src/gateway/schema/Activity.ts b/src/gateway/schema/Activity.ts
deleted file mode 100644
index f58b0fa9..00000000
--- a/src/gateway/schema/Activity.ts
+++ /dev/null
@@ -1,60 +0,0 @@
-import { Activity, Status } from "@fosscord/util";
-
-export const ActivitySchema = {
-	afk: Boolean,
-	status: String,
-	$activities: [
-		{
-			name: String,
-			type: Number,
-			$url: String,
-			$created_at: Date,
-			$timestamps: {
-				$start: Number,
-				$end: Number,
-			},
-			$application_id: String,
-			$details: String,
-			$state: String,
-			$emoji: {
-				$name: String,
-				$id: String,
-				$animated: Boolean,
-			},
-			$party: {
-				$id: String,
-				$size: [Number, Number],
-			},
-			$assets: {
-				$large_image: String,
-				$large_text: String,
-				$small_image: String,
-				$small_text: String,
-			},
-			$secrets: {
-				$join: String,
-				$spectate: String,
-				$match: String,
-			},
-			$instance: Boolean,
-			$flags: String,
-
-			$id: String,
-			$sync_id: String,
-			$metadata: { // spotify
-				$context_uri: String,
-				album_id: String,
-				artist_ids: [String],
-			},
-			$session_id: String,
-		},
-	],
-	$since: Number, // unix time (in milliseconds) of when the client went idle, or null if the client is not idle
-};
-
-export interface ActivitySchema {
-	afk: boolean;
-	status: Status;
-	activities?: Activity[];
-	since?: number; // unix time (in milliseconds) of when the client went idle, or null if the client is not idle
-}
diff --git a/src/gateway/schema/Identify.ts b/src/gateway/schema/Identify.ts
deleted file mode 100644
index 6f68b515..00000000
--- a/src/gateway/schema/Identify.ts
+++ /dev/null
@@ -1,106 +0,0 @@
-import { ActivitySchema } from "./Activity";
-
-export const IdentifySchema = {
-	token: String,
-	$intents: BigInt, // discord uses a Integer for bitfields we use bigints tho. | instanceOf will automatically convert the Number to a BigInt
-	$properties: Object,
-	// {
-	// 	// discord uses $ in the property key for bots, so we need to double prefix it, because instanceOf treats $ (prefix) as a optional key
-	// 	$os: String,
-	// 	$os_arch: String,
-	// 	$browser: String,
-	// 	$device: String,
-	// 	$$os: String,
-	// 	$$browser: String,
-	// 	$$device: String,
-	// 	$browser_user_agent: String,
-	// 	$browser_version: String,
-	// 	$os_version: String,
-	// 	$referrer: String,
-	// 	$$referrer: String,
-	// 	$referring_domain: String,
-	// 	$$referring_domain: String,
-	// 	$referrer_current: String,
-	// 	$referring_domain_current: String,
-	// 	$release_channel: String,
-	// 	$client_build_number: Number,
-	// 	$client_event_source: String,
-	// 	$client_version: String,
-	// 	$system_locale: String,
-	// 	$window_manager: String,
-	// 	$distro: String,
-	// },
-	$presence: ActivitySchema,
-	$compress: Boolean,
-	$large_threshold: Number,
-	$shard: [BigInt, BigInt],
-	$guild_subscriptions: Boolean,
-	$capabilities: Number,
-	$client_state: {
-		$guild_hashes: Object,
-		$highest_last_message_id: String || Number,
-		$read_state_version: Number,
-		$user_guild_settings_version: Number,
-		$user_settings_version: undefined,
-		$useruser_guild_settings_version: undefined,
-	},
-	$clientState: {
-		$guildHashes: Object,
-		$highestLastMessageId: String || Number,
-		$readStateVersion: Number,
-		$useruserGuildSettingsVersion: undefined,
-		$userGuildSettingsVersion: undefined,
-	},
-	$v: Number,
-	$version: Number,
-};
-
-export interface IdentifySchema {
-	token: string;
-	properties: {
-		// bruh discord really uses $ in the property key, so we need to double prefix it, because instanceOf treats $ (prefix) as a optional key
-		os?: string;
-		os_atch?: string;
-		browser?: string;
-		device?: string;
-		$os?: string;
-		$browser?: string;
-		$device?: string;
-		browser_user_agent?: string;
-		browser_version?: string;
-		os_version?: string;
-		referrer?: string;
-		referring_domain?: string;
-		referrer_current?: string;
-		referring_domain_current?: string;
-		release_channel?: "stable" | "dev" | "ptb" | "canary";
-		client_build_number?: number;
-		client_event_source?: any;
-		client_version?: string;
-		system_locale?: string;
-	};
-	intents?: bigint; // discord uses a Integer for bitfields we use bigints tho. | instanceOf will automatically convert the Number to a BigInt
-	presence?: ActivitySchema;
-	compress?: boolean;
-	large_threshold?: number;
-	largeThreshold?: number;
-	shard?: [bigint, bigint];
-	guild_subscriptions?: boolean;
-	capabilities?: number;
-	client_state?: {
-		guild_hashes?: any;
-		highest_last_message_id?: string | number;
-		read_state_version?: number;
-		user_guild_settings_version?: number;
-		user_settings_version?: number;
-		useruser_guild_settings_version?: number;
-	};
-	clientState?: {
-		guildHashes?: any;
-		highestLastMessageId?: string | number;
-		readStateVersion?: number;
-		userGuildSettingsVersion?: number;
-		useruserGuildSettingsVersion?: number;
-	};
-	v?: number;
-}
diff --git a/src/gateway/schema/LazyRequest.ts b/src/gateway/schema/LazyRequest.ts
deleted file mode 100644
index 1fe658bb..00000000
--- a/src/gateway/schema/LazyRequest.ts
+++ /dev/null
@@ -1,19 +0,0 @@
-export interface LazyRequest {
-	guild_id: string;
-	channels?: Record<string, [number, number][]>;
-	activities?: boolean;
-	threads?: boolean;
-	typing?: true;
-	members?: any[];
-	thread_member_lists?: any[];
-}
-
-export const LazyRequest = {
-	guild_id: String,
-	$activities: Boolean,
-	$channels: Object,
-	$typing: Boolean,
-	$threads: Boolean,
-	$members: [] as any[],
-	$thread_member_lists: [] as any[],
-};
diff --git a/src/gateway/schema/VoiceStateUpdateSchema.ts b/src/gateway/schema/VoiceStateUpdateSchema.ts
deleted file mode 100644
index f6480414..00000000
--- a/src/gateway/schema/VoiceStateUpdateSchema.ts
+++ /dev/null
@@ -1,17 +0,0 @@
-export const VoiceStateUpdateSchema = {
-	$guild_id: String,
-	$channel_id: String,
-	self_mute: Boolean,
-	self_deaf: Boolean,
-	$self_video: Boolean,	//required in docs but bots don't always send it
-	$preferred_region: String,
-};
-
-export interface VoiceStateUpdateSchema {
-	guild_id?: string;
-	channel_id?: string;
-	self_mute: boolean;
-	self_deaf: boolean;
-	self_video?: boolean;
-	preferred_region?: string;
-}
\ No newline at end of file