summary refs log tree commit diff
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-02 20:10:25 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-09-02 20:10:25 +0200
commit2ba4c21f6b62c0ee8c2db06d36d489c6b6b20155 (patch)
treee50f93f1aa3148cd51a5e56bc45c7560e8f3c11d
parentVOICE_STATE_UPDATE implementation, fix #210 (diff)
downloadserver-2ba4c21f6b62c0ee8c2db06d36d489c6b6b20155.tar.xz
:art: reformat files
-rw-r--r--gateway/src/events/Close.ts4
-rw-r--r--gateway/src/events/Connection.ts6
-rw-r--r--gateway/src/opcodes/Identify.ts7
-rw-r--r--gateway/src/opcodes/VoiceStateUpdate.ts43
-rw-r--r--gateway/src/util/SessionUtils.ts8
-rw-r--r--util/src/entities/Config.ts11
-rw-r--r--util/src/entities/Session.ts37
-rw-r--r--util/src/entities/VoiceState.ts6
8 files changed, 71 insertions, 51 deletions
diff --git a/gateway/src/events/Close.ts b/gateway/src/events/Close.ts
index 26f4e7df..d68fc751 100644
--- a/gateway/src/events/Close.ts
+++ b/gateway/src/events/Close.ts
@@ -1,9 +1,9 @@
 import WebSocket from "../util/WebSocket";
 import { Message } from "./Message";
-import {Session} from "@fosscord/util";
+import { Session } from "@fosscord/util";
 
 export async function Close(this: WebSocket, code: number, reason: string) {
-	await Session.delete({session_id: this.session_id})
+	await Session.delete({ session_id: this.session_id });
 	// @ts-ignore
 	this.off("message", Message);
 }
diff --git a/gateway/src/events/Connection.ts b/gateway/src/events/Connection.ts
index 1af484eb..b3c94eaf 100644
--- a/gateway/src/events/Connection.ts
+++ b/gateway/src/events/Connection.ts
@@ -7,7 +7,7 @@ import { Send } from "../util/Send";
 import { CLOSECODES, OPCODES } from "../util/Constants";
 import { createDeflate } from "zlib";
 import { URL } from "url";
-import {Session} from "@fosscord/util";
+import { Session } from "@fosscord/util";
 var erlpack: any;
 try {
 	erlpack = require("erlpack");
@@ -57,12 +57,12 @@ export async function Connection(this: Server, socket: WebSocket, request: Incom
 		});
 
 		socket.readyTimeout = setTimeout(() => {
-			Session.delete({session_id: socket.session_id}) //should we await?
+			Session.delete({ session_id: socket.session_id }); //should we await?
 			return socket.close(CLOSECODES.Session_timed_out);
 		}, 1000 * 30);
 	} catch (error) {
 		console.error(error);
-		Session.delete({session_id: socket.session_id}) //should we await?
+		Session.delete({ session_id: socket.session_id }); //should we await?
 		return socket.close(CLOSECODES.Unknown_error);
 	}
 }
diff --git a/gateway/src/opcodes/Identify.ts b/gateway/src/opcodes/Identify.ts
index 3984be73..f1d96959 100644
--- a/gateway/src/opcodes/Identify.ts
+++ b/gateway/src/opcodes/Identify.ts
@@ -80,11 +80,12 @@ export async function onIdentify(this: WebSocket, data: Payload) {
 		user_id: this.user_id,
 		session_id: session_id,
 		status: "online", //does the session always start as online?
-		client_info: { //TODO read from identity
+		client_info: {
+			//TODO read from identity
 			client: "desktop",
 			os: "linux",
-			version: 0
-		}
+			version: 0,
+		},
 	});
 
 	//We save the session and we delete it when the websocket is closed
diff --git a/gateway/src/opcodes/VoiceStateUpdate.ts b/gateway/src/opcodes/VoiceStateUpdate.ts
index 4719949c..04392b62 100644
--- a/gateway/src/opcodes/VoiceStateUpdate.ts
+++ b/gateway/src/opcodes/VoiceStateUpdate.ts
@@ -11,16 +11,20 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
 	check.call(this, VoiceStateUpdateSchema, data.d);
 	const body = data.d as VoiceStateUpdateSchema;
 
-	let voiceState
+	let voiceState;
 	try {
-		voiceState = await VoiceState.findOneOrFail({where:{ user_id: this.user_id },relations: ["member", "member.user", "member.roles"]});
-		if(voiceState.session_id !== this.session_id && body.channel_id === null) { //Should we also check guild_id === null?
+		voiceState = await VoiceState.findOneOrFail({
+			where: { user_id: this.user_id },
+			relations: ["member", "member.user", "member.roles"],
+		});
+		if (voiceState.session_id !== this.session_id && body.channel_id === null) {
+			//Should we also check guild_id === null?
 			//changing deaf or mute on a client that's not the one with the same session of the voicestate in the database should be ignored
-			return
+			return;
 		}
 
 		//The event send by Discord's client on channel leave has both guild_id and channel_id as null
-		if(body.guild_id === null) body.guild_id = voiceState.guild_id;
+		if (body.guild_id === null) body.guild_id = voiceState.guild_id;
 		voiceState.assign(body);
 	} catch (error) {
 		voiceState = new VoiceState({
@@ -28,32 +32,39 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
 			user_id: this.user_id,
 			deaf: false,
 			mute: false,
-			suppress: false
-		})
+			suppress: false,
+		});
 	}
 
 	//If the session changed we generate a new token
-	if(voiceState.session_id !== this.session_id)
-		voiceState.token = genVoiceToken()
-	voiceState.session_id = this.session_id
+	if (voiceState.session_id !== this.session_id) voiceState.token = genVoiceToken();
+	voiceState.session_id = this.session_id;
 
 	//TODO the member should only have these properties: hoisted_role, deaf, joined_at, mute, roles, user
 	//TODO the member.user should only have these properties: avatar, discriminator, id, username
