Update PlaybackSession to use new library item model

This commit is contained in:
advplyr 2025-01-03 11:16:03 -06:00
parent d205c6f734
commit c251f1899d
16 changed files with 284 additions and 193 deletions

View file

@ -249,10 +249,6 @@ class LibraryItem {
this.updatedAt = Date.now()
}
getDirectPlayTracklist(episodeId) {
return this.media.getDirectPlayTracklist(episodeId)
}
/**
* Save metadata.json file
* TODO: Move to new LibraryItem model

View file

@ -1,8 +1,6 @@
const date = require('../libs/dateAndTime')
const uuidv4 = require('uuid').v4
const serverVersion = require('../../package.json').version
const BookMetadata = require('./metadata/BookMetadata')
const PodcastMetadata = require('./metadata/PodcastMetadata')
const DeviceInfo = require('./DeviceInfo')
class PlaybackSession {
@ -60,7 +58,7 @@ class PlaybackSession {
bookId: this.bookId,
episodeId: this.episodeId,
mediaType: this.mediaType,
mediaMetadata: this.mediaMetadata?.toJSON() || null,
mediaMetadata: structuredClone(this.mediaMetadata),
chapters: (this.chapters || []).map((c) => ({ ...c })),
displayTitle: this.displayTitle,
displayAuthor: this.displayAuthor,
@ -82,7 +80,7 @@ class PlaybackSession {
/**
* Session data to send to clients
* @param {Object} [libraryItem] - old library item
* @param {import('../models/LibraryItem')} [libraryItem]
* @returns
*/
toJSONForClient(libraryItem) {
@ -94,7 +92,7 @@ class PlaybackSession {
bookId: this.bookId,
episodeId: this.episodeId,
mediaType: this.mediaType,
mediaMetadata: this.mediaMetadata?.toJSON() || null,
mediaMetadata: structuredClone(this.mediaMetadata),
chapters: (this.chapters || []).map((c) => ({ ...c })),
displayTitle: this.displayTitle,
displayAuthor: this.displayAuthor,
@ -112,7 +110,7 @@ class PlaybackSession {
startedAt: this.startedAt,
updatedAt: this.updatedAt,
audioTracks: this.audioTracks.map((at) => at.toJSON?.() || { ...at }),
libraryItem: libraryItem?.toJSONExpanded() || null
libraryItem: libraryItem?.toOldJSONExpanded() || null
}
}
@ -148,14 +146,7 @@ class PlaybackSession {
this.serverVersion = session.serverVersion
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.mediaMetadata = session.mediaMetadata
this.displayTitle = session.displayTitle || ''
this.displayAuthor = session.displayAuthor || ''
this.coverPath = session.coverPath
@ -205,6 +196,15 @@ class PlaybackSession {
}
}
/**
*
* @param {import('../models/LibraryItem')} libraryItem
* @param {*} userId
* @param {*} mediaPlayer
* @param {*} deviceInfo
* @param {*} startTime
* @param {*} episodeId
*/
setData(libraryItem, userId, mediaPlayer, deviceInfo, startTime, episodeId = null) {
this.id = uuidv4()
this.userId = userId
@ -213,13 +213,12 @@ class PlaybackSession {
this.bookId = episodeId ? null : libraryItem.media.id
this.episodeId = episodeId
this.mediaType = libraryItem.mediaType
this.mediaMetadata = libraryItem.media.metadata.clone()
this.mediaMetadata = libraryItem.media.oldMetadataToJSON()
this.chapters = libraryItem.media.getChapters(episodeId)
this.displayTitle = libraryItem.media.getPlaybackTitle(episodeId)
this.displayAuthor = libraryItem.media.getPlaybackAuthor()
this.coverPath = libraryItem.media.coverPath
this.setDuration(libraryItem, episodeId)
this.duration = libraryItem.media.getPlaybackDuration(episodeId)
this.mediaPlayer = mediaPlayer
this.deviceInfo = deviceInfo || new DeviceInfo()
@ -235,14 +234,6 @@ class PlaybackSession {
this.updatedAt = Date.now()
}
setDuration(libraryItem, episodeId) {
if (episodeId) {
this.duration = libraryItem.media.getEpisodeDuration(episodeId)
} else {
this.duration = libraryItem.media.duration
}
}
addListeningTime(timeListened) {
if (!timeListened || isNaN(timeListened)) return

View file

@ -18,6 +18,7 @@ class Stream extends EventEmitter {
this.id = sessionId
this.user = user
/** @type {import('../models/LibraryItem')} */
this.libraryItem = libraryItem
this.episodeId = episodeId
@ -40,31 +41,25 @@ class Stream extends EventEmitter {
this.furthestSegmentCreated = 0
}
get isPodcast() {
return this.libraryItem.mediaType === 'podcast'
}
/**
* @returns {import('../models/PodcastEpisode') | null}
*/
get episode() {
if (!this.isPodcast) return null
return this.libraryItem.media.episodes.find((ep) => ep.id === this.episodeId)
}
get libraryItemId() {
return this.libraryItem.id
if (!this.libraryItem.isPodcast) return null
return this.libraryItem.media.podcastEpisodes.find((ep) => ep.id === this.episodeId)
}
get mediaTitle() {
if (this.episode) return this.episode.title || ''
return this.libraryItem.media.metadata.title || ''
return this.libraryItem.media.getPlaybackTitle(this.episodeId)
}
get totalDuration() {
if (this.episode) return this.episode.duration
return this.libraryItem.media.duration
return this.libraryItem.media.getPlaybackDuration(this.episodeId)
}
get tracks() {
if (this.episode) return this.episode.tracks
return this.libraryItem.media.tracks
return this.libraryItem.getTrackList(this.episodeId)
}
get tracksAudioFileType() {
if (!this.tracks.length) return null
return this.tracks[0].metadata.format
return this.tracks[0].metadata.ext.slice(1)
}
get tracksMimeType() {
if (!this.tracks.length) return null
@ -116,8 +111,8 @@ class Stream extends EventEmitter {
return {
id: this.id,
userId: this.user.id,
libraryItem: this.libraryItem.toJSONExpanded(),
episode: this.episode ? this.episode.toJSONExpanded() : null,
libraryItem: this.libraryItem.toOldJSONExpanded(),
episode: this.episode ? this.episode.toOldJSONExpanded(this.libraryItem.id) : null,
segmentLength: this.segmentLength,
playlistPath: this.playlistPath,
clientPlaylistUri: this.clientPlaylistUri,

View file

@ -168,16 +168,6 @@ class PodcastEpisode {
return hasUpdates
}
// Only checks container format
checkCanDirectPlay(payload) {
const supportedMimeTypes = payload.supportedMimeTypes || []
return supportedMimeTypes.includes(this.audioFile.mimeType)
}
getDirectPlayTracklist() {
return this.tracks
}
checkEqualsEnclosureUrl(url) {
if (!this.enclosure?.url) return false
return this.enclosure.url == url

View file

@ -150,27 +150,5 @@ class Book {
this.coverPath = coverPath
return true
}
// Only checks container format
checkCanDirectPlay(payload) {
var supportedMimeTypes = payload.supportedMimeTypes || []
return !this.tracks.some((t) => !supportedMimeTypes.includes(t.mimeType))
}
getDirectPlayTracklist() {
return this.tracks
}
getPlaybackTitle() {
return this.metadata.title
}
getPlaybackAuthor() {
return this.metadata.authorName
}
getChapters() {
return this.chapters?.map((ch) => ({ ...ch })) || []
}
}
module.exports = Book

View file

@ -199,19 +199,6 @@ class Podcast {
return this.episodes.some((ep) => (ep.guid && ep.guid === guid) || ep.checkEqualsEnclosureUrl(url))
}
// Only checks container format
checkCanDirectPlay(payload, episodeId) {
var episode = this.episodes.find((ep) => ep.id === episodeId)
if (!episode) return false
return episode.checkCanDirectPlay(payload)
}
getDirectPlayTracklist(episodeId) {
var episode = this.episodes.find((ep) => ep.id === episodeId)
if (!episode) return false
return episode.getDirectPlayTracklist()
}
addPodcastEpisode(podcastEpisode) {
this.episodes.push(podcastEpisode)
}
@ -224,22 +211,6 @@ class Podcast {
return episode
}
getPlaybackTitle(episodeId) {
var episode = this.episodes.find((ep) => ep.id == episodeId)
if (!episode) return this.metadata.title
return episode.title
}
getPlaybackAuthor() {
return this.metadata.author
}
getEpisodeDuration(episodeId) {
var episode = this.episodes.find((ep) => ep.id == episodeId)
if (!episode) return 0
return episode.duration
}
getEpisode(episodeId) {
if (!episodeId) return null
@ -248,9 +219,5 @@ class Podcast {
return this.episodes.find((ep) => ep.id == episodeId)
}
getChapters(episodeId) {
return this.getEpisode(episodeId)?.chapters?.map((ch) => ({ ...ch })) || []
}
}
module.exports = Podcast

View file

@ -159,11 +159,6 @@ class BookMetadata {
getSeries(seriesId) {
return this.series.find((se) => se.id == seriesId)
}
getSeriesSequence(seriesId) {
const series = this.series.find((se) => se.id == seriesId)
if (!series) return null
return series.sequence || ''
}
update(payload) {
const json = this.toJSON()