summary refs log tree commit diff
path: root/gateway/src/events
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-17 00:39:54 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-17 00:39:54 +0200
commit0ea7d5f35cba312545d56bee8abc878fe34383a0 (patch)
tree8606217c7ecff9fe223b80a33e4f42270850ca14 /gateway/src/events
parent:sparkles: added session + memberlist event (diff)
downloadserver-0ea7d5f35cba312545d56bee8abc878fe34383a0.tar.xz
:sparkles: User presence/status
Diffstat (limited to 'gateway/src/events')
-rw-r--r--gateway/src/events/Close.ts41
-rw-r--r--gateway/src/events/Connection.ts2
2 files changed, 38 insertions, 5 deletions
diff --git a/gateway/src/events/Close.ts b/gateway/src/events/Close.ts
index 5c1bd292..5b7c512c 100644
--- a/gateway/src/events/Close.ts
+++ b/gateway/src/events/Close.ts
@@ -1,13 +1,46 @@
 import { WebSocket } from "@fosscord/gateway";
-import { Session } from "@fosscord/util";
+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.session_id) await Session.delete({ session_id: this.session_id });
 	if (this.heartbeatTimeout) clearTimeout(this.heartbeatTimeout);
 	if (this.readyTimeout) clearTimeout(this.readyTimeout);
-
 	this.deflate?.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);
+	}
 }
diff --git a/gateway/src/events/Connection.ts b/gateway/src/events/Connection.ts
index 9bb034f0..4954cd08 100644
--- a/gateway/src/events/Connection.ts
+++ b/gateway/src/events/Connection.ts
@@ -8,7 +8,6 @@ import { Close } from "./Close";
 import { Message } from "./Message";
 import { createDeflate } from "zlib";
 import { URL } from "url";
-import { Session } from "@fosscord/util";
 var erlpack: any;
 try {
 	erlpack = require("@yukikaze-bot/erlpack");
@@ -57,6 +56,7 @@ export async function Connection(
 		}
 
 		socket.events = {};
+		socket.member_events = {};
 		socket.permissions = {};
 		socket.sequence = 0;