[Fix]: Fix Notifications

This commit is contained in:
ClaraCrazy 2025-12-27 16:17:01 +01:00
parent 2f7194e6bd
commit 2c7b3ead3c
No known key found for this signature in database
GPG key ID: EBBC896ACB497011
2 changed files with 36 additions and 2 deletions

View file

@ -6,9 +6,21 @@ require('events').defaultMaxListeners = 50;
* Receives sign-ins from users and notifies them when new mails are available. * Receives sign-ins from users and notifies them when new mails are available.
*/ */
class ClientNotification extends EventEmitter { class ClientNotification extends EventEmitter {
constructor() {
super();
this.pendingNotifications = new Map(); // address -> count
}
use(io) { use(io) {
io.on('connection', socket => { io.on('connection', socket => {
socket.on('sign in', address => this._signIn(socket, address)) debug(`[SOCKET] New connection: id=${socket.id}`);
socket.on('sign in', address => {
debug(`[SOCKET] sign in received for address: ${address}, socket id: ${socket.id}`);
this._signIn(socket, address.toLowerCase())
});
socket.on('disconnect', reason => {
debug(`[SOCKET] Disconnected: id=${socket.id}, reason=${reason}`);
});
}) })
} }
@ -23,11 +35,33 @@ class ClientNotification extends EventEmitter {
this.on(address, newMailListener) this.on(address, newMailListener)
// Deliver any pending notifications
const pending = this.pendingNotifications.get(address) || 0;
if (pending > 0) {
debug(`Delivering ${pending} pending notifications to ${address}`);
for (let i = 0; i < pending; i++) {
socket.emit('new emails');
}
this.pendingNotifications.delete(address);
}
socket.on('disconnect', reason => { socket.on('disconnect', reason => {
debug(`client disconnect: ${address} (${reason})`) debug(`client disconnect: ${address} (${reason})`)
this.removeListener(address, newMailListener) this.removeListener(address, newMailListener)
}) })
} }
emit(address) {
address = address.toLowerCase();
const hadListeners = super.emit(address);
if (!hadListeners) {
// Queue notification for later delivery
const prev = this.pendingNotifications.get(address) || 0;
this.pendingNotifications.set(address, prev + 1);
debug(`No listeners for ${address}, queued notification (${prev + 1} pending)`);
}
return hadListeners;
}
} }
module.exports = ClientNotification module.exports = ClientNotification

View file

@ -1,5 +1,5 @@
document.addEventListener('DOMContentLoaded', () => { document.addEventListener('DOMContentLoaded', () => {
const script = document.currentScript; const script = document.querySelector('script[data-address]');
const address = script ? script.dataset.address : ''; const address = script ? script.dataset.address : '';
if (address) { if (address) {
enableNewMessageNotifications(address, true); enableNewMessageNotifications(address, true);