feat: re-send unreads on login

This commit is contained in:
KernelDeimos 2024-06-15 14:58:32 -04:00 committed by Eric Dubé
parent a1e6887bf9
commit 02fc4d86b7
3 changed files with 61 additions and 0 deletions

View File

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

View File

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

View File

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