summary refs log tree commit diff
path: root/src/gateway
diff options
context:
space:
mode:
Diffstat (limited to 'src/gateway')
-rw-r--r--src/gateway/events/Connection.ts12
-rw-r--r--src/gateway/events/Message.ts8
-rw-r--r--src/gateway/listener/listener.ts14
-rw-r--r--src/gateway/opcodes/Identify.ts12
-rw-r--r--src/gateway/opcodes/LazyRequest.ts72
-rw-r--r--src/gateway/opcodes/PresenceUpdate.ts10
-rw-r--r--src/gateway/opcodes/VoiceStateUpdate.ts8
-rw-r--r--src/gateway/start.ts2
-rw-r--r--src/gateway/util/Send.ts4
9 files changed, 94 insertions, 48 deletions
diff --git a/src/gateway/events/Connection.ts b/src/gateway/events/Connection.ts
index bed3cf44..8747e3ad 100644
--- a/src/gateway/events/Connection.ts
+++ b/src/gateway/events/Connection.ts
@@ -21,10 +21,12 @@ try {
 export async function Connection(
 	this: WS.Server,
 	socket: WebSocket,
-	request: IncomingMessage
+	request: IncomingMessage,
 ) {
 	const forwardedFor = Config.get().security.forwadedFor;
-	const ipAddress = forwardedFor ? request.headers[forwardedFor] as string : request.socket.remoteAddress;
+	const ipAddress = forwardedFor
+		? (request.headers[forwardedFor] as string)
+		: request.socket.remoteAddress;
 
 	socket.ipAddress = ipAddress;
 
@@ -33,7 +35,9 @@ export async function Connection(
 		socket.on("close", Close);
 		// @ts-ignore
 		socket.on("message", Message);
-		console.log(`[Gateway] New connection from ${socket.ipAddress}, total ${this.clients.size}`);
+		console.log(
+			`[Gateway] New connection from ${socket.ipAddress}, total ${this.clients.size}`,
+		);
 
 		const { searchParams } = new URL(`http://localhost${request.url}`);
 		// @ts-ignore
@@ -41,7 +45,7 @@ export async function Connection(
 		if (!["json", "etf"].includes(socket.encoding)) {
 			if (socket.encoding === "etf" && erlpack) {
 				throw new Error(
-					"Erlpack is not installed: 'npm i @yukikaze-bot/erlpack'"
+					"Erlpack is not installed: 'npm i @yukikaze-bot/erlpack'",
 				);
 			}
 			return socket.close(CLOSECODES.Decode_error);
diff --git a/src/gateway/events/Message.ts b/src/gateway/events/Message.ts
index 4699f1af..603f68fa 100644
--- a/src/gateway/events/Message.ts
+++ b/src/gateway/events/Message.ts
@@ -3,7 +3,7 @@ import { WebSocket, Payload } from "@fosscord/gateway";
 var erlpack: any;
 try {
 	erlpack = require("@yukikaze-bot/erlpack");
-} catch (error) { }
+} catch (error) {}
 import OPCodeHandlers from "../opcodes";
 import { Tuple } from "lambert-server";
 import { check } from "../opcodes/instanceOf";
@@ -34,11 +34,9 @@ export async function Message(this: WebSocket, buffer: WS.Data) {
 			}
 		}
 		data = bigIntJson.parse(buffer as string);
-	}
-	else if (typeof buffer == "string") {
+	} else if (typeof buffer == "string") {
 		data = bigIntJson.parse(buffer as string);
-	}
-	else return;
+	} else return;
 
 	check.call(this, PayloadSchema, data);
 
diff --git a/src/gateway/listener/listener.ts b/src/gateway/listener/listener.ts
index 72dd9d5b..1d9caebb 100644
--- a/src/gateway/listener/listener.ts
+++ b/src/gateway/listener/listener.ts
@@ -26,7 +26,7 @@ import { Recipient } from "@fosscord/util";
 
 export function handlePresenceUpdate(
 	this: WebSocket,
-	{ event, acknowledge, data }: EventOpts
+	{ event, acknowledge, data }: EventOpts,
 ) {
 	acknowledge?.();
 	if (event === EVENTEnum.PresenceUpdate) {
@@ -54,14 +54,14 @@ export async function setupListener(this: WebSocket) {
 			where: {
 				from_id: this.user_id,
 				type: RelationshipType.friends,
-			}
+			},
 		}),
 	]);
 
 	const guilds = members.map((x) => x.guild);
 	const dm_channels = recipients.map((x) => x.channel);
 
