diff --git a/server/SocketAuthority.js b/server/SocketAuthority.js index 050e7e2f2..19c686d97 100644 --- a/server/SocketAuthority.js +++ b/server/SocketAuthority.js @@ -84,42 +84,6 @@ class SocketAuthority { } } - /** - * Emits event with library item to all clients that can access the library item - * Note: Emits toOldJSONExpanded() - * - * @param {string} evt - * @param {import('./models/LibraryItem')} libraryItem - */ - libraryItemEmitter(evt, libraryItem) { - for (const socketId in this.clients) { - if (this.clients[socketId].user?.checkCanAccessLibraryItem(libraryItem)) { - this.clients[socketId].socket.emit(evt, libraryItem.toOldJSONExpanded()) - } - } - } - - /** - * Emits event with library items to all clients that can access the library items - * Note: Emits toOldJSONExpanded() - * - * @param {string} evt - * @param {import('./models/LibraryItem')[]} libraryItems - */ - libraryItemsEmitter(evt, libraryItems) { - for (const socketId in this.clients) { - if (this.clients[socketId].user) { - const libraryItemsAccessibleToUser = libraryItems.filter((li) => this.clients[socketId].user.checkCanAccessLibraryItem(li)) - if (libraryItemsAccessibleToUser.length) { - this.clients[socketId].socket.emit( - evt, - libraryItemsAccessibleToUser.map((li) => li.toOldJSONExpanded()) - ) - } - } - } - } - /** * Closes the Socket.IO server and disconnect all clients * diff --git a/server/controllers/AuthorController.js b/server/controllers/AuthorController.js index 50eeda31a..471508837 100644 --- a/server/controllers/AuthorController.js +++ b/server/controllers/AuthorController.js @@ -152,7 +152,10 @@ class AuthorController { for (const libraryItem of libraryItems) { await libraryItem.saveMetadataFile() } - SocketAuthority.libraryItemsEmitter('items_updated', libraryItems) + SocketAuthority.emitter( + 'items_updated', + libraryItems.map((li) => li.toOldJSONExpanded()) + ) } // Remove old author @@ -207,7 +210,10 @@ class AuthorController { } if (libraryItems.length) { - SocketAuthority.libraryItemsEmitter('items_updated', libraryItems) + SocketAuthority.emitter( + 'items_updated', + libraryItems.map((li) => li.toOldJSONExpanded()) + ) } } else { numBooksForAuthor = await Database.bookAuthorModel.getCountForAuthor(req.author.id) diff --git a/server/controllers/LibraryController.js b/server/controllers/LibraryController.js index e63441f0b..f9aeba3f4 100644 --- a/server/controllers/LibraryController.js +++ b/server/controllers/LibraryController.js @@ -1188,7 +1188,10 @@ class LibraryController { } if (itemsUpdated.length) { - SocketAuthority.libraryItemsEmitter('items_updated', itemsUpdated) + SocketAuthority.emitter( + 'items_updated', + itemsUpdated.map((li) => li.toOldJSONExpanded()) + ) } res.json({ @@ -1229,7 +1232,10 @@ class LibraryController { } if (itemsUpdated.length) { - SocketAuthority.libraryItemsEmitter('items_updated', itemsUpdated) + SocketAuthority.emitter( + 'items_updated', + itemsUpdated.map((li) => li.toOldJSONExpanded()) + ) } res.json({ diff --git a/server/controllers/LibraryItemController.js b/server/controllers/LibraryItemController.js index 768a23dcb..2e696ff00 100644 --- a/server/controllers/LibraryItemController.js +++ b/server/controllers/LibraryItemController.js @@ -253,7 +253,7 @@ class LibraryItemController { } Logger.debug(`[LibraryItemController] Updated library item media ${req.libraryItem.media.title}`) - SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem) + SocketAuthority.emitter('item_updated', req.libraryItem.toOldJSONExpanded()) } res.json({ updated: hasUpdates, @@ -300,7 +300,7 @@ class LibraryItemController { req.libraryItem.changed('updatedAt', true) await req.libraryItem.save() - SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem) + SocketAuthority.emitter('item_updated', req.libraryItem.toOldJSONExpanded()) res.json({ success: true, cover: result.cover @@ -332,7 +332,7 @@ class LibraryItemController { req.libraryItem.changed('updatedAt', true) await req.libraryItem.save() - SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem) + SocketAuthority.emitter('item_updated', req.libraryItem.toOldJSONExpanded()) } res.json({ success: true, @@ -358,7 +358,7 @@ class LibraryItemController { await CacheManager.purgeCoverCache(req.libraryItem.id) - SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem) + SocketAuthority.emitter('item_updated', req.libraryItem.toOldJSONExpanded()) } res.sendStatus(200) @@ -485,7 +485,7 @@ class LibraryItemController { req.libraryItem.media.changed('audioFiles', true) await req.libraryItem.media.save() - SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem) + SocketAuthority.emitter('item_updated', req.libraryItem.toOldJSONExpanded()) res.json(req.libraryItem.toOldJSON()) } @@ -663,7 +663,7 @@ class LibraryItemController { await libraryItem.saveMetadataFile() Logger.debug(`[LibraryItemController] Updated library item media "${libraryItem.media.title}"`) - SocketAuthority.libraryItemEmitter('item_updated', libraryItem) + SocketAuthority.emitter('item_updated', libraryItem.toOldJSONExpanded()) itemsUpdated++ } } @@ -894,7 +894,7 @@ class LibraryItemController { await req.libraryItem.saveMetadataFile() - SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem) + SocketAuthority.emitter('item_updated', req.libraryItem.toOldJSONExpanded()) } res.json({ @@ -1005,7 +1005,7 @@ class LibraryItemController { await req.libraryItem.save() - SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem) + SocketAuthority.emitter('item_updated', req.libraryItem.toOldJSONExpanded()) res.sendStatus(200) } @@ -1153,7 +1153,7 @@ class LibraryItemController { await req.libraryItem.save() - SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem) + SocketAuthority.emitter('item_updated', req.libraryItem.toOldJSONExpanded()) res.sendStatus(200) } diff --git a/server/controllers/MiscController.js b/server/controllers/MiscController.js index 0e9f03774..48eca3f87 100644 --- a/server/controllers/MiscController.js +++ b/server/controllers/MiscController.js @@ -343,7 +343,7 @@ class MiscController { }) await libraryItem.saveMetadataFile() - SocketAuthority.libraryItemEmitter('item_updated', libraryItem) + SocketAuthority.emitter('item_updated', libraryItem.toOldJSONExpanded()) numItemsUpdated++ } } @@ -386,7 +386,7 @@ class MiscController { }) await libraryItem.saveMetadataFile() - SocketAuthority.libraryItemEmitter('item_updated', libraryItem) + SocketAuthority.emitter('item_updated', libraryItem.toOldJSONExpanded()) numItemsUpdated++ } @@ -481,7 +481,7 @@ class MiscController { }) await libraryItem.saveMetadataFile() - SocketAuthority.libraryItemEmitter('item_updated', libraryItem) + SocketAuthority.emitter('item_updated', libraryItem.toOldJSONExpanded()) numItemsUpdated++ } } @@ -524,7 +524,7 @@ class MiscController { }) await libraryItem.saveMetadataFile() - SocketAuthority.libraryItemEmitter('item_updated', libraryItem) + SocketAuthority.emitter('item_updated', libraryItem.toOldJSONExpanded()) numItemsUpdated++ } diff --git a/server/controllers/PodcastController.js b/server/controllers/PodcastController.js index 6395e05ba..c66b4088d 100644 --- a/server/controllers/PodcastController.js +++ b/server/controllers/PodcastController.js @@ -161,7 +161,7 @@ class PodcastController { } } - SocketAuthority.libraryItemEmitter('item_added', newLibraryItem) + SocketAuthority.emitter('item_added', newLibraryItem.toOldJSONExpanded()) res.json(newLibraryItem.toOldJSONExpanded()) @@ -379,7 +379,7 @@ class PodcastController { const overrideDetails = req.query.override === '1' const episodesUpdated = await Scanner.quickMatchPodcastEpisodes(req.libraryItem, { overrideDetails }) if (episodesUpdated) { - SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem) + SocketAuthority.emitter('item_updated', req.libraryItem.toOldJSONExpanded()) } res.json({ @@ -418,7 +418,7 @@ class PodcastController { Logger.info(`[PodcastController] Updated episode "${episode.title}" keys`, episode.changed()) await episode.save() - SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem) + SocketAuthority.emitter('item_updated', req.libraryItem.toOldJSONExpanded()) } else { Logger.info(`[PodcastController] No changes to episode "${episode.title}"`) } @@ -504,7 +504,7 @@ class PodcastController { req.libraryItem.media.numEpisodes = req.libraryItem.media.podcastEpisodes.length await req.libraryItem.media.save() - SocketAuthority.libraryItemEmitter('item_updated', req.libraryItem) + SocketAuthority.emitter('item_updated', req.libraryItem.toOldJSONExpanded()) res.json(req.libraryItem.toOldJSON()) } diff --git a/server/managers/PodcastManager.js b/server/managers/PodcastManager.js index 052ba8b33..9c621ea14 100644 --- a/server/managers/PodcastManager.js +++ b/server/managers/PodcastManager.js @@ -254,7 +254,7 @@ class PodcastManager { await libraryItem.media.save() } - SocketAuthority.libraryItemEmitter('item_updated', libraryItem) + SocketAuthority.emitter('item_updated', libraryItem.toOldJSONExpanded()) const podcastEpisodeExpanded = podcastEpisode.toOldJSONExpanded(libraryItem.id) podcastEpisodeExpanded.libraryItem = libraryItem.toOldJSONExpanded() SocketAuthority.emitter('episode_added', podcastEpisodeExpanded) @@ -367,7 +367,7 @@ class PodcastManager { libraryItem.changed('updatedAt', true) await libraryItem.save() - SocketAuthority.libraryItemEmitter('item_updated', libraryItem) + SocketAuthority.emitter('item_updated', libraryItem.toOldJSONExpanded()) return libraryItem.media.autoDownloadEpisodes } @@ -425,7 +425,7 @@ class PodcastManager { libraryItem.changed('updatedAt', true) await libraryItem.save() - SocketAuthority.libraryItemEmitter('item_updated', libraryItem) + SocketAuthority.emitter('item_updated', libraryItem.toOldJSONExpanded()) return newEpisodes || [] } @@ -712,7 +712,7 @@ class PodcastManager { } } - SocketAuthority.libraryItemEmitter('item_added', newLibraryItem) + SocketAuthority.emitter('item_added', newLibraryItem.toOldJSONExpanded()) // Turn on podcast auto download cron if not already on if (newLibraryItem.media.autoDownloadEpisodes) { diff --git a/server/scanner/LibraryItemScanner.js b/server/scanner/LibraryItemScanner.js index 133a1e301..bd99060c0 100644 --- a/server/scanner/LibraryItemScanner.js +++ b/server/scanner/LibraryItemScanner.js @@ -64,7 +64,7 @@ class LibraryItemScanner { const { libraryItem: expandedLibraryItem, wasUpdated } = await this.rescanLibraryItemMedia(libraryItem, libraryItemScanData, library.settings, scanLogger) if (libraryItemDataUpdated || wasUpdated) { - SocketAuthority.libraryItemEmitter('item_updated', expandedLibraryItem) + SocketAuthority.emitter('item_updated', expandedLibraryItem.toOldJSONExpanded()) await this.checkAuthorsAndSeriesRemovedFromBooks(library.id, scanLogger) diff --git a/server/scanner/LibraryScanner.js b/server/scanner/LibraryScanner.js index 4d0285dd5..1e92efdec 100644 --- a/server/scanner/LibraryScanner.js +++ b/server/scanner/LibraryScanner.js @@ -223,7 +223,11 @@ class LibraryScanner { // Emit item updates in chunks of 10 to client if (libraryItemsUpdated.length === 10) { - SocketAuthority.libraryItemsEmitter('items_updated', libraryItemsUpdated) + // TODO: Should only emit to clients where library item is accessible + SocketAuthority.emitter( + 'items_updated', + libraryItemsUpdated.map((li) => li.toOldJSONExpanded()) + ) libraryItemsUpdated = [] } @@ -231,7 +235,11 @@ class LibraryScanner { } // Emit item updates to client if (libraryItemsUpdated.length) { - SocketAuthority.libraryItemsEmitter('items_updated', libraryItemsUpdated) + // TODO: Should only emit to clients where library item is accessible + SocketAuthority.emitter( + 'items_updated', + libraryItemsUpdated.map((li) => li.toOldJSONExpanded()) + ) } // Authors and series that were removed from books should be removed if they are now empty @@ -269,7 +277,11 @@ class LibraryScanner { // Emit new items in chunks of 10 to client if (newLibraryItems.length === 10) { - SocketAuthority.libraryItemsEmitter('items_added', newLibraryItems) + // TODO: Should only emit to clients where library item is accessible + SocketAuthority.emitter( + 'items_added', + newLibraryItems.map((li) => li.toOldJSONExpanded()) + ) newLibraryItems = [] } @@ -277,7 +289,11 @@ class LibraryScanner { } // Emit new items to client if (newLibraryItems.length) { - SocketAuthority.libraryItemsEmitter('items_added', newLibraryItems) + // TODO: Should only emit to clients where library item is accessible + SocketAuthority.emitter( + 'items_added', + newLibraryItems.map((li) => li.toOldJSONExpanded()) + ) } } @@ -593,7 +609,7 @@ class LibraryScanner { Logger.info(`[LibraryScanner] Scanning file update group and library item was deleted "${existingLibraryItem.media.title}" - marking as missing`) existingLibraryItem.isMissing = true await existingLibraryItem.save() - SocketAuthority.libraryItemEmitter('item_updated', existingLibraryItem) + SocketAuthority.emitter('item_updated', existingLibraryItem.toOldJSONExpanded()) itemGroupingResults[itemDir] = ScanResult.REMOVED continue @@ -627,7 +643,7 @@ class LibraryScanner { const isSingleMediaItem = isSingleMediaFile(fileUpdateGroup, itemDir) const newLibraryItem = await LibraryItemScanner.scanPotentialNewLibraryItem(fullPath, library, folder, isSingleMediaItem) if (newLibraryItem) { - SocketAuthority.libraryItemEmitter('item_added', newLibraryItem) + SocketAuthority.emitter('item_added', newLibraryItem.toOldJSONExpanded()) } itemGroupingResults[itemDir] = newLibraryItem ? ScanResult.ADDED : ScanResult.NOTHING } diff --git a/server/scanner/Scanner.js b/server/scanner/Scanner.js index 1e2751ed8..c5c625320 100644 --- a/server/scanner/Scanner.js +++ b/server/scanner/Scanner.js @@ -126,7 +126,7 @@ class Scanner { await libraryItem.saveMetadataFile() - SocketAuthority.libraryItemEmitter('item_updated', libraryItem) + SocketAuthority.emitter('item_updated', libraryItem.toOldJSONExpanded()) } return {