audiobookshelf/server/objects/PlaybackSession.js

260 lines
7.7 KiB
JavaScript
Raw Normal View History

2022-07-07 02:18:27 +02:00
const date = require('../libs/dateAndTime')
2024-06-23 18:01:25 +02:00
const uuidv4 = require('uuid').v4
2023-07-05 01:14:44 +02:00
const serverVersion = require('../../package.json').version
const BookMetadata = require('./metadata/BookMetadata')
const PodcastMetadata = require('./metadata/PodcastMetadata')
const DeviceInfo = require('./DeviceInfo')
class PlaybackSession {
constructor(session) {
this.id = null
this.userId = null
this.libraryId = null
this.libraryItemId = null
2023-07-05 01:14:44 +02:00
this.bookId = null
this.episodeId = null
this.mediaType = null
this.mediaMetadata = null
2022-04-02 18:41:17 +02:00
this.chapters = null
this.displayTitle = null
this.displayAuthor = null
this.coverPath = null
this.duration = null
this.playMethod = null
2022-04-02 18:19:57 +02:00
this.mediaPlayer = null
this.deviceInfo = null
2023-07-05 01:14:44 +02:00
this.serverVersion = null
this.date = null
this.dayOfWeek = null
this.timeListening = null
this.startTime = null // media current time at start of playback
this.currentTime = 0 // Last current time set
this.startedAt = null
this.updatedAt = null
// Not saved in DB
this.lastSave = 0
this.audioTracks = []
this.stream = null
// Used for share sessions
this.shareSessionId = null
this.mediaItemShareId = null
this.coverAspectRatio = null
if (session) {
this.construct(session)
}
}
toJSON() {
return {
id: this.id,
userId: this.userId,
libraryId: this.libraryId,
libraryItemId: this.libraryItemId,
2023-07-05 01:14:44 +02:00
bookId: this.bookId,
episodeId: this.episodeId,
mediaType: this.mediaType,
mediaMetadata: this.mediaMetadata?.toJSON() || null,
2024-06-23 18:01:25 +02:00
chapters: (this.chapters || []).map((c) => ({ ...c })),
displayTitle: this.displayTitle,
displayAuthor: this.displayAuthor,
coverPath: this.coverPath,
duration: this.duration,
playMethod: this.playMethod,
2022-04-02 18:19:57 +02:00
mediaPlayer: this.mediaPlayer,
deviceInfo: this.deviceInfo?.toJSON() || null,
2023-07-05 01:14:44 +02:00
serverVersion: this.serverVersion,
date: this.date,
dayOfWeek: this.dayOfWeek,
timeListening: this.timeListening,
startTime: this.startTime,
currentTime: this.currentTime,
startedAt: this.startedAt,
updatedAt: this.updatedAt
}
}
/**
* Session data to send to clients
* @param {Object} [libraryItem] - old library item
* @returns
*/
toJSONForClient(libraryItem) {
return {
id: this.id,
userId: this.userId,
libraryId: this.libraryId,
libraryItemId: this.libraryItemId,
2023-07-05 01:14:44 +02:00
bookId: this.bookId,
episodeId: this.episodeId,
mediaType: this.mediaType,
mediaMetadata: this.mediaMetadata?.toJSON() || null,
2024-06-23 18:01:25 +02:00
chapters: (this.chapters || []).map((c) => ({ ...c })),
displayTitle: this.displayTitle,
displayAuthor: this.displayAuthor,
coverPath: this.coverPath,
duration: this.duration,
playMethod: this.playMethod,
2022-04-02 18:19:57 +02:00
mediaPlayer: this.mediaPlayer,
deviceInfo: this.deviceInfo?.toJSON() || null,
2023-07-05 01:14:44 +02:00
serverVersion: this.serverVersion,
date: this.date,
dayOfWeek: this.dayOfWeek,
timeListening: this.timeListening,
startTime: this.startTime,
currentTime: this.currentTime,
startedAt: this.startedAt,
updatedAt: this.updatedAt,
audioTracks: this.audioTracks.map((at) => at.toJSON?.() || { ...at }),
libraryItem: libraryItem?.toJSONExpanded() || null
}
}
construct(session) {
this.id = session.id
this.userId = session.userId
this.libraryId = session.libraryId || null
this.libraryItemId = session.libraryItemId
this.bookId = session.bookId || null
this.episodeId = session.episodeId
this.mediaType = session.mediaType
this.duration = session.duration
this.playMethod = session.playMethod
2022-04-02 18:19:57 +02:00
this.mediaPlayer = session.mediaPlayer || null
2023-07-05 01:14:44 +02:00
// Temp do not store old IDs
if (this.libraryId?.startsWith('lib_')) {
this.libraryId = null
}
if (this.libraryItemId?.startsWith('li_') || this.libraryItemId?.startsWith('local_')) {
this.libraryItemId = null
}
if (this.episodeId?.startsWith('ep_') || this.episodeId?.startsWith('local_')) {
this.episodeId = null
}
2023-07-05 01:14:44 +02:00
if (session.deviceInfo instanceof DeviceInfo) {
this.deviceInfo = new DeviceInfo(session.deviceInfo.toJSON())
} else {
this.deviceInfo = new DeviceInfo(session.deviceInfo)
}
this.serverVersion = session.serverVersion
2022-04-02 18:41:17 +02:00
this.chapters = session.chapters || []
this.mediaMetadata = null
if (session.mediaMetadata) {
if (this.mediaType === 'book') {
this.mediaMetadata = new BookMetadata(session.mediaMetadata)
} else if (this.mediaType === 'podcast') {
this.mediaMetadata = new PodcastMetadata(session.mediaMetadata)
}
}
this.displayTitle = session.displayTitle || ''
this.displayAuthor = session.displayAuthor || ''
this.coverPath = session.coverPath
this.date = session.date
this.dayOfWeek = session.dayOfWeek
this.timeListening = session.timeListening || null
this.startTime = session.startTime || 0
this.currentTime = session.currentTime || 0
this.startedAt = session.startedAt
this.updatedAt = session.updatedAt || session.startedAt
// Local playback sessions dont set this date field so set using updatedAt
if (!this.date && session.updatedAt) {
this.date = date.format(new Date(session.updatedAt), 'YYYY-MM-DD')
this.dayOfWeek = date.format(new Date(session.updatedAt), 'dddd')
}
}
get mediaItemId() {
if (this.episodeId) return `${this.libraryItemId}-${this.episodeId}`
return this.libraryItemId
}
2024-06-23 18:01:25 +02:00
get progress() {
// Value between 0 and 1
if (!this.duration) return 0
return Math.max(0, Math.min(this.currentTime / this.duration, 1))
}
get deviceId() {
2023-07-05 01:14:44 +02:00
return this.deviceInfo?.id
}
2023-02-04 20:23:13 +01:00
get deviceDescription() {
if (!this.deviceInfo) return 'No Device Info'
return this.deviceInfo.deviceDescription
}
get mediaProgressObject() {
return {
duration: this.duration,
currentTime: this.currentTime,
progress: this.progress,
lastUpdate: this.updatedAt
}
}
2024-06-23 18:01:25 +02:00
setData(libraryItem, userId, mediaPlayer, deviceInfo, startTime, episodeId = null) {
2023-07-05 01:14:44 +02:00
this.id = uuidv4()
2024-06-23 18:01:25 +02:00
this.userId = userId
this.libraryId = libraryItem.libraryId
this.libraryItemId = libraryItem.id
2023-07-05 01:14:44 +02:00
this.bookId = episodeId ? null : libraryItem.media.id
this.episodeId = episodeId
this.mediaType = libraryItem.mediaType
this.mediaMetadata = libraryItem.media.metadata.clone()
this.chapters = libraryItem.media.getChapters(episodeId)
this.displayTitle = libraryItem.media.getPlaybackTitle(episodeId)
this.displayAuthor = libraryItem.media.getPlaybackAuthor()
this.coverPath = libraryItem.media.coverPath
2024-09-12 20:05:08 +02:00
this.setDuration(libraryItem, episodeId)
2022-04-02 18:19:57 +02:00
this.mediaPlayer = mediaPlayer
this.deviceInfo = deviceInfo || new DeviceInfo()
2023-07-05 01:14:44 +02:00
this.serverVersion = serverVersion
this.timeListening = 0
this.startTime = startTime
this.currentTime = startTime
this.date = date.format(new Date(), 'YYYY-MM-DD')
this.dayOfWeek = date.format(new Date(), 'dddd')
this.startedAt = Date.now()
this.updatedAt = Date.now()
}
2024-09-12 20:05:08 +02:00
setDuration(libraryItem, episodeId) {
if (episodeId) {
this.duration = libraryItem.media.getEpisodeDuration(episodeId)
} else {
this.duration = libraryItem.media.duration
}
}
addListeningTime(timeListened) {
if (!timeListened || isNaN(timeListened)) return
if (!this.date) {
// Set date info on first listening update
this.date = date.format(new Date(), 'YYYY-MM-DD')
this.dayOfWeek = date.format(new Date(), 'dddd')
}
2022-04-15 12:59:42 +02:00
this.timeListening += Number.parseFloat(timeListened)
this.updatedAt = Date.now()
}
}
2024-06-23 18:01:25 +02:00
module.exports = PlaybackSession