diff --git a/server/controllers/ShareController.js b/server/controllers/ShareController.js index ca201f94..bd1a5ff5 100644 --- a/server/controllers/ShareController.js +++ b/server/controllers/ShareController.js @@ -1,6 +1,6 @@ +const { Op } = require('sequelize') const Logger = require('../Logger') const Database = require('../Database') -const { Op } = require('sequelize') const ShareManager = require('../managers/ShareManager') @@ -34,6 +34,7 @@ class ShareController { if (!mediaItemShare.mediaItem) { return res.status(404).send('Media item not found') } + res.json(mediaItemShare) } catch (error) { Logger.error(`[ShareController] Failed`, error) diff --git a/server/managers/PlaybackSessionManager.js b/server/managers/PlaybackSessionManager.js index 90acbd9a..1d292b3d 100644 --- a/server/managers/PlaybackSessionManager.js +++ b/server/managers/PlaybackSessionManager.js @@ -1,4 +1,4 @@ -const uuidv4 = require("uuid").v4 +const uuidv4 = require('uuid').v4 const Path = require('path') const serverVersion = require('../../package.json').version const Logger = require('../Logger') @@ -25,10 +25,10 @@ class PlaybackSessionManager { } getSession(sessionId) { - return this.sessions.find(s => s.id === sessionId) + return this.sessions.find((s) => s.id === sessionId) } getUserSession(userId) { - return this.sessions.find(s => s.userId === userId) + return this.sessions.find((s) => s.userId === userId) } getStream(sessionId) { const session = this.getSession(sessionId) @@ -59,6 +59,12 @@ class PlaybackSessionManager { return deviceInfo } + /** + * + * @param {import('express').Request} req + * @param {import('express').Response} res + * @param {string} [episodeId] + */ async startSessionRequest(req, res, episodeId) { const deviceInfo = await this.getDeviceInfo(req) Logger.debug(`[PlaybackSessionManager] startSessionRequest for device ${deviceInfo.deviceDescription}`) @@ -94,7 +100,7 @@ class PlaybackSessionManager { async syncLocalSession(user, sessionJson, deviceInfo) { const libraryItem = await Database.libraryItemModel.getOldById(sessionJson.libraryItemId) - const episode = (sessionJson.episodeId && libraryItem && libraryItem.isPodcast) ? libraryItem.media.getEpisode(sessionJson.episodeId) : null + const episode = sessionJson.episodeId && libraryItem && libraryItem.isPodcast ? libraryItem.media.getEpisode(sessionJson.episodeId) : null if (!libraryItem || (libraryItem.isPodcast && !episode)) { Logger.error(`[PlaybackSessionManager] syncLocalSession: Media item not found for session "${sessionJson.displayTitle}" (${sessionJson.id})`) return { @@ -209,9 +215,18 @@ class PlaybackSessionManager { res.sendStatus(200) } + /** + * + * @param {import('../objects/user/User')} user + * @param {DeviceInfo} deviceInfo + * @param {import('../objects/LibraryItem')} libraryItem + * @param {string|null} episodeId + * @param {{forceDirectPlay?:boolean, forceTranscode?:boolean, mediaPlayer:string, supportedMimeTypes?:string[]}} options + * @returns {Promise} + */ async startSession(user, deviceInfo, libraryItem, episodeId, options) { // Close any sessions already open for user and device - const userSessions = this.sessions.filter(playbackSession => playbackSession.userId === user.id && playbackSession.deviceId === deviceInfo.id) + const userSessions = this.sessions.filter((playbackSession) => playbackSession.userId === user.id && playbackSession.deviceId === deviceInfo.id) for (const session of userSessions) { Logger.info(`[PlaybackSessionManager] startSession: Closing open session "${session.displayTitle}" for user "${user.username}" (Device: ${session.deviceDescription})`) await this.closeSession(user, session, null) @@ -231,7 +246,7 @@ class PlaybackSessionManager { } } const newPlaybackSession = new PlaybackSession() - newPlaybackSession.setData(libraryItem, user, mediaPlayer, deviceInfo, userStartTime, episodeId) + newPlaybackSession.setData(libraryItem, user.id, mediaPlayer, deviceInfo, userStartTime, episodeId) if (libraryItem.mediaType === 'video') { if (shouldDirectPlay) { @@ -328,12 +343,12 @@ class PlaybackSessionManager { } async removeSession(sessionId) { - const session = this.sessions.find(s => s.id === sessionId) + const session = this.sessions.find((s) => s.id === sessionId) if (!session) return if (session.stream) { await session.stream.close() } - this.sessions = this.sessions.filter(s => s.id !== sessionId) + this.sessions = this.sessions.filter((s) => s.id !== sessionId) Logger.debug(`[PlaybackSessionManager] Removed session "${sessionId}"`) } @@ -345,8 +360,9 @@ class PlaybackSessionManager { try { const streamsInPath = await fs.readdir(this.StreamsPath) for (const streamId of streamsInPath) { - if (/[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/.test(streamId)) { // Ensure is uuidv4 - const session = this.sessions.find(se => se.id === streamId) + if (/[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/.test(streamId)) { + // Ensure is uuidv4 + const session = this.sessions.find((se) => se.id === streamId) if (!session) { const streamPath = Path.join(this.StreamsPath, streamId) Logger.debug(`[PlaybackSessionManager] Removing orphan stream "${streamPath}"`) diff --git a/server/objects/PlaybackSession.js b/server/objects/PlaybackSession.js index 20b50009..d2890a11 100644 --- a/server/objects/PlaybackSession.js +++ b/server/objects/PlaybackSession.js @@ -1,5 +1,5 @@ const date = require('../libs/dateAndTime') -const uuidv4 = require("uuid").v4 +const uuidv4 = require('uuid').v4 const serverVersion = require('../../package.json').version const BookMetadata = require('./metadata/BookMetadata') const PodcastMetadata = require('./metadata/PodcastMetadata') @@ -59,7 +59,7 @@ class PlaybackSession { episodeId: this.episodeId, mediaType: this.mediaType, mediaMetadata: this.mediaMetadata?.toJSON() || null, - chapters: (this.chapters || []).map(c => ({ ...c })), + chapters: (this.chapters || []).map((c) => ({ ...c })), displayTitle: this.displayTitle, displayAuthor: this.displayAuthor, coverPath: this.coverPath, @@ -93,7 +93,7 @@ class PlaybackSession { episodeId: this.episodeId, mediaType: this.mediaType, mediaMetadata: this.mediaMetadata?.toJSON() || null, - chapters: (this.chapters || []).map(c => ({ ...c })), + chapters: (this.chapters || []).map((c) => ({ ...c })), displayTitle: this.displayTitle, displayAuthor: this.displayAuthor, coverPath: this.coverPath, @@ -109,7 +109,7 @@ class PlaybackSession { currentTime: this.currentTime, startedAt: this.startedAt, updatedAt: this.updatedAt, - audioTracks: this.audioTracks.map(at => at.toJSON()), + audioTracks: this.audioTracks.map((at) => at.toJSON()), videoTrack: this.videoTrack?.toJSON() || null, libraryItem: libraryItem?.toJSONExpanded() || null } @@ -182,7 +182,8 @@ class PlaybackSession { return this.libraryItemId } - get progress() { // Value between 0 and 1 + get progress() { + // Value between 0 and 1 if (!this.duration) return 0 return Math.max(0, Math.min(this.currentTime / this.duration, 1)) } @@ -205,9 +206,9 @@ class PlaybackSession { } } - setData(libraryItem, user, mediaPlayer, deviceInfo, startTime, episodeId = null) { + setData(libraryItem, userId, mediaPlayer, deviceInfo, startTime, episodeId = null) { this.id = uuidv4() - this.userId = user.id + this.userId = userId this.libraryId = libraryItem.libraryId this.libraryItemId = libraryItem.id this.bookId = episodeId ? null : libraryItem.media.id @@ -258,4 +259,4 @@ class PlaybackSession { return date.format(new Date(), 'YYYY-MM-DD') !== this.date } } -module.exports = PlaybackSession \ No newline at end of file +module.exports = PlaybackSession