-	const {id, ...newObj} = voiceState;
+	const { id, ...newObj } = voiceState;
 
 	await Promise.all([
 		voiceState.save(),
-		emitEvent({ event: "VOICE_STATE_UPDATE", data: newObj, guild_id: voiceState.guild_id} as VoiceStateUpdateEvent),
+		emitEvent({
+			event: "VOICE_STATE_UPDATE",
+			data: newObj,
+			guild_id: voiceState.guild_id,
+		} as VoiceStateUpdateEvent),
 	]);
 
 	//If it's null it means that we are leaving the channel and this event is not needed
-	if(voiceState.channel_id !== null) {
+	if (voiceState.channel_id !== null) {
 		const regions = Config.get().regions;
 
-		await emitEvent({ event: "VOICE_SERVER_UPDATE", data: {
+		await emitEvent({
+			event: "VOICE_SERVER_UPDATE",
+			data: {
 				token: voiceState.token,
 				guild_id: voiceState.guild_id,
 				endpoint: regions.available[0].endpoint, //TODO return best endpoint or default
-			}, guild_id: voiceState.guild_id } as VoiceServerUpdateEvent)
+			},
+			guild_id: voiceState.guild_id,
+		} as VoiceServerUpdateEvent);
 	}
-}
\ No newline at end of file
+}
diff --git a/gateway/src/util/SessionUtils.ts b/gateway/src/util/SessionUtils.ts
index 1ca23316..c66c7e76 100644
--- a/gateway/src/util/SessionUtils.ts
+++ b/gateway/src/util/SessionUtils.ts
@@ -1,11 +1,11 @@
 export function genSessionId() {
-    return genRanHex(32)
+	return genRanHex(32);
 }
 
 export function genVoiceToken() {
-    return genRanHex(16)
+	return genRanHex(16);
 }
 
 function genRanHex(size: number) {
-    return [...Array(size)].map(() => Math.floor(Math.random() * 16).toString(16)).join('');
-}
\ No newline at end of file
+	return [...Array(size)].map(() => Math.floor(Math.random() * 16).toString(16)).join("");
+}
diff --git a/util/src/entities/Config.ts b/util/src/entities/Config.ts
index ebc1c61d..fd830db8 100644
--- a/util/src/entities/Config.ts
+++ b/util/src/entities/Config.ts
@@ -271,7 +271,16 @@ export const DefaultConfigOptions: ConfigValue = {
 	regions: {
 		default: "fosscord",
 		useDefaultAsOptimal: true,
-		available: [{ id: "fosscord", name: "Fosscord", endpoint: "127.0.0.1:3004", vip: false, custom: false, deprecated: false }],
+		available: [
+			{
+				id: "fosscord",
+				name: "Fosscord",
+				endpoint: "127.0.0.1:3004",
+				vip: false,
+				custom: false,
+				deprecated: false,
+			},
+		],
 	},
 	rabbitmq: {
 		host: null,
diff --git a/util/src/entities/Session.ts b/util/src/entities/Session.ts
index fb288522..d42a8f98 100644
--- a/util/src/entities/Session.ts
+++ b/util/src/entities/Session.ts
@@ -4,30 +4,29 @@ import { Column, Entity, JoinColumn, ManyToOne, RelationId } from "typeorm";
 
 //TODO we need to remove all sessions on server start because if the server crashes without closing websockets it won't delete them
 
-
 @Entity("sessions")
 export class Session extends BaseClass {
-    @Column({ nullable: true })
-    @RelationId((session: Session) => session.user)
-    user_id: string;
+	@Column({ nullable: true })
+	@RelationId((session: Session) => session.user)
+	user_id: string;
 
-    @JoinColumn({ name: "user_id" })
-    @ManyToOne(() => User)
-    user: User;
+	@JoinColumn({ name: "user_id" })
+	@ManyToOne(() => User)
+	user: User;
 
-    //TODO check, should be 32 char long hex string
-    @Column({ nullable: false })
-    session_id: string;
+	//TODO check, should be 32 char long hex string
+	@Column({ nullable: false })
+	session_id: string;
 
-    activities: []; //TODO
+	activities: []; //TODO
 
-    @Column({ type: "simple-json", select: false })
-    client_info: {
-        client: string,
-        os: string,
-        version: number
-    }
+	@Column({ type: "simple-json", select: false })
+	client_info: {
+		client: string;
+		os: string;
+		version: number;
+	};
 
-    @Column({ nullable: false })
-    status: string; //TODO enum
+	@Column({ nullable: false })
+	status: string; //TODO enum
 }
diff --git a/util/src/entities/VoiceState.ts b/util/src/entities/VoiceState.ts
index 7975273b..d7a032c7 100644
--- a/util/src/entities/VoiceState.ts
+++ b/util/src/entities/VoiceState.ts
@@ -3,7 +3,7 @@ import { BaseClass } from "./BaseClass";
 import { Channel } from "./Channel";
 import { Guild } from "./Guild";
 import { User } from "./User";
-import {Member} from "./Member";
+import { Member } from "./Member";
 
 //https://gist.github.com/vassjozsef/e482c65df6ee1facaace8b3c9ff66145#file-voice_state-ex
 @Entity("voice_states")
@@ -63,6 +63,6 @@ export class VoiceState extends BaseClass {
 	@Column()
 	suppress: boolean; // whether this user is muted by the current user
 
-	@Column({ nullable: true , default: null})
-	request_to_speak_timestamp?: Date
+	@Column({ nullable: true, default: null })
+	request_to_speak_timestamp?: Date;
 }