From 02fc4d86b7166fb4803be5d28e2a593d6b7d9785 Mon Sep 17 00:00:00 2001 From: KernelDeimos Date: Sat, 15 Jun 2024 14:58:32 -0400 Subject: [PATCH] feat: re-send unreads on login --- .../src/services/NotificationService.js | 39 +++++++++++++++++++ .../backend/src/services/WebServerService.js | 9 +++++ src/UI/UIDesktop.js | 13 +++++++ 3 files changed, 61 insertions(+) diff --git a/packages/backend/src/services/NotificationService.js b/packages/backend/src/services/NotificationService.js index 9dc27d43..ce3e61a9 100644 --- a/packages/backend/src/services/NotificationService.js +++ b/packages/backend/src/services/NotificationService.js @@ -22,7 +22,46 @@ class NotificationService extends BaseService { this.notify(UsernameNotifSelector(username), { summary }); }); + + const svc_event = this.services.get('event'); + svc_event.on('web.socket.user-connected', (_, { user }) => { + this.on_user_connected({ user }); + }); } + + async on_user_connected ({ user }) { + // query the users unread notifications + const notifications = await this.db.read( + 'SELECT * FROM `notification` ' + + 'WHERE user_id=? AND read=0 ' + + 'ORDER BY created_at ASC', + [user.id] + ); + for ( const n of notifications ) { + n.value = this.db.case({ + mysql: () => n.value, + otherwise: () => JSON.parse(n.value ?? '{}'), + })(); + } + + const client_safe_notifications = []; + for ( const notif of notifications ) { + client_safe_notifications.push({ + uid: notif.uid, + notification: notif.value, + }) + } + + // send the unread notifications to gui + const svc_event = this.services.get('event'); + svc_event.emit('outer.gui.notif.unreads', { + user_id_list: [user.id], + response: { + unreads: client_safe_notifications, + }, + }); + } + async notify (selector, notification) { const uid = this.modules.uuidv4(); const svc_event = this.services.get('event'); diff --git a/packages/backend/src/services/WebServerService.js b/packages/backend/src/services/WebServerService.js index b92142ee..03848d7a 100644 --- a/packages/backend/src/services/WebServerService.js +++ b/packages/backend/src/services/WebServerService.js @@ -169,6 +169,11 @@ class WebServerService extends BaseService { socket.token = auth_res.token; // join user room socket.join(socket.user.id); + + // setTimeout 0 is needed because we need to send + // the notifications after this handler is done + // setTimeout(() => { + // }, 1000); next(); } catch (e) { console.log('socket auth err', e); @@ -181,6 +186,10 @@ 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 + }); }); }); diff --git a/src/UI/UIDesktop.js b/src/UI/UIDesktop.js index 873335be..b73a5425 100644 --- a/src/UI/UIDesktop.js +++ b/src/UI/UIDesktop.js @@ -117,6 +117,19 @@ async function UIDesktop(options){ }); }); + window.__already_got_unreads = false; + window.socket.on('notif.unreads', ({ unreads }) => { + if ( window.__already_got_unreads ) return; + window.__already_got_unreads = true; + + for ( const notif_info of unreads ) { + const notification = notif_info.notification; + UINotification({ + content: notification.summary + }); + } + }); + window.socket.on('app.opened', async (app) => { // don't update if this is the original client that initiated the action if(app.original_client_socket_id === window.socket.id)