mirror of
https://github.com/Chocobozzz/PeerTube.git
synced 2025-10-05 02:39:33 +02:00
Add user notification base code
This commit is contained in:
parent
1de1d05f4c
commit
cef534ed53
52 changed files with 2479 additions and 141 deletions
52
server/lib/peertube-socket.ts
Normal file
52
server/lib/peertube-socket.ts
Normal file
|
@ -0,0 +1,52 @@
|
|||
import * as SocketIO from 'socket.io'
|
||||
import { authenticateSocket } from '../middlewares'
|
||||
import { UserNotificationModel } from '../models/account/user-notification'
|
||||
import { logger } from '../helpers/logger'
|
||||
import { Server } from 'http'
|
||||
|
||||
class PeerTubeSocket {
|
||||
|
||||
private static instance: PeerTubeSocket
|
||||
|
||||
private userNotificationSockets: { [ userId: number ]: SocketIO.Socket } = {}
|
||||
|
||||
private constructor () {}
|
||||
|
||||
init (server: Server) {
|
||||
const io = SocketIO(server)
|
||||
|
||||
io.of('/user-notifications')
|
||||
.use(authenticateSocket)
|
||||
.on('connection', socket => {
|
||||
const userId = socket.handshake.query.user.id
|
||||
|
||||
logger.debug('User %d connected on the notification system.', userId)
|
||||
|
||||
this.userNotificationSockets[userId] = socket
|
||||
|
||||
socket.on('disconnect', () => {
|
||||
logger.debug('User %d disconnected from SocketIO notifications.', userId)
|
||||
|
||||
delete this.userNotificationSockets[userId]
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
sendNotification (userId: number, notification: UserNotificationModel) {
|
||||
const socket = this.userNotificationSockets[userId]
|
||||
|
||||
if (!socket) return
|
||||
|
||||
socket.emit('new-notification', notification.toFormattedJSON())
|
||||
}
|
||||
|
||||
static get Instance () {
|
||||
return this.instance || (this.instance = new this())
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
export {
|
||||
PeerTubeSocket
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue