1717*/
1818
1919import { WebSocket } from "@spacebar/gateway" ;
20- import { emitEvent , Member , PresenceUpdateEvent , Session , SessionsReplace , User , VoiceState , VoiceStateUpdateEvent } from "@spacebar/util" ;
21- import { distributePresenceUpdate } from "../../util/util/Presence" ;
22- import { randomString } from "@spacebar/api*" ;
20+ import { emitEvent , Member , PresenceUpdateEvent , Session , SessionsReplace , User , VoiceState , VoiceStateUpdateEvent , distributePresenceUpdate } from "@spacebar/util" ;
21+ import { randomString } from "@spacebar/api" ;
2322
2423export async function Close ( this : WebSocket , code : number , reason : Buffer ) {
2524 console . log ( "[WebSocket] closed" , code , reason . toString ( ) ) ;
@@ -34,13 +33,17 @@ export async function Close(this: WebSocket, code: number, reason: Buffer) {
3433 const closedAt = Date . now ( ) ;
3534
3635 setTimeout ( async ( ) => {
36+ console . log ( "Handling presence update after disconnect" ) ;
3737 try {
3838 if ( authSessionId && this . user_id ) {
3939 const s = await Session . findOne ( {
4040 where : { user_id : this . user_id , session_id : authSessionId } ,
4141 } ) ;
4242 if ( s && ( s . last_seen ?. getTime ( ) ?? 0 ) <= closedAt ) {
43+ console . log ( "... updating session" ) ;
4344 await Session . update ( { user_id : this . user_id , session_id : authSessionId } , { status : "offline" , activities : [ ] , client_status : { } } ) ;
45+ this . session = await Session . findOneOrFail ( { where : { session_id : this . session_id } } ) ;
46+ console . log ( "... distributing PRESENCE_UPDATE" ) ;
4447 await distributePresenceUpdate ( this . user_id , {
4548 event : "PRESENCE_UPDATE" ,
4649 data : {
@@ -52,7 +55,8 @@ export async function Close(this: WebSocket, code: number, reason: Buffer) {
5255 origin : "GATEWAY_CLOSE" ,
5356 transaction_id : `IDENT_${ this . user_id } _${ randomString ( ) } ` ,
5457 } satisfies PresenceUpdateEvent ) ;
55- }
58+ console . log ( "... done!" ) ;
59+ } else console . log ( "... Discarding presence update as the session reactivated" ) ;
5660 }
5761 } catch ( e ) {
5862 console . error ( "[WebSocket] Close session cleanup failed" , code , e ) ;
0 commit comments