2024-08-11 22:15:34 +02:00
|
|
|
const { Request, Response, NextFunction } = require('express')
|
2022-11-26 22:14:45 +01:00
|
|
|
const Logger = require('../Logger')
|
|
|
|
const SocketAuthority = require('../SocketAuthority')
|
2023-07-05 01:14:44 +02:00
|
|
|
const Database = require('../Database')
|
2022-11-26 22:14:45 +01:00
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
2024-08-12 00:01:25 +02:00
|
|
|
* @typedef RequestUserObject
|
2024-08-11 23:07:29 +02:00
|
|
|
* @property {import('../models/User')} user
|
2024-08-11 22:15:34 +02:00
|
|
|
*
|
2024-08-12 00:01:25 +02:00
|
|
|
* @typedef {Request & RequestUserObject} RequestWithUser
|
2025-01-01 00:01:42 +01:00
|
|
|
*
|
|
|
|
* @typedef RequestEntityObject
|
|
|
|
* @property {import('../models/Playlist')} playlist
|
|
|
|
*
|
|
|
|
* @typedef {RequestWithUser & RequestEntityObject} PlaylistControllerRequest
|
2024-08-11 22:15:34 +02:00
|
|
|
*/
|
|
|
|
|
2022-11-26 22:14:45 +01:00
|
|
|
class PlaylistController {
|
2024-08-11 00:15:21 +02:00
|
|
|
constructor() {}
|
2022-11-26 22:14:45 +01:00
|
|
|
|
2023-08-13 18:22:38 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/playlists
|
|
|
|
* Create playlist
|
2024-08-11 22:15:34 +02:00
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2023-08-13 18:22:38 +02:00
|
|
|
*/
|
2022-11-26 22:14:45 +01:00
|
|
|
async create(req, res) {
|
2025-01-01 00:01:42 +01:00
|
|
|
const reqBody = req.body || {}
|
2023-08-13 18:22:38 +02:00
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
// Validation
|
|
|
|
if (!reqBody.name || !reqBody.libraryId) {
|
|
|
|
return res.status(400).send('Invalid playlist data')
|
|
|
|
}
|
|
|
|
if (reqBody.description && typeof reqBody.description !== 'string') {
|
|
|
|
return res.status(400).send('Invalid playlist description')
|
|
|
|
}
|
|
|
|
const items = reqBody.items || []
|
|
|
|
const isPodcast = items.some((i) => i.episodeId)
|
|
|
|
const libraryItemIds = new Set()
|
|
|
|
for (const item of items) {
|
|
|
|
if (!item.libraryItemId || typeof item.libraryItemId !== 'string') {
|
|
|
|
return res.status(400).send('Invalid playlist item')
|
|
|
|
}
|
|
|
|
if (isPodcast && (!item.episodeId || typeof item.episodeId !== 'string')) {
|
|
|
|
return res.status(400).send('Invalid playlist item episodeId')
|
|
|
|
} else if (!isPodcast && item.episodeId) {
|
|
|
|
return res.status(400).send('Invalid playlist item episodeId')
|
|
|
|
}
|
|
|
|
libraryItemIds.add(item.libraryItemId)
|
|
|
|
}
|
2023-08-13 18:22:38 +02:00
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
// Load library items
|
|
|
|
const libraryItems = await Database.libraryItemModel.findAll({
|
|
|
|
attributes: ['id', 'mediaId', 'mediaType', 'libraryId'],
|
2023-08-13 18:22:38 +02:00
|
|
|
where: {
|
2025-01-01 00:01:42 +01:00
|
|
|
id: Array.from(libraryItemIds),
|
|
|
|
libraryId: reqBody.libraryId,
|
|
|
|
mediaType: isPodcast ? 'podcast' : 'book'
|
2023-08-13 18:22:38 +02:00
|
|
|
}
|
|
|
|
})
|
2025-01-01 00:01:42 +01:00
|
|
|
if (libraryItems.length !== libraryItemIds.size) {
|
|
|
|
return res.status(400).send('Invalid playlist data. Invalid items')
|
|
|
|
}
|
2023-08-13 18:22:38 +02:00
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
// Validate podcast episodes
|
|
|
|
if (isPodcast) {
|
|
|
|
const podcastEpisodeIds = items.map((i) => i.episodeId)
|
|
|
|
const podcastEpisodes = await Database.podcastEpisodeModel.findAll({
|
|
|
|
attributes: ['id'],
|
|
|
|
where: {
|
|
|
|
id: podcastEpisodeIds
|
|
|
|
}
|
2023-08-13 18:22:38 +02:00
|
|
|
})
|
2025-01-01 00:01:42 +01:00
|
|
|
if (podcastEpisodes.length !== podcastEpisodeIds.length) {
|
|
|
|
return res.status(400).send('Invalid playlist data. Invalid podcast episodes')
|
|
|
|
}
|
2023-08-13 18:22:38 +02:00
|
|
|
}
|
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
const transaction = await Database.sequelize.transaction()
|
|
|
|
try {
|
|
|
|
// Create playlist
|
|
|
|
const newPlaylist = await Database.playlistModel.create(
|
|
|
|
{
|
|
|
|
libraryId: reqBody.libraryId,
|
|
|
|
userId: req.user.id,
|
|
|
|
name: reqBody.name,
|
|
|
|
description: reqBody.description || null
|
|
|
|
},
|
|
|
|
{ transaction }
|
|
|
|
)
|
|
|
|
|
|
|
|
// Create playlistMediaItems
|
|
|
|
const playlistItemPayloads = []
|
|
|
|
for (const [index, item] of items.entries()) {
|
|
|
|
const libraryItem = libraryItems.find((li) => li.id === item.libraryItemId)
|
|
|
|
playlistItemPayloads.push({
|
|
|
|
playlistId: newPlaylist.id,
|
|
|
|
mediaItemId: item.episodeId || libraryItem.mediaId,
|
|
|
|
mediaItemType: item.episodeId ? 'podcastEpisode' : 'book',
|
|
|
|
order: index + 1
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
await Database.playlistMediaItemModel.bulkCreate(playlistItemPayloads, { transaction })
|
|
|
|
|
|
|
|
await transaction.commit()
|
|
|
|
|
|
|
|
newPlaylist.playlistMediaItems = await newPlaylist.getMediaItemsExpandedWithLibraryItem()
|
|
|
|
|
|
|
|
const jsonExpanded = newPlaylist.toOldJSONExpanded()
|
|
|
|
SocketAuthority.clientEmitter(newPlaylist.userId, 'playlist_added', jsonExpanded)
|
|
|
|
res.json(jsonExpanded)
|
|
|
|
} catch (error) {
|
|
|
|
await transaction.rollback()
|
|
|
|
Logger.error('[PlaylistController] create:', error)
|
|
|
|
res.status(500).send('Failed to create playlist')
|
|
|
|
}
|
2022-11-26 22:14:45 +01:00
|
|
|
}
|
|
|
|
|
2023-08-13 18:22:38 +02:00
|
|
|
/**
|
2025-01-01 00:01:42 +01:00
|
|
|
* @deprecated - Use /api/libraries/:libraryId/playlists
|
|
|
|
* This is not used by Abs web client or mobile apps
|
2025-01-01 00:11:31 +01:00
|
|
|
* TODO: Remove this endpoint or make it the primary
|
2025-01-01 00:01:42 +01:00
|
|
|
*
|
2023-08-13 18:22:38 +02:00
|
|
|
* GET: /api/playlists
|
|
|
|
* Get all playlists for user
|
2024-08-11 22:15:34 +02:00
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2023-08-13 18:22:38 +02:00
|
|
|
*/
|
2023-07-23 16:42:57 +02:00
|
|
|
async findAllForUser(req, res) {
|
2025-01-01 00:11:31 +01:00
|
|
|
const playlistsForUser = await Database.playlistModel.getOldPlaylistsForUserAndLibrary(req.user.id)
|
2022-11-26 22:14:45 +01:00
|
|
|
res.json({
|
2025-01-01 00:01:42 +01:00
|
|
|
playlists: playlistsForUser
|
2022-11-26 22:14:45 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-13 18:22:38 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/playlists/:id
|
2024-08-11 22:15:34 +02:00
|
|
|
*
|
2025-01-01 00:01:42 +01:00
|
|
|
* @param {PlaylistControllerRequest} req
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {Response} res
|
2023-08-13 18:22:38 +02:00
|
|
|
*/
|
|
|
|
async findOne(req, res) {
|
2025-01-01 00:01:42 +01:00
|
|
|
req.playlist.playlistMediaItems = await req.playlist.getMediaItemsExpandedWithLibraryItem()
|
|
|
|
res.json(req.playlist.toOldJSONExpanded())
|
2022-11-26 22:14:45 +01:00
|
|
|
}
|
|
|
|
|
2023-08-13 18:22:38 +02:00
|
|
|
/**
|
|
|
|
* PATCH: /api/playlists/:id
|
|
|
|
* Update playlist
|
2024-08-11 22:15:34 +02:00
|
|
|
*
|
2025-01-01 00:01:42 +01:00
|
|
|
* Used for updating name and description or reordering items
|
|
|
|
*
|
|
|
|
* @param {PlaylistControllerRequest} req
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {Response} res
|
2023-08-13 18:22:38 +02:00
|
|
|
*/
|
2022-11-26 22:14:45 +01:00
|
|
|
async update(req, res) {
|
2025-01-01 00:01:42 +01:00
|
|
|
// Validation
|
|
|
|
const reqBody = req.body || {}
|
|
|
|
if (reqBody.libraryId || reqBody.userId) {
|
|
|
|
// Could allow support for this if needed with additional validation
|
|
|
|
return res.status(400).send('Invalid playlist data. Cannot update libraryId or userId')
|
|
|
|
}
|
|
|
|
if (reqBody.name && typeof reqBody.name !== 'string') {
|
|
|
|
return res.status(400).send('Invalid playlist name')
|
|
|
|
}
|
|
|
|
if (reqBody.description && typeof reqBody.description !== 'string') {
|
|
|
|
return res.status(400).send('Invalid playlist description')
|
|
|
|
}
|
|
|
|
if (reqBody.items && (!Array.isArray(reqBody.items) || reqBody.items.some((i) => !i.libraryItemId || typeof i.libraryItemId !== 'string' || (i.episodeId && typeof i.episodeId !== 'string')))) {
|
|
|
|
return res.status(400).send('Invalid playlist items')
|
|
|
|
}
|
|
|
|
|
|
|
|
const playlistUpdatePayload = {}
|
|
|
|
if (reqBody.name) playlistUpdatePayload.name = reqBody.name
|
|
|
|
if (reqBody.description) playlistUpdatePayload.description = reqBody.description
|
|
|
|
|
|
|
|
// Update name and description
|
2023-08-13 18:22:38 +02:00
|
|
|
let wasUpdated = false
|
2025-01-01 00:01:42 +01:00
|
|
|
if (Object.keys(playlistUpdatePayload).length) {
|
|
|
|
req.playlist.set(playlistUpdatePayload)
|
|
|
|
const changed = req.playlist.changed()
|
|
|
|
if (changed?.length) {
|
|
|
|
await req.playlist.save()
|
|
|
|
Logger.debug(`[PlaylistController] Updated playlist ${req.playlist.id} keys [${changed.join(',')}]`)
|
|
|
|
wasUpdated = true
|
|
|
|
}
|
2023-08-13 18:22:38 +02:00
|
|
|
}
|
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
// If array of items is set then update order of playlist media items
|
|
|
|
if (reqBody.items?.length) {
|
|
|
|
const libraryItemIds = Array.from(new Set(reqBody.items.map((i) => i.libraryItemId)))
|
2023-08-20 20:34:03 +02:00
|
|
|
const libraryItems = await Database.libraryItemModel.findAll({
|
2025-01-01 00:01:42 +01:00
|
|
|
attributes: ['id', 'mediaId', 'mediaType'],
|
2023-08-13 18:22:38 +02:00
|
|
|
where: {
|
|
|
|
id: libraryItemIds
|
|
|
|
}
|
|
|
|
})
|
2025-01-01 00:01:42 +01:00
|
|
|
if (libraryItems.length !== libraryItemIds.length) {
|
|
|
|
return res.status(400).send('Invalid playlist items. Items not found')
|
|
|
|
}
|
|
|
|
/** @type {import('../models/PlaylistMediaItem')[]} */
|
|
|
|
const existingPlaylistMediaItems = await req.playlist.getPlaylistMediaItems({
|
2023-08-13 18:22:38 +02:00
|
|
|
order: [['order', 'ASC']]
|
|
|
|
})
|
2025-01-01 00:01:42 +01:00
|
|
|
if (existingPlaylistMediaItems.length !== reqBody.items.length) {
|
|
|
|
return res.status(400).send('Invalid playlist items. Length mismatch')
|
|
|
|
}
|
2023-08-13 18:22:38 +02:00
|
|
|
|
|
|
|
// Set an array of mediaItemId
|
|
|
|
const newMediaItemIdOrder = []
|
2025-01-01 00:01:42 +01:00
|
|
|
for (const item of reqBody.items) {
|
2024-08-11 00:15:21 +02:00
|
|
|
const libraryItem = libraryItems.find((li) => li.id === item.libraryItemId)
|
2023-08-13 18:22:38 +02:00
|
|
|
const mediaItemId = item.episodeId || libraryItem.mediaId
|
|
|
|
newMediaItemIdOrder.push(mediaItemId)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sort existing playlist media items into new order
|
|
|
|
existingPlaylistMediaItems.sort((a, b) => {
|
2024-08-11 00:15:21 +02:00
|
|
|
const aIndex = newMediaItemIdOrder.findIndex((i) => i === a.mediaItemId)
|
|
|
|
const bIndex = newMediaItemIdOrder.findIndex((i) => i === b.mediaItemId)
|
2023-08-13 18:22:38 +02:00
|
|
|
return aIndex - bIndex
|
|
|
|
})
|
|
|
|
|
|
|
|
// Update order on playlistMediaItem records
|
2025-01-01 00:01:42 +01:00
|
|
|
for (const [index, playlistMediaItem] of existingPlaylistMediaItems.entries()) {
|
|
|
|
if (playlistMediaItem.order !== index + 1) {
|
2023-08-13 18:22:38 +02:00
|
|
|
await playlistMediaItem.update({
|
2025-01-01 00:01:42 +01:00
|
|
|
order: index + 1
|
2023-08-13 18:22:38 +02:00
|
|
|
})
|
|
|
|
wasUpdated = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
req.playlist.playlistMediaItems = await req.playlist.getMediaItemsExpandedWithLibraryItem()
|
|
|
|
|
|
|
|
const jsonExpanded = req.playlist.toOldJSONExpanded()
|
2022-11-26 22:14:45 +01:00
|
|
|
if (wasUpdated) {
|
2025-01-01 00:01:42 +01:00
|
|
|
SocketAuthority.clientEmitter(req.playlist.userId, 'playlist_updated', jsonExpanded)
|
2022-11-26 22:14:45 +01:00
|
|
|
}
|
|
|
|
res.json(jsonExpanded)
|
|
|
|
}
|
|
|
|
|
2023-08-13 18:22:38 +02:00
|
|
|
/**
|
|
|
|
* DELETE: /api/playlists/:id
|
|
|
|
* Remove playlist
|
2024-08-11 22:15:34 +02:00
|
|
|
*
|
2025-01-01 00:01:42 +01:00
|
|
|
* @param {PlaylistControllerRequest} req
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {Response} res
|
2023-08-13 18:22:38 +02:00
|
|
|
*/
|
2022-11-26 22:14:45 +01:00
|
|
|
async delete(req, res) {
|
2025-01-01 00:01:42 +01:00
|
|
|
req.playlist.playlistMediaItems = await req.playlist.getMediaItemsExpandedWithLibraryItem()
|
|
|
|
const jsonExpanded = req.playlist.toOldJSONExpanded()
|
|
|
|
|
2023-08-13 18:22:38 +02:00
|
|
|
await req.playlist.destroy()
|
|
|
|
SocketAuthority.clientEmitter(jsonExpanded.userId, 'playlist_removed', jsonExpanded)
|
2022-11-26 22:14:45 +01:00
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2023-08-13 18:22:38 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/playlists/:id/item
|
|
|
|
* Add item to playlist
|
2024-08-11 22:15:34 +02:00
|
|
|
*
|
2025-01-01 00:01:42 +01:00
|
|
|
* This is not used by Abs web client or mobile apps. Only the batch endpoints are used.
|
|
|
|
*
|
|
|
|
* @param {PlaylistControllerRequest} req
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {Response} res
|
2023-08-13 18:22:38 +02:00
|
|
|
*/
|
2022-11-26 22:14:45 +01:00
|
|
|
async addItem(req, res) {
|
2025-01-01 00:01:42 +01:00
|
|
|
const itemToAdd = req.body || {}
|
2022-11-26 22:14:45 +01:00
|
|
|
|
|
|
|
if (!itemToAdd.libraryItemId) {
|
|
|
|
return res.status(400).send('Request body has no libraryItemId')
|
|
|
|
}
|
|
|
|
|
2023-08-20 20:34:03 +02:00
|
|
|
const libraryItem = await Database.libraryItemModel.getOldById(itemToAdd.libraryItemId)
|
2022-11-26 22:14:45 +01:00
|
|
|
if (!libraryItem) {
|
|
|
|
return res.status(400).send('Library item not found')
|
|
|
|
}
|
2025-01-01 00:01:42 +01:00
|
|
|
if (libraryItem.libraryId !== req.playlist.libraryId) {
|
2022-11-26 22:14:45 +01:00
|
|
|
return res.status(400).send('Library item in different library')
|
|
|
|
}
|
|
|
|
if ((itemToAdd.episodeId && !libraryItem.isPodcast) || (libraryItem.isPodcast && !itemToAdd.episodeId)) {
|
|
|
|
return res.status(400).send('Invalid item to add for this library type')
|
|
|
|
}
|
|
|
|
if (itemToAdd.episodeId && !libraryItem.media.checkHasEpisode(itemToAdd.episodeId)) {
|
|
|
|
return res.status(400).send('Episode not found in library item')
|
|
|
|
}
|
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
req.playlist.playlistMediaItems = await req.playlist.getMediaItemsExpandedWithLibraryItem()
|
|
|
|
|
|
|
|
if (req.playlist.checkHasMediaItem(itemToAdd.libraryItemId, itemToAdd.episodeId)) {
|
|
|
|
return res.status(400).send('Item already in playlist')
|
|
|
|
}
|
|
|
|
|
|
|
|
const jsonExpanded = req.playlist.toOldJSONExpanded()
|
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
const playlistMediaItem = {
|
2025-01-01 00:01:42 +01:00
|
|
|
playlistId: req.playlist.id,
|
2023-07-05 01:14:44 +02:00
|
|
|
mediaItemId: itemToAdd.episodeId || libraryItem.media.id,
|
|
|
|
mediaItemType: itemToAdd.episodeId ? 'podcastEpisode' : 'book',
|
2025-01-01 00:01:42 +01:00
|
|
|
order: req.playlist.playlistMediaItems.length + 1
|
|
|
|
}
|
|
|
|
await Database.playlistMediaItemModel.create(playlistMediaItem)
|
|
|
|
|
|
|
|
// Add the new item to to the old json expanded to prevent having to fully reload the playlist media items
|
|
|
|
if (itemToAdd.episodeId) {
|
|
|
|
const episode = libraryItem.media.episodes.find((ep) => ep.id === itemToAdd.episodeId)
|
|
|
|
jsonExpanded.items.push({
|
|
|
|
episodeId: itemToAdd.episodeId,
|
|
|
|
episode: episode.toJSONExpanded(),
|
|
|
|
libraryItemId: libraryItem.id,
|
|
|
|
libraryItem: libraryItem.toJSONMinified()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
jsonExpanded.items.push({
|
|
|
|
libraryItemId: libraryItem.id,
|
|
|
|
libraryItem: libraryItem.toJSONExpanded()
|
|
|
|
})
|
2023-07-05 01:14:44 +02:00
|
|
|
}
|
|
|
|
|
2023-09-17 19:40:13 +02:00
|
|
|
SocketAuthority.clientEmitter(jsonExpanded.userId, 'playlist_updated', jsonExpanded)
|
2022-11-26 22:14:45 +01:00
|
|
|
res.json(jsonExpanded)
|
|
|
|
}
|
|
|
|
|
2023-08-13 18:22:38 +02:00
|
|
|
/**
|
|
|
|
* DELETE: /api/playlists/:id/item/:libraryItemId/:episodeId?
|
|
|
|
* Remove item from playlist
|
2024-08-11 22:15:34 +02:00
|
|
|
*
|
2025-01-01 00:01:42 +01:00
|
|
|
* @param {PlaylistControllerRequest} req
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {Response} res
|
2023-08-13 18:22:38 +02:00
|
|
|
*/
|
2022-11-26 22:14:45 +01:00
|
|
|
async removeItem(req, res) {
|
2025-01-01 00:01:42 +01:00
|
|
|
req.playlist.playlistMediaItems = await req.playlist.getMediaItemsExpandedWithLibraryItem()
|
2023-08-13 18:22:38 +02:00
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
let playlistMediaItem = null
|
|
|
|
if (req.params.episodeId) {
|
|
|
|
playlistMediaItem = req.playlist.playlistMediaItems.find((pmi) => pmi.mediaItemId === req.params.episodeId)
|
|
|
|
} else {
|
|
|
|
playlistMediaItem = req.playlist.playlistMediaItems.find((pmi) => pmi.mediaItem.libraryItem?.id === req.params.libraryItemId)
|
|
|
|
}
|
|
|
|
if (!playlistMediaItem) {
|
2023-08-13 18:22:38 +02:00
|
|
|
return res.status(404).send('Media item not found in playlist')
|
2022-11-26 22:14:45 +01:00
|
|
|
}
|
|
|
|
|
2023-08-13 18:22:38 +02:00
|
|
|
// Remove record
|
2025-01-01 00:01:42 +01:00
|
|
|
await playlistMediaItem.destroy()
|
|
|
|
req.playlist.playlistMediaItems = req.playlist.playlistMediaItems.filter((pmi) => pmi.id !== playlistMediaItem.id)
|
2022-11-26 22:14:45 +01:00
|
|
|
|
2023-08-13 18:22:38 +02:00
|
|
|
// Update playlist media items order
|
2025-01-01 00:01:42 +01:00
|
|
|
for (const [index, mediaItem] of req.playlist.playlistMediaItems.entries()) {
|
|
|
|
if (mediaItem.order !== index + 1) {
|
2023-08-13 18:22:38 +02:00
|
|
|
await mediaItem.update({
|
2025-01-01 00:01:42 +01:00
|
|
|
order: index + 1
|
2023-08-13 18:22:38 +02:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
const jsonExpanded = req.playlist.toOldJSONExpanded()
|
2022-11-27 19:04:49 +01:00
|
|
|
|
|
|
|
// Playlist is removed when there are no items
|
2023-08-13 18:22:38 +02:00
|
|
|
if (!jsonExpanded.items.length) {
|
|
|
|
Logger.info(`[PlaylistController] Playlist "${jsonExpanded.name}" has no more items - removing it`)
|
|
|
|
await req.playlist.destroy()
|
|
|
|
SocketAuthority.clientEmitter(jsonExpanded.userId, 'playlist_removed', jsonExpanded)
|
2022-11-27 19:04:49 +01:00
|
|
|
} else {
|
2023-08-13 18:22:38 +02:00
|
|
|
SocketAuthority.clientEmitter(jsonExpanded.userId, 'playlist_updated', jsonExpanded)
|
2022-11-27 19:04:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
res.json(jsonExpanded)
|
2022-11-26 22:14:45 +01:00
|
|
|
}
|
|
|
|
|
2023-08-13 18:22:38 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/playlists/:id/batch/add
|
|
|
|
* Batch add playlist items
|
2024-08-11 22:15:34 +02:00
|
|
|
*
|
2025-01-01 00:01:42 +01:00
|
|
|
* @param {PlaylistControllerRequest} req
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {Response} res
|
2023-08-13 18:22:38 +02:00
|
|
|
*/
|
2022-11-26 22:14:45 +01:00
|
|
|
async addBatch(req, res) {
|
2025-01-01 00:01:42 +01:00
|
|
|
if (!req.body.items?.length || !Array.isArray(req.body.items) || req.body.items.some((i) => !i?.libraryItemId || typeof i.libraryItemId !== 'string' || (i.episodeId && typeof i.episodeId !== 'string'))) {
|
|
|
|
return res.status(400).send('Invalid request body items')
|
2023-08-13 18:22:38 +02:00
|
|
|
}
|
2022-11-26 22:14:45 +01:00
|
|
|
|
2023-08-13 18:22:38 +02:00
|
|
|
// Find all library items
|
2025-01-01 00:01:42 +01:00
|
|
|
const libraryItemIds = new Set(req.body.items.map((i) => i.libraryItemId).filter((i) => i))
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
const oldLibraryItems = await Database.libraryItemModel.getAllOldLibraryItems({ id: Array.from(libraryItemIds) })
|
|
|
|
if (oldLibraryItems.length !== libraryItemIds.size) {
|
|
|
|
return res.status(400).send('Invalid request body items')
|
|
|
|
}
|
|
|
|
|
|
|
|
req.playlist.playlistMediaItems = await req.playlist.getMediaItemsExpandedWithLibraryItem()
|
2023-08-13 18:22:38 +02:00
|
|
|
|
|
|
|
const mediaItemsToAdd = []
|
2025-01-01 00:01:42 +01:00
|
|
|
const jsonExpanded = req.playlist.toOldJSONExpanded()
|
2023-08-13 18:22:38 +02:00
|
|
|
|
|
|
|
// Setup array of playlistMediaItem records to add
|
2025-01-01 00:01:42 +01:00
|
|
|
let order = req.playlist.playlistMediaItems.length + 1
|
|
|
|
for (const item of req.body.items) {
|
|
|
|
const libraryItem = oldLibraryItems.find((li) => li.id === item.libraryItemId)
|
|
|
|
|
|
|
|
const mediaItemId = item.episodeId || libraryItem.media.id
|
|
|
|
if (req.playlist.playlistMediaItems.some((pmi) => pmi.mediaItemId === mediaItemId)) {
|
|
|
|
// Already exists in playlist
|
|
|
|
continue
|
2023-08-13 18:22:38 +02:00
|
|
|
} else {
|
2025-01-01 00:01:42 +01:00
|
|
|
mediaItemsToAdd.push({
|
|
|
|
playlistId: req.playlist.id,
|
|
|
|
mediaItemId,
|
|
|
|
mediaItemType: item.episodeId ? 'podcastEpisode' : 'book',
|
|
|
|
order: order++
|
|
|
|
})
|
|
|
|
|
|
|
|
// Add the new item to to the old json expanded to prevent having to fully reload the playlist media items
|
|
|
|
if (item.episodeId) {
|
|
|
|
const episode = libraryItem.media.episodes.find((ep) => ep.id === item.episodeId)
|
|
|
|
jsonExpanded.items.push({
|
|
|
|
episodeId: item.episodeId,
|
|
|
|
episode: episode.toJSONExpanded(),
|
|
|
|
libraryItemId: libraryItem.id,
|
|
|
|
libraryItem: libraryItem.toJSONMinified()
|
|
|
|
})
|
2023-08-13 18:22:38 +02:00
|
|
|
} else {
|
2025-01-01 00:01:42 +01:00
|
|
|
jsonExpanded.items.push({
|
|
|
|
libraryItemId: libraryItem.id,
|
|
|
|
libraryItem: libraryItem.toJSONExpanded()
|
2023-08-13 18:22:38 +02:00
|
|
|
})
|
|
|
|
}
|
2022-11-26 22:14:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-13 18:22:38 +02:00
|
|
|
if (mediaItemsToAdd.length) {
|
2025-01-01 00:01:42 +01:00
|
|
|
await Database.playlistMediaItemModel.bulkCreate(mediaItemsToAdd)
|
|
|
|
|
2023-08-13 18:22:38 +02:00
|
|
|
SocketAuthority.clientEmitter(req.playlist.userId, 'playlist_updated', jsonExpanded)
|
2022-11-26 22:14:45 +01:00
|
|
|
}
|
2025-01-01 00:01:42 +01:00
|
|
|
|
2022-11-26 22:14:45 +01:00
|
|
|
res.json(jsonExpanded)
|
|
|
|
}
|
|
|
|
|
2023-08-13 18:22:38 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/playlists/:id/batch/remove
|
|
|
|
* Batch remove playlist items
|
2024-08-11 22:15:34 +02:00
|
|
|
*
|
2025-01-01 00:01:42 +01:00
|
|
|
* @param {PlaylistControllerRequest} req
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {Response} res
|
2023-08-13 18:22:38 +02:00
|
|
|
*/
|
2022-11-26 22:14:45 +01:00
|
|
|
async removeBatch(req, res) {
|
2025-01-01 00:01:42 +01:00
|
|
|
if (!req.body.items?.length || !Array.isArray(req.body.items) || req.body.items.some((i) => !i?.libraryItemId || typeof i.libraryItemId !== 'string' || (i.episodeId && typeof i.episodeId !== 'string'))) {
|
|
|
|
return res.status(400).send('Invalid request body items')
|
2022-11-26 22:14:45 +01:00
|
|
|
}
|
2023-08-13 18:22:38 +02:00
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
req.playlist.playlistMediaItems = await req.playlist.getMediaItemsExpandedWithLibraryItem()
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
// Remove playlist media items
|
|
|
|
let hasUpdated = false
|
|
|
|
for (const item of req.body.items) {
|
|
|
|
let playlistMediaItem = null
|
|
|
|
if (item.episodeId) {
|
|
|
|
playlistMediaItem = req.playlist.playlistMediaItems.find((pmi) => pmi.mediaItemId === item.episodeId)
|
|
|
|
} else {
|
|
|
|
playlistMediaItem = req.playlist.playlistMediaItems.find((pmi) => pmi.mediaItem.libraryItem?.id === item.libraryItemId)
|
|
|
|
}
|
|
|
|
if (!playlistMediaItem) {
|
|
|
|
Logger.warn(`[PlaylistController] Playlist item not found in playlist ${req.playlist.id}`, item)
|
|
|
|
continue
|
2022-11-26 22:14:45 +01:00
|
|
|
}
|
2023-08-13 18:22:38 +02:00
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
await playlistMediaItem.destroy()
|
|
|
|
req.playlist.playlistMediaItems = req.playlist.playlistMediaItems.filter((pmi) => pmi.id !== playlistMediaItem.id)
|
2023-08-13 18:22:38 +02:00
|
|
|
|
|
|
|
hasUpdated = true
|
2022-11-26 22:14:45 +01:00
|
|
|
}
|
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
const jsonExpanded = req.playlist.toOldJSONExpanded()
|
2022-11-26 22:14:45 +01:00
|
|
|
if (hasUpdated) {
|
2022-11-27 19:04:49 +01:00
|
|
|
// Playlist is removed when there are no items
|
2025-01-01 00:01:42 +01:00
|
|
|
if (!req.playlist.playlistMediaItems.length) {
|
2023-08-13 18:22:38 +02:00
|
|
|
Logger.info(`[PlaylistController] Playlist "${req.playlist.name}" has no more items - removing it`)
|
|
|
|
await req.playlist.destroy()
|
2023-09-17 19:40:13 +02:00
|
|
|
SocketAuthority.clientEmitter(jsonExpanded.userId, 'playlist_removed', jsonExpanded)
|
2022-11-27 19:04:49 +01:00
|
|
|
} else {
|
2023-09-17 19:40:13 +02:00
|
|
|
SocketAuthority.clientEmitter(jsonExpanded.userId, 'playlist_updated', jsonExpanded)
|
2022-11-27 19:04:49 +01:00
|
|
|
}
|
2022-11-26 22:14:45 +01:00
|
|
|
}
|
|
|
|
res.json(jsonExpanded)
|
|
|
|
}
|
|
|
|
|
2023-08-13 18:22:38 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/playlists/collection/:collectionId
|
|
|
|
* Create a playlist from a collection
|
2024-08-11 22:15:34 +02:00
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2023-08-13 18:22:38 +02:00
|
|
|
*/
|
2022-12-18 00:31:19 +01:00
|
|
|
async createFromCollection(req, res) {
|
2023-08-20 20:34:03 +02:00
|
|
|
const collection = await Database.collectionModel.findByPk(req.params.collectionId)
|
2022-12-18 00:31:19 +01:00
|
|
|
if (!collection) {
|
|
|
|
return res.status(404).send('Collection not found')
|
|
|
|
}
|
|
|
|
// Expand collection to get library items
|
2024-08-11 23:07:29 +02:00
|
|
|
const collectionExpanded = await collection.getOldJsonExpanded(req.user)
|
2023-08-13 18:22:38 +02:00
|
|
|
if (!collectionExpanded) {
|
|
|
|
// This can happen if the user has no access to all items in collection
|
|
|
|
return res.status(404).send('Collection not found')
|
2022-12-18 00:31:19 +01:00
|
|
|
}
|
|
|
|
|
2023-08-13 18:22:38 +02:00
|
|
|
// Playlists cannot be empty
|
|
|
|
if (!collectionExpanded.books.length) {
|
|
|
|
return res.status(400).send('Collection has no books')
|
|
|
|
}
|
2022-12-18 00:31:19 +01:00
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
const transaction = await Database.sequelize.transaction()
|
|
|
|
try {
|
|
|
|
const playlist = await Database.playlistModel.create(
|
|
|
|
{
|
|
|
|
userId: req.user.id,
|
|
|
|
libraryId: collection.libraryId,
|
|
|
|
name: collection.name,
|
|
|
|
description: collection.description || null
|
|
|
|
},
|
|
|
|
{ transaction }
|
|
|
|
)
|
|
|
|
|
|
|
|
const mediaItemsToAdd = []
|
|
|
|
for (const [index, libraryItem] of collectionExpanded.books.entries()) {
|
|
|
|
mediaItemsToAdd.push({
|
|
|
|
playlistId: playlist.id,
|
|
|
|
mediaItemId: libraryItem.media.id,
|
|
|
|
mediaItemType: 'book',
|
|
|
|
order: index + 1
|
|
|
|
})
|
|
|
|
}
|
|
|
|
await Database.playlistMediaItemModel.bulkCreate(mediaItemsToAdd, { transaction })
|
2023-08-13 18:22:38 +02:00
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
await transaction.commit()
|
2023-08-13 18:22:38 +02:00
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
playlist.playlistMediaItems = await playlist.getMediaItemsExpandedWithLibraryItem()
|
2022-12-18 00:31:19 +01:00
|
|
|
|
2025-01-01 00:01:42 +01:00
|
|
|
const jsonExpanded = playlist.toOldJSONExpanded()
|
|
|
|
SocketAuthority.clientEmitter(playlist.userId, 'playlist_added', jsonExpanded)
|
|
|
|
res.json(jsonExpanded)
|
|
|
|
} catch (error) {
|
|
|
|
await transaction.rollback()
|
|
|
|
Logger.error('[PlaylistController] createFromCollection:', error)
|
|
|
|
res.status(500).send('Failed to create playlist')
|
|
|
|
}
|
2022-12-18 00:31:19 +01:00
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
* @param {NextFunction} next
|
|
|
|
*/
|
2023-07-23 16:42:57 +02:00
|
|
|
async middleware(req, res, next) {
|
2022-11-26 22:14:45 +01:00
|
|
|
if (req.params.id) {
|
2023-08-20 20:34:03 +02:00
|
|
|
const playlist = await Database.playlistModel.findByPk(req.params.id)
|
2022-11-26 22:14:45 +01:00
|
|
|
if (!playlist) {
|
|
|
|
return res.status(404).send('Playlist not found')
|
|
|
|
}
|
2024-08-11 23:07:29 +02:00
|
|
|
if (playlist.userId !== req.user.id) {
|
|
|
|
Logger.warn(`[PlaylistController] Playlist ${req.params.id} requested by user ${req.user.id} that is not the owner`)
|
2022-11-26 22:14:45 +01:00
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
req.playlist = playlist
|
|
|
|
}
|
|
|
|
|
|
|
|
next()
|
|
|
|
}
|
|
|
|
}
|
2024-08-11 00:15:21 +02:00
|
|
|
module.exports = new PlaylistController()
|