From b1ffb8eca13520fa41833f5361ff6a6505a80a2c Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Thu, 14 Nov 2024 19:25:57 -0500 Subject: [PATCH] fix: new sessions miss notifications --- src/backend/src/services/NotificationService.js | 15 +++++++++++++-- src/backend/src/services/WebServerService.js | 8 +++++--- src/gui/src/UI/UIDesktop.js | 1 + 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/backend/src/services/NotificationService.js b/src/backend/src/services/NotificationService.js index 9314838c..89412e36 100644 --- a/src/backend/src/services/NotificationService.js +++ b/src/backend/src/services/NotificationService.js @@ -35,6 +35,10 @@ class NotificationService extends BaseService { express: require('express'), } + _construct () { + this.merged_on_user_connected_ = {}; + } + async _init () { const svc_database = this.services.get('database'); this.db = svc_database.get(DB_WRITE, 'notification'); @@ -109,6 +113,13 @@ class NotificationService extends BaseService { } async on_user_connected ({ user }) { + if ( this.merged_on_user_connected_[user.uuid] ) { + clearTimeout(this.merged_on_user_connected_[user.uuid]); + } + this.merged_on_user_connected_[user.uuid] = + setTimeout(() => this.do_on_user_connected({ user }), 2000); + } + async do_on_user_connected ({ user }) { // query the users unread notifications const notifications = await this.db.read( 'SELECT * FROM `notification` ' + @@ -116,7 +127,7 @@ class NotificationService extends BaseService { 'ORDER BY created_at ASC', [user.id] ); - + // set all the notifications to "shown" const shown_ts = Math.floor(Date.now() / 1000); await this.db.write( @@ -140,7 +151,7 @@ class NotificationService extends BaseService { notification: notif.value, }) } - + // send the unread notifications to gui const svc_event = this.services.get('event'); svc_event.emit('outer.gui.notif.unreads', { diff --git a/src/backend/src/services/WebServerService.js b/src/backend/src/services/WebServerService.js index 88ce2711..fc42ee05 100644 --- a/src/backend/src/services/WebServerService.js +++ b/src/backend/src/services/WebServerService.js @@ -183,9 +183,11 @@ class WebServerService extends BaseService { socket.on('trash.is_empty', (msg) => { socket.broadcast.to(socket.user.id).emit('trash.is_empty', msg); }); - const svc_event = this.services.get('event'); - svc_event.emit('web.socket.user-connected', { - user: socket.user + socket.on('puter_is_actually_open', (msg) => { + const svc_event = this.services.get('event'); + svc_event.emit('web.socket.user-connected', { + user: socket.user + }); }); }); diff --git a/src/gui/src/UI/UIDesktop.js b/src/gui/src/UI/UIDesktop.js index d62b104f..81ed4c93 100644 --- a/src/gui/src/UI/UIDesktop.js +++ b/src/gui/src/UI/UIDesktop.js @@ -66,6 +66,7 @@ async function UIDesktop(options){ window.socket.on('connect', function(){ // console.log('GUI Socket: Connected', window.socket.id); + window.socket.emit('puter_is_actually_open'); }); window.socket.on('reconnect', function(){