mirror of
				https://github.com/advplyr/audiobookshelf.git
				synced 2025-10-27 11:18:14 +01:00 
			
		
		
		
	Remove item from playlist when removing item, update PlaylistController socket events to emit to playlist userId
This commit is contained in:
		
							parent
							
								
									2d6c997b38
								
							
						
					
					
						commit
						e307ded192
					
				| @ -16,7 +16,7 @@ class PlaylistController { | |||||||
|     } |     } | ||||||
|     const jsonExpanded = newPlaylist.toJSONExpanded(this.db.libraryItems) |     const jsonExpanded = newPlaylist.toJSONExpanded(this.db.libraryItems) | ||||||
|     await this.db.insertEntity('playlist', newPlaylist) |     await this.db.insertEntity('playlist', newPlaylist) | ||||||
|     SocketAuthority.emitter('playlist_added', jsonExpanded) |     SocketAuthority.clientEmitter(newPlaylist.userId, 'playlist_added', jsonExpanded) | ||||||
|     res.json(jsonExpanded) |     res.json(jsonExpanded) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
| @ -39,7 +39,7 @@ class PlaylistController { | |||||||
|     const jsonExpanded = playlist.toJSONExpanded(this.db.libraryItems) |     const jsonExpanded = playlist.toJSONExpanded(this.db.libraryItems) | ||||||
|     if (wasUpdated) { |     if (wasUpdated) { | ||||||
|       await this.db.updateEntity('playlist', playlist) |       await this.db.updateEntity('playlist', playlist) | ||||||
|       SocketAuthority.emitter('playlist_updated', jsonExpanded) |       SocketAuthority.clientEmitter(playlist.userId, 'playlist_updated', jsonExpanded) | ||||||
|     } |     } | ||||||
|     res.json(jsonExpanded) |     res.json(jsonExpanded) | ||||||
|   } |   } | ||||||
| @ -49,7 +49,7 @@ class PlaylistController { | |||||||
|     const playlist = req.playlist |     const playlist = req.playlist | ||||||
|     const jsonExpanded = playlist.toJSONExpanded(this.db.libraryItems) |     const jsonExpanded = playlist.toJSONExpanded(this.db.libraryItems) | ||||||
|     await this.db.removeEntity('playlist', playlist.id) |     await this.db.removeEntity('playlist', playlist.id) | ||||||
|     SocketAuthority.emitter('playlist_removed', jsonExpanded) |     SocketAuthority.clientEmitter(playlist.userId, 'playlist_removed', jsonExpanded) | ||||||
|     res.sendStatus(200) |     res.sendStatus(200) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
| @ -82,7 +82,7 @@ class PlaylistController { | |||||||
|     playlist.addItem(itemToAdd.libraryItemId, itemToAdd.episodeId) |     playlist.addItem(itemToAdd.libraryItemId, itemToAdd.episodeId) | ||||||
|     const jsonExpanded = playlist.toJSONExpanded(this.db.libraryItems) |     const jsonExpanded = playlist.toJSONExpanded(this.db.libraryItems) | ||||||
|     await this.db.updateEntity('playlist', playlist) |     await this.db.updateEntity('playlist', playlist) | ||||||
|     SocketAuthority.emitter('playlist_updated', jsonExpanded) |     SocketAuthority.clientEmitter(playlist.userId, 'playlist_updated', jsonExpanded) | ||||||
|     res.json(jsonExpanded) |     res.json(jsonExpanded) | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
| @ -105,10 +105,10 @@ class PlaylistController { | |||||||
|     if (!playlist.items.length) { |     if (!playlist.items.length) { | ||||||
|       Logger.info(`[PlaylistController] Playlist "${playlist.name}" has no more items - removing it`) |       Logger.info(`[PlaylistController] Playlist "${playlist.name}" has no more items - removing it`) | ||||||
|       await this.db.removeEntity('playlist', playlist.id) |       await this.db.removeEntity('playlist', playlist.id) | ||||||
|       SocketAuthority.emitter('playlist_removed', jsonExpanded) |       SocketAuthority.clientEmitter(playlist.userId, 'playlist_removed', jsonExpanded) | ||||||
|     } else { |     } else { | ||||||
|       await this.db.updateEntity('playlist', playlist) |       await this.db.updateEntity('playlist', playlist) | ||||||
|       SocketAuthority.emitter('playlist_updated', jsonExpanded) |       SocketAuthority.clientEmitter(playlist.userId, 'playlist_updated', jsonExpanded) | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     res.json(jsonExpanded) |     res.json(jsonExpanded) | ||||||
| @ -136,7 +136,7 @@ class PlaylistController { | |||||||
|     const jsonExpanded = playlist.toJSONExpanded(this.db.libraryItems) |     const jsonExpanded = playlist.toJSONExpanded(this.db.libraryItems) | ||||||
|     if (hasUpdated) { |     if (hasUpdated) { | ||||||
|       await this.db.updateEntity('playlist', playlist) |       await this.db.updateEntity('playlist', playlist) | ||||||
|       SocketAuthority.emitter('playlist_updated', jsonExpanded) |       SocketAuthority.clientEmitter(playlist.userId, 'playlist_updated', jsonExpanded) | ||||||
|     } |     } | ||||||
|     res.json(jsonExpanded) |     res.json(jsonExpanded) | ||||||
|   } |   } | ||||||
| @ -165,10 +165,10 @@ class PlaylistController { | |||||||
|       if (!playlist.items.length) { |       if (!playlist.items.length) { | ||||||
|         Logger.info(`[PlaylistController] Playlist "${playlist.name}" has no more items - removing it`) |         Logger.info(`[PlaylistController] Playlist "${playlist.name}" has no more items - removing it`) | ||||||
|         await this.db.removeEntity('playlist', playlist.id) |         await this.db.removeEntity('playlist', playlist.id) | ||||||
|         SocketAuthority.emitter('playlist_removed', jsonExpanded) |         SocketAuthority.clientEmitter(playlist.userId, 'playlist_removed', jsonExpanded) | ||||||
|       } else { |       } else { | ||||||
|         await this.db.updateEntity('playlist', playlist) |         await this.db.updateEntity('playlist', playlist) | ||||||
|         SocketAuthority.emitter('playlist_updated', jsonExpanded) |         SocketAuthority.clientEmitter(playlist.userId, 'playlist_updated', jsonExpanded) | ||||||
|       } |       } | ||||||
|     } |     } | ||||||
|     res.json(jsonExpanded) |     res.json(jsonExpanded) | ||||||
|  | |||||||
| @ -137,5 +137,13 @@ class Playlist { | |||||||
|     if (item.episodeId) return this.items.some(i => i.libraryItemId === item.libraryItemId && i.episodeId === item.episodeId) |     if (item.episodeId) return this.items.some(i => i.libraryItemId === item.libraryItemId && i.episodeId === item.episodeId) | ||||||
|     return this.items.some(i => i.libraryItemId === item.libraryItemId) |     return this.items.some(i => i.libraryItemId === item.libraryItemId) | ||||||
|   } |   } | ||||||
|  | 
 | ||||||
|  |   hasItemsForLibraryItem(libraryItemId) { | ||||||
|  |     return this.items.some(i => i.libraryItemId === libraryItemId) | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   removeItemsForLibraryItem(libraryItemId) { | ||||||
|  |     this.items = this.items.filter(i => i.libraryItemId !== libraryItemId) | ||||||
|  |   } | ||||||
| } | } | ||||||
| module.exports = Playlist | module.exports = Playlist | ||||||
| @ -354,7 +354,24 @@ class ApiRouter { | |||||||
|       const collection = collectionsWithBook[i] |       const collection = collectionsWithBook[i] | ||||||
|       collection.removeBook(libraryItem.id) |       collection.removeBook(libraryItem.id) | ||||||
|       await this.db.updateEntity('collection', collection) |       await this.db.updateEntity('collection', collection) | ||||||
|       SocketAuthority.clientEmitter(collection.userId, 'collection_updated', collection.toJSONExpanded(this.db.libraryItems)) |       SocketAuthority.emitter('collection_updated', collection.toJSONExpanded(this.db.libraryItems)) | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // remove item from playlists
 | ||||||
|  |     const playlistsWithItem = this.db.playlists.filter(p => p.hasItemsForLibraryItem(libraryItem.id)) | ||||||
|  |     for (let i = 0; i < playlistsWithItem.length; i++) { | ||||||
|  |       const playlist = playlistsWithItem[i] | ||||||
|  |       playlist.removeItemsForLibraryItem(libraryItem.id) | ||||||
|  | 
 | ||||||
|  |       // If playlist is now empty then remove it
 | ||||||
|  |       if (!playlist.items.length) { | ||||||
|  |         Logger.info(`[ApiRouter] Playlist "${playlist.name}" has no more items - removing it`) | ||||||
|  |         await this.db.removeEntity('playlist', playlist.id) | ||||||
|  |         SocketAuthority.clientEmitter(playlist.userId, 'playlist_removed', playlist.toJSONExpanded(this.db.libraryItems)) | ||||||
|  |       } else { | ||||||
|  |         await this.db.updateEntity('playlist', playlist) | ||||||
|  |         SocketAuthority.clientEmitter(playlist.userId, 'playlist_updated', playlist.toJSONExpanded(this.db.libraryItems)) | ||||||
|  |       } | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|     // purge cover cache
 |     // purge cover cache
 | ||||||
|  | |||||||
		Loading…
	
		Reference in New Issue
	
	Block a user