🎨 reformat files

This commit is contained in:
Flam3rboy 2021-09-02 20:10:25 +02:00
parent 8ff56b1f9e
commit 8092216343
8 changed files with 71 additions and 51 deletions

View File

@ -3,7 +3,7 @@ import { Message } from "./Message";
import { Session } from "@fosscord/util"; import { Session } from "@fosscord/util";
export async function Close(this: WebSocket, code: number, reason: string) { 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 // @ts-ignore
this.off("message", Message); this.off("message", Message);
} }

View File

@ -57,12 +57,12 @@ export async function Connection(this: Server, socket: WebSocket, request: Incom
}); });
socket.readyTimeout = setTimeout(() => { 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); return socket.close(CLOSECODES.Session_timed_out);
}, 1000 * 30); }, 1000 * 30);
} catch (error) { } catch (error) {
console.error(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); return socket.close(CLOSECODES.Unknown_error);
} }
} }

View File

@ -80,11 +80,12 @@ export async function onIdentify(this: WebSocket, data: Payload) {
user_id: this.user_id, user_id: this.user_id,
session_id: session_id, session_id: session_id,
status: "online", //does the session always start as online? status: "online", //does the session always start as online?
client_info: { //TODO read from identity client_info: {
//TODO read from identity
client: "desktop", client: "desktop",
os: "linux", os: "linux",
version: 0 version: 0,
} },
}); });
//We save the session and we delete it when the websocket is closed //We save the session and we delete it when the websocket is closed

View File

@ -11,12 +11,16 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
check.call(this, VoiceStateUpdateSchema, data.d); check.call(this, VoiceStateUpdateSchema, data.d);
const body = data.d as VoiceStateUpdateSchema; const body = data.d as VoiceStateUpdateSchema;
let voiceState let voiceState;
try { try {
voiceState = await VoiceState.findOneOrFail({where:{ user_id: this.user_id },relations: ["member", "member.user", "member.roles"]}); voiceState = await VoiceState.findOneOrFail({
if(voiceState.session_id !== this.session_id && body.channel_id === null) { //Should we also check guild_id === null? 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 //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 //The event send by Discord's client on channel leave has both guild_id and channel_id as null
@ -28,14 +32,13 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
user_id: this.user_id, user_id: this.user_id,
deaf: false, deaf: false,
mute: false, mute: false,
suppress: false suppress: false,
}) });
} }
//If the session changed we generate a new token //If the session changed we generate a new token
if(voiceState.session_id !== this.session_id) if (voiceState.session_id !== this.session_id) voiceState.token = genVoiceToken();
voiceState.token = genVoiceToken() voiceState.session_id = this.session_id;
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 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 //TODO the member.user should only have these properties: avatar, discriminator, id, username
@ -43,17 +46,25 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
await Promise.all([ await Promise.all([
voiceState.save(), 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 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; const regions = Config.get().regions;
await emitEvent({ event: "VOICE_SERVER_UPDATE", data: { await emitEvent({
event: "VOICE_SERVER_UPDATE",
data: {
token: voiceState.token, token: voiceState.token,
guild_id: voiceState.guild_id, guild_id: voiceState.guild_id,
endpoint: regions.available[0].endpoint, //TODO return best endpoint or default 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);
} }
} }

View File

@ -1,11 +1,11 @@
export function genSessionId() { export function genSessionId() {
return genRanHex(32) return genRanHex(32);
} }
export function genVoiceToken() { export function genVoiceToken() {
return genRanHex(16) return genRanHex(16);
} }
function genRanHex(size: number) { function genRanHex(size: number) {
return [...Array(size)].map(() => Math.floor(Math.random() * 16).toString(16)).join(''); return [...Array(size)].map(() => Math.floor(Math.random() * 16).toString(16)).join("");
} }

View File

@ -271,7 +271,16 @@ export const DefaultConfigOptions: ConfigValue = {
regions: { regions: {
default: "fosscord", default: "fosscord",
useDefaultAsOptimal: true, 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: { rabbitmq: {
host: null, host: null,

View File

@ -4,7 +4,6 @@ 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 //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") @Entity("sessions")
export class Session extends BaseClass { export class Session extends BaseClass {
@Column({ nullable: true }) @Column({ nullable: true })
@ -23,10 +22,10 @@ export class Session extends BaseClass {
@Column({ type: "simple-json", select: false }) @Column({ type: "simple-json", select: false })
client_info: { client_info: {
client: string, client: string;
os: string, os: string;
version: number version: number;
} };
@Column({ nullable: false }) @Column({ nullable: false })
status: string; //TODO enum status: string; //TODO enum

View File

@ -64,5 +64,5 @@ export class VoiceState extends BaseClass {
suppress: boolean; // whether this user is muted by the current user suppress: boolean; // whether this user is muted by the current user
@Column({ nullable: true, default: null }) @Column({ nullable: true, default: null })
request_to_speak_timestamp?: Date request_to_speak_timestamp?: Date;
} }