fix: new sessions miss notifications

This commit is contained in:
KernelDeimos 2024-11-14 19:25:57 -05:00
parent d0f16c8105
commit b1ffb8eca1
3 changed files with 19 additions and 5 deletions

View File

@ -35,6 +35,10 @@ class NotificationService extends BaseService {
express: require('express'), express: require('express'),
} }
_construct () {
this.merged_on_user_connected_ = {};
}
async _init () { async _init () {
const svc_database = this.services.get('database'); const svc_database = this.services.get('database');
this.db = svc_database.get(DB_WRITE, 'notification'); this.db = svc_database.get(DB_WRITE, 'notification');
@ -109,6 +113,13 @@ class NotificationService extends BaseService {
} }
async on_user_connected ({ user }) { 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 // query the users unread notifications
const notifications = await this.db.read( const notifications = await this.db.read(
'SELECT * FROM `notification` ' + 'SELECT * FROM `notification` ' +
@ -116,7 +127,7 @@ class NotificationService extends BaseService {
'ORDER BY created_at ASC', 'ORDER BY created_at ASC',
[user.id] [user.id]
); );
// set all the notifications to "shown" // set all the notifications to "shown"
const shown_ts = Math.floor(Date.now() / 1000); const shown_ts = Math.floor(Date.now() / 1000);
await this.db.write( await this.db.write(
@ -140,7 +151,7 @@ class NotificationService extends BaseService {
notification: notif.value, notification: notif.value,
}) })
} }
// send the unread notifications to gui // send the unread notifications to gui
const svc_event = this.services.get('event'); const svc_event = this.services.get('event');
svc_event.emit('outer.gui.notif.unreads', { svc_event.emit('outer.gui.notif.unreads', {

View File

@ -183,9 +183,11 @@ 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'); socket.on('puter_is_actually_open', (msg) => {
svc_event.emit('web.socket.user-connected', { const svc_event = this.services.get('event');
user: socket.user svc_event.emit('web.socket.user-connected', {
user: socket.user
});
}); });
}); });

View File

@ -66,6 +66,7 @@ async function UIDesktop(options){
window.socket.on('connect', function(){ window.socket.on('connect', function(){
// console.log('GUI Socket: Connected', window.socket.id); // console.log('GUI Socket: Connected', window.socket.id);
window.socket.emit('puter_is_actually_open');
}); });
window.socket.on('reconnect', function(){ window.socket.on('reconnect', function(){