Presence disconnect works!

This commit is contained in:
Rory&
2026-04-13 18:58:21 +02:00
parent 10a973eced
commit 05fba68bb3
3 changed files with 10 additions and 5 deletions

View File

@@ -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);

View File

@@ -131,6 +131,7 @@ export async function Connection(this: WS.Server, socket: WebSocket, request: In
}
}
socket.recentTransactions = [];
socket.events = {};
socket.member_events = {};
socket.permissions = {};

View File

@@ -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,