mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-01-03 00:06:46 +01:00
Update:Socket event for getting online users & test event for messaging all online users
This commit is contained in:
parent
64a8a046c1
commit
77a86d92f4
@ -330,6 +330,9 @@ export default {
|
|||||||
this.$toast.info(toast)
|
this.$toast.info(toast)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
adminMessageEvt(message) {
|
||||||
|
this.$toast.info(message)
|
||||||
|
},
|
||||||
initializeSocket() {
|
initializeSocket() {
|
||||||
this.socket = this.$nuxtSocket({
|
this.socket = this.$nuxtSocket({
|
||||||
name: process.env.NODE_ENV === 'development' ? 'dev' : 'prod',
|
name: process.env.NODE_ENV === 'development' ? 'dev' : 'prod',
|
||||||
@ -400,6 +403,8 @@ export default {
|
|||||||
this.socket.on('backup_applied', this.backupApplied)
|
this.socket.on('backup_applied', this.backupApplied)
|
||||||
|
|
||||||
this.socket.on('batch_quickmatch_complete', this.batchQuickMatchComplete)
|
this.socket.on('batch_quickmatch_complete', this.batchQuickMatchComplete)
|
||||||
|
|
||||||
|
this.socket.on('admin_message', this.adminMessageEvt)
|
||||||
},
|
},
|
||||||
showUpdateToast(versionData) {
|
showUpdateToast(versionData) {
|
||||||
var ignoreVersion = localStorage.getItem('ignoreVersion')
|
var ignoreVersion = localStorage.getItem('ignoreVersion')
|
||||||
|
@ -95,10 +95,21 @@ class Server {
|
|||||||
this.clients = {}
|
this.clients = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// returns an array of User.toJSONForPublic with `connections` for the # of socket connections
|
||||||
|
// a user can have many socket connections
|
||||||
getUsersOnline() {
|
getUsersOnline() {
|
||||||
return Object.values(this.clients).filter(c => c.user).map(client => {
|
const onlineUsersMap = {}
|
||||||
return client.user.toJSONForPublic(this.playbackSessionManager.sessions, this.db.libraryItems)
|
Object.values(this.clients).filter(c => c.user).forEach(client => {
|
||||||
|
if (onlineUsersMap[client.user.id]) {
|
||||||
|
onlineUsersMap[client.user.id].connections++
|
||||||
|
} else {
|
||||||
|
onlineUsersMap[client.user.id] = {
|
||||||
|
...client.user.toJSONForPublic(this.playbackSessionManager.sessions, this.db.libraryItems),
|
||||||
|
connections: 1
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
return Object.values(onlineUsersMap)
|
||||||
}
|
}
|
||||||
|
|
||||||
getClientsForUser(userId) {
|
getClientsForUser(userId) {
|
||||||
@ -288,6 +299,7 @@ class Server {
|
|||||||
|
|
||||||
Logger.info('[Server] Socket Connected', socket.id)
|
Logger.info('[Server] Socket Connected', socket.id)
|
||||||
|
|
||||||
|
// Required for associating a User with a socket
|
||||||
socket.on('auth', (token) => this.authenticateSocket(socket, token))
|
socket.on('auth', (token) => this.authenticateSocket(socket, token))
|
||||||
|
|
||||||
// Scanning
|
// Scanning
|
||||||
@ -298,17 +310,29 @@ class Server {
|
|||||||
socket.on('remove_log_listener', () => Logger.removeSocketListener(socket.id))
|
socket.on('remove_log_listener', () => Logger.removeSocketListener(socket.id))
|
||||||
socket.on('fetch_daily_logs', () => this.logManager.socketRequestDailyLogs(socket))
|
socket.on('fetch_daily_logs', () => this.logManager.socketRequestDailyLogs(socket))
|
||||||
|
|
||||||
|
// Events for testing
|
||||||
|
socket.on('message_all_users', (payload) => {
|
||||||
|
// admin user can send a message to all authenticated users
|
||||||
|
// displays on the web app as a toast
|
||||||
|
const client = this.clients[socket.id] || {}
|
||||||
|
if (client.user && client.user.isAdminOrUp) {
|
||||||
|
this.emitter('admin_message', payload.message || '')
|
||||||
|
} else {
|
||||||
|
Logger.error(`[Server] Non-admin user sent the message_all_users event`)
|
||||||
|
}
|
||||||
|
})
|
||||||
socket.on('ping', () => {
|
socket.on('ping', () => {
|
||||||
var client = this.clients[socket.id] || {}
|
const client = this.clients[socket.id] || {}
|
||||||
var user = client.user || {}
|
const user = client.user || {}
|
||||||
Logger.debug(`[Server] Received ping from socket ${user.username || 'No User'}`)
|
Logger.debug(`[Server] Received ping from socket ${user.username || 'No User'}`)
|
||||||
socket.emit('pong')
|
socket.emit('pong')
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Sent automatically from socket.io clients
|
||||||
socket.on('disconnect', (reason) => {
|
socket.on('disconnect', (reason) => {
|
||||||
Logger.removeSocketListener(socket.id)
|
Logger.removeSocketListener(socket.id)
|
||||||
|
|
||||||
var _client = this.clients[socket.id]
|
const _client = this.clients[socket.id]
|
||||||
if (!_client) {
|
if (!_client) {
|
||||||
Logger.warn(`[Server] Socket ${socket.id} disconnect, no client (Reason: ${reason})`)
|
Logger.warn(`[Server] Socket ${socket.id} disconnect, no client (Reason: ${reason})`)
|
||||||
} else if (!_client.user) {
|
} else if (!_client.user) {
|
||||||
@ -446,13 +470,15 @@ class Server {
|
|||||||
res.sendStatus(200)
|
res.sendStatus(200)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// When setting up a socket connection the user needs to be associated with a socket id
|
||||||
|
// for this the client will send a 'auth' event that includes the users API token
|
||||||
async authenticateSocket(socket, token) {
|
async authenticateSocket(socket, token) {
|
||||||
var user = await this.auth.authenticateUser(token)
|
const user = await this.auth.authenticateUser(token)
|
||||||
if (!user) {
|
if (!user) {
|
||||||
Logger.error('Cannot validate socket - invalid token')
|
Logger.error('Cannot validate socket - invalid token')
|
||||||
return socket.emit('invalid_token')
|
return socket.emit('invalid_token')
|
||||||
}
|
}
|
||||||
var client = this.clients[socket.id]
|
const client = this.clients[socket.id]
|
||||||
|
|
||||||
if (client.user !== undefined) {
|
if (client.user !== undefined) {
|
||||||
Logger.debug(`[Server] Authenticating socket client already has user`, client.user.username)
|
Logger.debug(`[Server] Authenticating socket client already has user`, client.user.username)
|
||||||
@ -467,6 +493,7 @@ class Server {
|
|||||||
|
|
||||||
Logger.debug(`[Server] User Online ${client.user.username}`)
|
Logger.debug(`[Server] User Online ${client.user.username}`)
|
||||||
|
|
||||||
|
// TODO: Send to authenticated clients only
|
||||||
this.io.emit('user_online', client.user.toJSONForPublic(this.playbackSessionManager.sessions, this.db.libraryItems))
|
this.io.emit('user_online', client.user.toJSONForPublic(this.playbackSessionManager.sessions, this.db.libraryItems))
|
||||||
|
|
||||||
user.lastSeen = Date.now()
|
user.lastSeen = Date.now()
|
||||||
|
Loading…
Reference in New Issue
Block a user