From 05fba68bb3c92f9a1674552ea6a0809798bb40c9 Mon Sep 17 00:00:00 2001 From: Rory& Date: Mon, 13 Apr 2026 18:58:21 +0200 Subject: [PATCH] Presence disconnect works! --- src/gateway/events/Close.ts | 12 ++++++++---- src/gateway/events/Connection.ts | 1 + src/util/entities/Message.ts | 2 +- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/gateway/events/Close.ts b/src/gateway/events/Close.ts index be6a71058..594149057 100644 --- a/src/gateway/events/Close.ts +++ b/src/gateway/events/Close.ts @@ -17,9 +17,8 @@ */ import { WebSocket } from "@spacebar/gateway"; -import { emitEvent, Member, PresenceUpdateEvent, Session, SessionsReplace, User, VoiceState, VoiceStateUpdateEvent } from "@spacebar/util"; -import { distributePresenceUpdate } from "../../util/util/Presence"; -import { randomString } from "@spacebar/api*"; +import { emitEvent, Member, PresenceUpdateEvent, Session, SessionsReplace, User, VoiceState, VoiceStateUpdateEvent, distributePresenceUpdate } from "@spacebar/util"; +import { randomString } from "@spacebar/api"; export async function Close(this: WebSocket, code: number, reason: Buffer) { console.log("[WebSocket] closed", code, reason.toString()); @@ -34,13 +33,17 @@ export async function Close(this: WebSocket, code: number, reason: Buffer) { const closedAt = Date.now(); setTimeout(async () => { + console.log("Handling presence update after disconnect"); try { if (authSessionId && this.user_id) { const s = await Session.findOne({ where: { user_id: this.user_id, session_id: authSessionId }, }); if (s && (s.last_seen?.getTime() ?? 0) <= closedAt) { + console.log("... updating session"); await Session.update({ user_id: this.user_id, session_id: authSessionId }, { status: "offline", activities: [], client_status: {} }); + this.session = await Session.findOneOrFail({ where: { session_id: this.session_id } }); + console.log("... distributing PRESENCE_UPDATE"); await distributePresenceUpdate(this.user_id, { event: "PRESENCE_UPDATE", data: { @@ -52,7 +55,8 @@ export async function Close(this: WebSocket, code: number, reason: Buffer) { origin: "GATEWAY_CLOSE", transaction_id: `IDENT_${this.user_id}_${randomString()}`, } satisfies PresenceUpdateEvent); - } + console.log("... done!"); + } else console.log("... Discarding presence update as the session reactivated"); } } catch (e) { console.error("[WebSocket] Close session cleanup failed", code, e); diff --git a/src/gateway/events/Connection.ts b/src/gateway/events/Connection.ts index 06ab9e5a1..d34050502 100644 --- a/src/gateway/events/Connection.ts +++ b/src/gateway/events/Connection.ts @@ -131,6 +131,7 @@ export async function Connection(this: WS.Server, socket: WebSocket, request: In } } + socket.recentTransactions = []; socket.events = {}; socket.member_events = {}; socket.permissions = {}; diff --git a/src/util/entities/Message.ts b/src/util/entities/Message.ts index 458bb8037..333f7bd2b 100644 --- a/src/util/entities/Message.ts +++ b/src/util/entities/Message.ts @@ -264,7 +264,7 @@ export class Message extends BaseClass { } toJSON(shallow = false): PublicMessage { - this.clean_data(); + // this.clean_data(); return { ...this, channel_id: this.channel_id ?? this.channel.id,