-	const opts: { acknowledge: boolean; channel?: AMQChannel; } = {
+	const opts: { acknowledge: boolean; channel?: AMQChannel } = {
 		acknowledge: true,
 	};
 	this.listen_options = opts;
@@ -79,7 +79,7 @@ export async function setupListener(this: WebSocket) {
 		this.events[relationship.to_id] = await listenEvent(
 			relationship.to_id,
 			handlePresenceUpdate.bind(this),
-			opts
+			opts,
 		);
 	});
 
@@ -101,7 +101,7 @@ export async function setupListener(this: WebSocket) {
 				this.events[channel.id] = await listenEvent(
 					channel.id,
 					consumer,
-					opts
+					opts,
 				);
 			}
 		});
@@ -137,7 +137,7 @@ async function consume(this: WebSocket, opts: EventOpts) {
 			this.member_events[data.user.id] = await listenEvent(
 				data.user.id,
 				handlePresenceUpdate.bind(this),
-				this.listen_options
+				this.listen_options,
 			);
 			break;
 		case "GUILD_MEMBER_REMOVE":
@@ -164,7 +164,7 @@ async function consume(this: WebSocket, opts: EventOpts) {
 			this.events[data.user.id] = await listenEvent(
 				data.user.id,
 				handlePresenceUpdate.bind(this),
-				this.listen_options
+				this.listen_options,
 			);
 			break;
 		case "GUILD_CREATE":
diff --git a/src/gateway/opcodes/Identify.ts b/src/gateway/opcodes/Identify.ts
index b4b36075..c5c78f1a 100644
--- a/src/gateway/opcodes/Identify.ts
+++ b/src/gateway/opcodes/Identify.ts
@@ -158,11 +158,13 @@ export async function onIdentify(this: WebSocket, data: Payload) {
 		...x.settings,
 		guild_id: x.guild.id,
 		// disgusting
-		channel_overrides: Object.entries(x.settings.channel_overrides ?? {}).map(y => ({
+		channel_overrides: Object.entries(
+			x.settings.channel_overrides ?? {},
+		).map((y) => ({
 			...y[1],
 			channel_id: y[0],
-		}))
-	})) as any as UserGuildSettings[];	// VERY disgusting. don't care.
+		})),
+	})) as any as UserGuildSettings[]; // VERY disgusting. don't care.
 
 	const channels = recipients.map((x) => {
 		// @ts-ignore
@@ -171,7 +173,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
 		users = users.concat(x.channel.recipients as unknown as User[]);
 		if (x.channel.isDm()) {
 			x.channel.recipients = x.channel.recipients!.filter(
-				(x) => x.id !== this.user_id
+				(x) => x.id !== this.user_id,
 			);
 		}
 		return x.channel;
@@ -243,7 +245,7 @@ export async function onIdentify(this: WebSocket, data: Payload) {
 		accent_color: user.accent_color,
 		banner: user.banner,
 		bio: user.bio,
-		premium_since: user.premium_since
+		premium_since: user.premium_since,
 	};
 
 	const d: ReadyEventData = {
diff --git a/src/gateway/opcodes/LazyRequest.ts b/src/gateway/opcodes/LazyRequest.ts
index 0f21d087..f5bbad14 100644
--- a/src/gateway/opcodes/LazyRequest.ts
+++ b/src/gateway/opcodes/LazyRequest.ts
@@ -1,5 +1,19 @@
-import { getDatabase, getPermission, listenEvent, Member, Role, Session, LazyRequestSchema } from "@fosscord/util";
-import { WebSocket, Payload, handlePresenceUpdate, OPCODES, Send } from "@fosscord/gateway";
+import {
+	getDatabase,
+	getPermission,
+	listenEvent,
+	Member,
+	Role,
+	Session,
+	LazyRequestSchema,
+} from "@fosscord/util";
+import {
+	WebSocket,
+	Payload,
+	handlePresenceUpdate,
+	OPCODES,
+	Send,
+} from "@fosscord/gateway";
 import { check } from "./instanceOf";
 
 // TODO: only show roles/members that have access to this channel
@@ -14,7 +28,8 @@ async function getMembers(guild_id: string, range: [number, number]) {
 
 	let members: Member[] = [];
 	try {
-		members = await getDatabase()!.getRepository(Member)
+		members = await getDatabase()!
+			.getRepository(Member)
 			.createQueryBuilder("member")
 			.where("member.guild_id = :guild_id", { guild_id })
 			.leftJoinAndSelect("member.roles", "role")
@@ -23,7 +38,7 @@ async function getMembers(guild_id: string, range: [number, number]) {
 			.addSelect("user.settings")
 			.addSelect(
 				"CASE WHEN session.status = 'offline' THEN 0 ELSE 1 END",
-				"_status"
+				"_status",
 			)
 			.orderBy("role.position", "DESC")
 			.addOrderBy("_status", "DESC")
@@ -31,8 +46,7 @@ async function getMembers(guild_id: string, range: [number, number]) {
 			.offset(Number(range[0]) || 0)
 			.limit(Number(range[1]) || 100)
 			.getMany();
-	}
-	catch (e) {
+	} catch (e) {
 		console.error(`LazyRequest`, e);
 	}
 
@@ -51,14 +65,20 @@ async function getMembers(guild_id: string, range: [number, number]) {
 		.map((m) => m.roles)
 		.flat()
 		.unique((r: Role) => r.id);
-	member_roles.push(member_roles.splice(member_roles.findIndex(x => x.id === x.guild_id), 1)[0]);
+	member_roles.push(
+		member_roles.splice(
+			member_roles.findIndex((x) => x.id === x.guild_id),
+			1,
+		)[0],
+	);
 
 	const offlineItems = [];
 
 	for (const role of member_roles) {
 		// @ts-ignore
-		const [role_members, other_members]: Member[][] = partition(members, (m: Member) =>
-			m.roles.find((r) => r.id === role.id)
+		const [role_members, other_members]: Member[][] = partition(
+			members,
+			(m: Member) => m.roles.find((r) => r.id === role.id),
 		);
 		const group = {
 			count: role_members.length,
@@ -74,15 +94,19 @@ async function getMembers(guild_id: string, range: [number, number]) {
 				.map((x: Role) => x.id);
 
 			const statusMap = {
-				"online": 0,
-				"idle": 1,
-				"dnd": 2,
-				"invisible": 3,
-				"offline": 4,
+				online: 0,
+				idle: 1,
+				dnd: 2,
+				invisible: 3,
+				offline: 4,
 			};
 			// sort sessions by relevance
 			const sessions = member.user.sessions.sort((a, b) => {
-				return (statusMap[a.status] - statusMap[b.status]) + ((a.activities.length - b.activities.length) * 2);
+				return (
+					statusMap[a.status] -
+					statusMap[b.status] +
+					(a.activities.length - b.activities.length) * 2
+				);
 			});
 			var session: Session | undefined = sessions.first();
 
@@ -103,7 +127,11 @@ async function getMembers(guild_id: string, range: [number, number]) {
 				},
 			};
 
-			if (!session || session.status == "invisible" || session.status == "offline") {
+			if (
+				!session ||
+				session.status == "invisible" ||
+				session.status == "offline"
+			) {
 				item.member.presence.status = "offline";
 				offlineItems.push(item);
 				group.count--;
@@ -130,7 +158,9 @@ async function getMembers(guild_id: string, range: [number, number]) {
 		items,
 		groups,
 		range,
-		members: items.map((x) => 'member' in x ? x.member : undefined).filter(x => !!x),
+		members: items
+			.map((x) => ("member" in x ? x.member : undefined))
+			.filter((x) => !!x),
 	};
 }
 
@@ -161,7 +191,7 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) {
 			this.member_events[member.user.id] = await listenEvent(
 				member.user.id,
 				handlePresenceUpdate.bind(this),
-				this.listen_options
+				this.listen_options,
 			);
 		});
 	});
@@ -181,7 +211,9 @@ export async function onLazyRequest(this: WebSocket, { d }: Payload) {
 				op: "SYNC",
 				range: x.range,
 			})),
-			online_count: member_count - (groups.find(x => x.id == "offline")?.count ?? 0),
+			online_count:
+				member_count -
+				(groups.find((x) => x.id == "offline")?.count ?? 0),
 			member_count,
 			id: "everyone",
 			guild_id,
@@ -199,6 +231,6 @@ function partition<T>(array: T[], isValid: Function) {
 				? [[...pass, elem], fail]
 				: [pass, [...fail, elem]];
 		},
-		[[], []]
+		[[], []],
 	);
 }
diff --git a/src/gateway/opcodes/PresenceUpdate.ts b/src/gateway/opcodes/PresenceUpdate.ts
index d17b7dd7..37299213 100644
--- a/src/gateway/opcodes/PresenceUpdate.ts
+++ b/src/gateway/opcodes/PresenceUpdate.ts
@@ -1,5 +1,11 @@
 import { WebSocket, Payload } from "@fosscord/gateway";
-import { emitEvent, PresenceUpdateEvent, Session, User, ActivitySchema } from "@fosscord/util";
+import {
+	emitEvent,
+	PresenceUpdateEvent,
+	Session,
+	User,
+	ActivitySchema,
+} from "@fosscord/util";
 import { check } from "./instanceOf";
 
 export async function onPresenceUpdate(this: WebSocket, { d }: Payload) {
@@ -8,7 +14,7 @@ export async function onPresenceUpdate(this: WebSocket, { d }: Payload) {
 
 	await Session.update(
 		{ session_id: this.session_id },
-		{ status: presence.status, activities: presence.activities }
+		{ status: presence.status, activities: presence.activities },
 	);
 
 	await emitEvent({
diff --git a/src/gateway/opcodes/VoiceStateUpdate.ts b/src/gateway/opcodes/VoiceStateUpdate.ts
index 8e1585ec..17ed7e4f 100644
--- a/src/gateway/opcodes/VoiceStateUpdate.ts
+++ b/src/gateway/opcodes/VoiceStateUpdate.ts
@@ -87,16 +87,18 @@ export async function onVoiceStateUpdate(this: WebSocket, data: Payload) {
 
 	//If it's null it means that we are leaving the channel and this event is not needed
 	if (voiceState.channel_id !== null) {
-		const guild = await Guild.findOne({ where: { id: voiceState.guild_id } });
+		const guild = await Guild.findOne({
+			where: { id: voiceState.guild_id },
+		});
 		const regions = Config.get().regions;
 		let guildRegion: Region;
 		if (guild && guild.region) {
 			guildRegion = regions.available.filter(
-				(r) => r.id === guild.region
+				(r) => r.id === guild.region,
 			)[0];
 		} else {
 			guildRegion = regions.available.filter(
-				(r) => r.id === regions.default
+				(r) => r.id === regions.default,
 			)[0];
 		}
 
diff --git a/src/gateway/start.ts b/src/gateway/start.ts
index 90d7f34e..84de674f 100644
--- a/src/gateway/start.ts
+++ b/src/gateway/start.ts
@@ -1,4 +1,4 @@
-require('module-alias/register');
+require("module-alias/register");
 process.on("uncaughtException", console.error);
 process.on("unhandledRejection", console.error);
 
diff --git a/src/gateway/util/Send.ts b/src/gateway/util/Send.ts
index e1460846..1c0f33c3 100644
--- a/src/gateway/util/Send.ts
+++ b/src/gateway/util/Send.ts
@@ -2,7 +2,9 @@ var erlpack: any;
 try {
 	erlpack = require("@yukikaze-bot/erlpack");
 } catch (error) {
-	console.log("Missing @yukikaze-bot/erlpack, electron-based desktop clients designed for discord.com will not be able to connect!");
+	console.log(
+		"Missing @yukikaze-bot/erlpack, electron-based desktop clients designed for discord.com will not be able to connect!",
+	);
 }
 import { Payload, WebSocket } from "@fosscord/gateway";