Remove item from playlist when removing item, update PlaylistController socket events to emit to playlist userId

This commit is contained in:
advplyr 2022-11-27 14:49:21 -06:00
parent 2d6c997b38
commit e307ded192
3 changed files with 35 additions and 10 deletions

View File

@ -16,7 +16,7 @@ class PlaylistController {
}
const jsonExpanded = newPlaylist.toJSONExpanded(this.db.libraryItems)
await this.db.insertEntity('playlist', newPlaylist)
SocketAuthority.emitter('playlist_added', jsonExpanded)
SocketAuthority.clientEmitter(newPlaylist.userId, 'playlist_added', jsonExpanded)
res.json(jsonExpanded)
}
@ -39,7 +39,7 @@ class PlaylistController {
const jsonExpanded = playlist.toJSONExpanded(this.db.libraryItems)
if (wasUpdated) {
await this.db.updateEntity('playlist', playlist)
SocketAuthority.emitter('playlist_updated', jsonExpanded)
SocketAuthority.clientEmitter(playlist.userId, 'playlist_updated', jsonExpanded)
}
res.json(jsonExpanded)
}
@ -49,7 +49,7 @@ class PlaylistController {
const playlist = req.playlist
const jsonExpanded = playlist.toJSONExpanded(this.db.libraryItems)
await this.db.removeEntity('playlist', playlist.id)
SocketAuthority.emitter('playlist_removed', jsonExpanded)
SocketAuthority.clientEmitter(playlist.userId, 'playlist_removed', jsonExpanded)
res.sendStatus(200)
}
@ -82,7 +82,7 @@ class PlaylistController {
playlist.addItem(itemToAdd.libraryItemId, itemToAdd.episodeId)
const jsonExpanded = playlist.toJSONExpanded(this.db.libraryItems)
await this.db.updateEntity('playlist', playlist)
SocketAuthority.emitter('playlist_updated', jsonExpanded)
SocketAuthority.clientEmitter(playlist.userId, 'playlist_updated', jsonExpanded)
res.json(jsonExpanded)
}
@ -105,10 +105,10 @@ class PlaylistController {
if (!playlist.items.length) {
Logger.info(`[PlaylistController] Playlist "${playlist.name}" has no more items - removing it`)
await this.db.removeEntity('playlist', playlist.id)
SocketAuthority.emitter('playlist_removed', jsonExpanded)
SocketAuthority.clientEmitter(playlist.userId, 'playlist_removed', jsonExpanded)
} else {
await this.db.updateEntity('playlist', playlist)
SocketAuthority.emitter('playlist_updated', jsonExpanded)
SocketAuthority.clientEmitter(playlist.userId, 'playlist_updated', jsonExpanded)
}
res.json(jsonExpanded)
@ -136,7 +136,7 @@ class PlaylistController {
const jsonExpanded = playlist.toJSONExpanded(this.db.libraryItems)
if (hasUpdated) {
await this.db.updateEntity('playlist', playlist)
SocketAuthority.emitter('playlist_updated', jsonExpanded)
SocketAuthority.clientEmitter(playlist.userId, 'playlist_updated', jsonExpanded)
}
res.json(jsonExpanded)
}
@ -165,10 +165,10 @@ class PlaylistController {
if (!playlist.items.length) {
Logger.info(`[PlaylistController] Playlist "${playlist.name}" has no more items - removing it`)
await this.db.removeEntity('playlist', playlist.id)
SocketAuthority.emitter('playlist_removed', jsonExpanded)
SocketAuthority.clientEmitter(playlist.userId, 'playlist_removed', jsonExpanded)
} else {
await this.db.updateEntity('playlist', playlist)
SocketAuthority.emitter('playlist_updated', jsonExpanded)
SocketAuthority.clientEmitter(playlist.userId, 'playlist_updated', jsonExpanded)
}
}
res.json(jsonExpanded)

View File

@ -137,5 +137,13 @@ class Playlist {
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)
}
hasItemsForLibraryItem(libraryItemId) {
return this.items.some(i => i.libraryItemId === libraryItemId)
}
removeItemsForLibraryItem(libraryItemId) {
this.items = this.items.filter(i => i.libraryItemId !== libraryItemId)
}
}
module.exports = Playlist

View File

@ -354,7 +354,24 @@ class ApiRouter {
const collection = collectionsWithBook[i]
collection.removeBook(libraryItem.id)
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