2023-11-01 11:48:19 +01:00
|
|
|
const EventEmitter = require('events')
|
|
|
|
const debug = require('debug')('48hr-email:notification')
|
2024-09-17 19:50:52 +02:00
|
|
|
require('events').defaultMaxListeners = 50;
|
2023-11-01 11:48:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Receives sign-ins from users and notifies them when new mails are available.
|
|
|
|
*/
|
|
|
|
class ClientNotification extends EventEmitter {
|
|
|
|
use(io) {
|
|
|
|
io.on('connection', socket => {
|
|
|
|
socket.on('sign in', address => this._signIn(socket, address))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
_signIn(socket, address) {
|
|
|
|
debug(`socketio signed in: ${address}`)
|
|
|
|
|
|
|
|
const newMailListener = () => {
|
|
|
|
debug(`${address} has new messages, sending notification`)
|
|
|
|
socket.emit('new emails')
|
|
|
|
}
|
|
|
|
|
|
|
|
this.on(address, newMailListener)
|
|
|
|
|
|
|
|
socket.on('disconnect', reason => {
|
|
|
|
debug(`client disconnect: ${address} (${reason})`)
|
|
|
|
this.removeListener(address, newMailListener)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = ClientNotification
|