mirror of
https://github.com/advplyr/audiobookshelf.git
synced 2025-05-13 01:19:55 +02:00
Update User.toJSONForPublic to remove mostRecent key and session key only includes the PlaybackSession
This commit is contained in:
parent
09c9c28028
commit
f465193b9c
@ -10,8 +10,11 @@ class SocketAuthority {
|
|||||||
this.clients = {}
|
this.clients = {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns an array of User.toJSONForPublic with `connections` for the # of socket connections
|
/**
|
||||||
// a user can have many socket connections
|
* returns an array of User.toJSONForPublic with `connections` for the # of socket connections
|
||||||
|
* a user can have many socket connections
|
||||||
|
* @returns {object[]}
|
||||||
|
*/
|
||||||
getUsersOnline() {
|
getUsersOnline() {
|
||||||
const onlineUsersMap = {}
|
const onlineUsersMap = {}
|
||||||
Object.values(this.clients).filter(c => c.user).forEach(client => {
|
Object.values(this.clients).filter(c => c.user).forEach(client => {
|
||||||
@ -19,7 +22,7 @@ class SocketAuthority {
|
|||||||
onlineUsersMap[client.user.id].connections++
|
onlineUsersMap[client.user.id].connections++
|
||||||
} else {
|
} else {
|
||||||
onlineUsersMap[client.user.id] = {
|
onlineUsersMap[client.user.id] = {
|
||||||
...client.user.toJSONForPublic(this.Server.playbackSessionManager.sessions, Database.libraryItems),
|
...client.user.toJSONForPublic(this.Server.playbackSessionManager.sessions),
|
||||||
connections: 1
|
connections: 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -108,7 +111,7 @@ class SocketAuthority {
|
|||||||
delete this.clients[socket.id]
|
delete this.clients[socket.id]
|
||||||
} else {
|
} else {
|
||||||
Logger.debug('[SocketAuthority] User Offline ' + _client.user.username)
|
Logger.debug('[SocketAuthority] User Offline ' + _client.user.username)
|
||||||
this.adminEmitter('user_offline', _client.user.toJSONForPublic(this.Server.playbackSessionManager.sessions, Database.libraryItems))
|
this.adminEmitter('user_offline', _client.user.toJSONForPublic(this.Server.playbackSessionManager.sessions))
|
||||||
|
|
||||||
const disconnectTime = Date.now() - _client.connected_at
|
const disconnectTime = Date.now() - _client.connected_at
|
||||||
Logger.info(`[SocketAuthority] Socket ${socket.id} disconnected from client "${_client.user.username}" after ${disconnectTime}ms (Reason: ${reason})`)
|
Logger.info(`[SocketAuthority] Socket ${socket.id} disconnected from client "${_client.user.username}" after ${disconnectTime}ms (Reason: ${reason})`)
|
||||||
@ -165,7 +168,7 @@ class SocketAuthority {
|
|||||||
|
|
||||||
Logger.debug(`[SocketAuthority] User Online ${client.user.username}`)
|
Logger.debug(`[SocketAuthority] User Online ${client.user.username}`)
|
||||||
|
|
||||||
this.adminEmitter('user_online', client.user.toJSONForPublic(this.Server.playbackSessionManager.sessions, Database.libraryItems))
|
this.adminEmitter('user_online', client.user.toJSONForPublic(this.Server.playbackSessionManager.sessions))
|
||||||
|
|
||||||
// Update user lastSeen
|
// Update user lastSeen
|
||||||
user.lastSeen = Date.now()
|
user.lastSeen = Date.now()
|
||||||
@ -191,7 +194,7 @@ class SocketAuthority {
|
|||||||
|
|
||||||
if (client.user) {
|
if (client.user) {
|
||||||
Logger.debug('[SocketAuthority] User Offline ' + client.user.username)
|
Logger.debug('[SocketAuthority] User Offline ' + client.user.username)
|
||||||
this.adminEmitter('user_offline', client.user.toJSONForPublic(null, Database.libraryItems))
|
this.adminEmitter('user_offline', client.user.toJSONForPublic())
|
||||||
}
|
}
|
||||||
|
|
||||||
delete this.clients[socketId].user
|
delete this.clients[socketId].user
|
||||||
|
@ -259,7 +259,7 @@ class PlaybackSessionManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.sessions.push(newPlaybackSession)
|
this.sessions.push(newPlaybackSession)
|
||||||
SocketAuthority.adminEmitter('user_stream_update', user.toJSONForPublic(this.sessions, Database.libraryItems))
|
SocketAuthority.adminEmitter('user_stream_update', user.toJSONForPublic(this.sessions))
|
||||||
|
|
||||||
return newPlaybackSession
|
return newPlaybackSession
|
||||||
}
|
}
|
||||||
@ -304,7 +304,7 @@ class PlaybackSessionManager {
|
|||||||
await this.saveSession(session)
|
await this.saveSession(session)
|
||||||
}
|
}
|
||||||
Logger.debug(`[PlaybackSessionManager] closeSession "${session.id}"`)
|
Logger.debug(`[PlaybackSessionManager] closeSession "${session.id}"`)
|
||||||
SocketAuthority.adminEmitter('user_stream_update', user.toJSONForPublic(this.sessions, Database.libraryItems))
|
SocketAuthority.adminEmitter('user_stream_update', user.toJSONForPublic(this.sessions))
|
||||||
SocketAuthority.clientEmitter(session.userId, 'user_session_closed', session.id)
|
SocketAuthority.clientEmitter(session.userId, 'user_session_closed', session.id)
|
||||||
return this.removeSession(session.id)
|
return this.removeSession(session.id)
|
||||||
}
|
}
|
||||||
|
@ -78,6 +78,11 @@ class PlaybackSession {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Session data to send to clients
|
||||||
|
* @param {[oldLibraryItem]} libraryItem optional
|
||||||
|
* @returns {object}
|
||||||
|
*/
|
||||||
toJSONForClient(libraryItem) {
|
toJSONForClient(libraryItem) {
|
||||||
return {
|
return {
|
||||||
id: this.id,
|
id: this.id,
|
||||||
@ -105,8 +110,8 @@ class PlaybackSession {
|
|||||||
startedAt: this.startedAt,
|
startedAt: this.startedAt,
|
||||||
updatedAt: this.updatedAt,
|
updatedAt: this.updatedAt,
|
||||||
audioTracks: this.audioTracks.map(at => at.toJSON()),
|
audioTracks: this.audioTracks.map(at => at.toJSON()),
|
||||||
videoTrack: this.videoTrack ? this.videoTrack.toJSON() : null,
|
videoTrack: this.videoTrack?.toJSON() || null,
|
||||||
libraryItem: libraryItem.toJSONExpanded()
|
libraryItem: libraryItem?.toJSONExpanded() || null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -117,23 +117,20 @@ class User {
|
|||||||
return json
|
return json
|
||||||
}
|
}
|
||||||
|
|
||||||
// Data broadcasted
|
/**
|
||||||
toJSONForPublic(sessions, libraryItems) {
|
* User data for clients
|
||||||
var userSession = sessions ? sessions.find(s => s.userId === this.id) : null
|
* @param {[oldPlaybackSession[]]} sessions optional array of open playback sessions
|
||||||
var session = null
|
* @returns {object}
|
||||||
if (userSession) {
|
*/
|
||||||
var libraryItem = libraryItems.find(li => li.id === userSession.libraryItemId)
|
toJSONForPublic(sessions) {
|
||||||
if (libraryItem) {
|
const userSession = sessions?.find(s => s.userId === this.id) || null
|
||||||
session = userSession.toJSONForClient(libraryItem)
|
const session = userSession?.toJSONForClient() || null
|
||||||
}
|
|
||||||
}
|
|
||||||
return {
|
return {
|
||||||
id: this.id,
|
id: this.id,
|
||||||
oldUserId: this.oldUserId,
|
oldUserId: this.oldUserId,
|
||||||
username: this.username,
|
username: this.username,
|
||||||
type: this.type,
|
type: this.type,
|
||||||
session,
|
session,
|
||||||
mostRecent: this.getMostRecentItemProgress(libraryItems),
|
|
||||||
lastSeen: this.lastSeen,
|
lastSeen: this.lastSeen,
|
||||||
createdAt: this.createdAt
|
createdAt: this.createdAt
|
||||||
}
|
}
|
||||||
@ -269,45 +266,6 @@ class User {
|
|||||||
return libraryIds.find(lid => this.checkCanAccessLibrary(lid)) || null
|
return libraryIds.find(lid => this.checkCanAccessLibrary(lid)) || null
|
||||||
}
|
}
|
||||||
|
|
||||||
// Returns most recent media progress w/ `media` object and optionally an `episode` object
|
|
||||||
getMostRecentItemProgress(libraryItems) {
|
|
||||||
if (!this.mediaProgress.length) return null
|
|
||||||
var mediaProgressObjects = this.mediaProgress.map(lip => lip.toJSON())
|
|
||||||
mediaProgressObjects.sort((a, b) => b.lastUpdate - a.lastUpdate)
|
|
||||||
|
|
||||||
var libraryItemMedia = null
|
|
||||||
var progressEpisode = null
|
|
||||||
// Find the most recent progress that still has a libraryItem and episode
|
|
||||||
var mostRecentProgress = mediaProgressObjects.find((progress) => {
|
|
||||||
const libraryItem = libraryItems.find(li => li.id === progress.libraryItemId)
|
|
||||||
if (!libraryItem) {
|
|
||||||
Logger.warn('[User] Library item not found for users progress ' + progress.libraryItemId)
|
|
||||||
return false
|
|
||||||
} else if (progress.episodeId) {
|
|
||||||
const episode = libraryItem.mediaType === 'podcast' ? libraryItem.media.getEpisode(progress.episodeId) : null
|
|
||||||
if (!episode) {
|
|
||||||
Logger.warn(`[User] Episode ${progress.episodeId} not found for user media progress, podcast: ${libraryItem.media.metadata.title}`)
|
|
||||||
return false
|
|
||||||
} else {
|
|
||||||
libraryItemMedia = libraryItem.media.toJSONExpanded()
|
|
||||||
progressEpisode = episode.toJSON()
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
libraryItemMedia = libraryItem.media.toJSONExpanded()
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
if (!mostRecentProgress) return null
|
|
||||||
|
|
||||||
return {
|
|
||||||
...mostRecentProgress,
|
|
||||||
media: libraryItemMedia,
|
|
||||||
episode: progressEpisode
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
getMediaProgress(libraryItemId, episodeId = null) {
|
getMediaProgress(libraryItemId, episodeId = null) {
|
||||||
if (!this.mediaProgress) return null
|
if (!this.mediaProgress) return null
|
||||||
return this.mediaProgress.find(lip => {
|
return this.mediaProgress.find(lip => {
|
||||||
|
Loading…
Reference in New Issue
Block a user