Bläddra i källkod

feat: re-send unreads on login

KernelDeimos 11 månader sedan
förälder
incheckning
02fc4d86b7

+ 39 - 0
packages/backend/src/services/NotificationService.js

@@ -22,7 +22,46 @@ class NotificationService extends BaseService {
             
             
             this.notify(UsernameNotifSelector(username), { summary });
             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) {
     async notify (selector, notification) {
         const uid = this.modules.uuidv4();
         const uid = this.modules.uuidv4();
         const svc_event = this.services.get('event');
         const svc_event = this.services.get('event');

+ 9 - 0
packages/backend/src/services/WebServerService.js

@@ -169,6 +169,11 @@ class WebServerService extends BaseService {
                     socket.token = auth_res.token;
                     socket.token = auth_res.token;
                     // join user room
                     // join user room
                     socket.join(socket.user.id);
                     socket.join(socket.user.id);
+                    
+                    // setTimeout 0 is needed because we need to send
+                    // the notifications after this handler is done
+                    // setTimeout(() => {
+                    // }, 1000);
                     next();
                     next();
                 } catch (e) {
                 } catch (e) {
                     console.log('socket auth err', e);
                     console.log('socket auth err', e);
@@ -181,6 +186,10 @@ class WebServerService extends BaseService {
             });
             });
             socket.on('trash.is_empty', (msg) => {
             socket.on('trash.is_empty', (msg) => {
                 socket.broadcast.to(socket.user.id).emit('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
+                });
             });
             });
         });
         });
         
         

+ 13 - 0
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) => {
     window.socket.on('app.opened', async (app) => {
         // don't update if this is the original client that initiated the action
         // don't update if this is the original client that initiated the action
         if(app.original_client_socket_id === window.socket.id)
         if(app.original_client_socket_id === window.socket.id)