summary refs log tree commit diff
path: root/src/gateway/events/Close.ts
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-09-25 18:24:21 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-09-25 23:35:18 +1000
commit0d23eaba09a4878520bf346af4cead90d76829fc (patch)
treed930eacceff0b407b44abe55f01d8e3c5dfbfa34 /src/gateway/events/Close.ts
parentAllow edited_timestamp to passthrough in handleMessage (diff)
downloadserver-0d23eaba09a4878520bf346af4cead90d76829fc.tar.xz
Refactor to mono-repo + upgrade packages
Diffstat (limited to 'src/gateway/events/Close.ts')
-rw-r--r--src/gateway/events/Close.ts47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/gateway/events/Close.ts b/src/gateway/events/Close.ts
new file mode 100644
index 00000000..40d9a6f7
--- /dev/null
+++ b/src/gateway/events/Close.ts
@@ -0,0 +1,47 @@
+import { WebSocket } from "@fosscord/gateway";
+import {
+	emitEvent,
+	PresenceUpdateEvent,
+	PrivateSessionProjection,
+	Session,
+	SessionsReplace,
+	User,
+} from "@fosscord/util";
+
+export async function Close(this: WebSocket, code: number, reason: string) {
+	console.log("[WebSocket] closed", code, reason);
+	if (this.heartbeatTimeout) clearTimeout(this.heartbeatTimeout);
+	if (this.readyTimeout) clearTimeout(this.readyTimeout);
+	this.deflate?.close();
+	this.inflate?.close();
+	this.removeAllListeners();
+
+	if (this.session_id) {
+		await Session.delete({ session_id: this.session_id });
+		const sessions = await Session.find({
+			where: { user_id: this.user_id },
+			select: PrivateSessionProjection,
+		});
+		await emitEvent({
+			event: "SESSIONS_REPLACE",
+			user_id: this.user_id,
+			data: sessions,
+		} as SessionsReplace);
+		const session = sessions.first() || {
+			activities: [],
+			client_info: {},
+			status: "offline",
+		};
+
+		await emitEvent({
+			event: "PRESENCE_UPDATE",
+			user_id: this.user_id,
+			data: {
+				user: await User.getPublicUser(this.user_id),
+				activities: session.activities,
+				client_status: session?.client_info,
+				status: session.status,
+			},
+		} as PresenceUpdateEvent);
+	}
+}