2024-08-11 22:15:34 +02:00
|
|
|
const { Request, Response, NextFunction } = require('express')
|
2022-03-12 02:46:32 +01:00
|
|
|
const Logger = require('../Logger')
|
2022-11-24 22:53:58 +01:00
|
|
|
const SocketAuthority = require('../SocketAuthority')
|
2023-07-05 01:14:44 +02:00
|
|
|
const Database = require('../Database')
|
2023-08-13 22:10:26 +02:00
|
|
|
const libraryItemsBookFilters = require('../utils/queries/libraryItemsBookFilters')
|
2022-03-12 02:46:32 +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
|
2024-09-01 22:08:56 +02:00
|
|
|
*
|
|
|
|
* @typedef RequestEntityObject
|
|
|
|
* @property {import('../models/Series')} series
|
|
|
|
*
|
|
|
|
* @typedef {RequestWithUser & RequestEntityObject} SeriesControllerRequest
|
2024-08-11 22:15:34 +02:00
|
|
|
*/
|
|
|
|
|
2022-03-12 02:46:32 +01:00
|
|
|
class SeriesController {
|
2024-08-11 00:15:21 +02:00
|
|
|
constructor() {}
|
2022-03-12 02:46:32 +01:00
|
|
|
|
2023-07-08 00:59:17 +02:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* /api/series/:id
|
2024-08-11 00:15:21 +02:00
|
|
|
*
|
2023-07-08 00:59:17 +02:00
|
|
|
* TODO: Update mobile app to use /api/libraries/:id/series/:seriesId API route instead
|
2023-07-08 16:57:32 +02:00
|
|
|
* Series are not library specific so we need to know what the library id is
|
2024-08-11 00:15:21 +02:00
|
|
|
*
|
2024-09-01 22:08:56 +02:00
|
|
|
* @param {SeriesControllerRequest} req
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {Response} res
|
2023-07-08 00:59:17 +02:00
|
|
|
*/
|
2022-03-13 01:50:31 +01:00
|
|
|
async findOne(req, res) {
|
2024-08-11 00:15:21 +02:00
|
|
|
const include = (req.query.include || '')
|
|
|
|
.split(',')
|
|
|
|
.map((v) => v.trim())
|
|
|
|
.filter((v) => !!v)
|
2022-04-25 00:46:21 +02:00
|
|
|
|
2024-09-01 22:08:56 +02:00
|
|
|
const seriesJson = req.series.toOldJSON()
|
2022-04-25 00:46:21 +02:00
|
|
|
|
|
|
|
// Add progress map with isFinished flag
|
|
|
|
if (include.includes('progress')) {
|
2023-05-28 15:51:34 +02:00
|
|
|
const libraryItemsInSeries = req.libraryItemsInSeries
|
2024-08-11 00:15:21 +02:00
|
|
|
const libraryItemsFinished = libraryItemsInSeries.filter((li) => {
|
2024-08-11 23:07:29 +02:00
|
|
|
return req.user.getMediaProgress(li.media.id)?.isFinished
|
2022-04-25 00:46:21 +02:00
|
|
|
})
|
|
|
|
seriesJson.progress = {
|
2024-08-11 00:15:21 +02:00
|
|
|
libraryItemIds: libraryItemsInSeries.map((li) => li.id),
|
|
|
|
libraryItemIdsFinished: libraryItemsFinished.map((li) => li.id),
|
2022-04-25 00:46:21 +02:00
|
|
|
isFinished: libraryItemsFinished.length === libraryItemsInSeries.length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-31 23:58:19 +01:00
|
|
|
if (include.includes('rssfeed')) {
|
2023-07-17 23:48:46 +02:00
|
|
|
const feedObj = await this.rssFeedManager.findFeedForEntityId(seriesJson.id)
|
2022-12-31 23:58:19 +01:00
|
|
|
seriesJson.rssFeed = feedObj?.toJSONMinified() || null
|
|
|
|
}
|
|
|
|
|
2023-07-08 00:59:17 +02:00
|
|
|
res.json(seriesJson)
|
2022-03-13 01:50:31 +01:00
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
2024-09-01 22:26:43 +02:00
|
|
|
* TODO: Currently unused in the client, should check for duplicate name
|
2024-08-11 22:15:34 +02:00
|
|
|
*
|
2024-09-01 22:08:56 +02:00
|
|
|
* @param {SeriesControllerRequest} req
|
2024-08-11 22:15:34 +02:00
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-09-28 00:48:45 +02:00
|
|
|
async update(req, res) {
|
2024-09-01 22:26:43 +02:00
|
|
|
const keysToUpdate = ['name', 'description']
|
|
|
|
const payload = {}
|
|
|
|
for (const key of keysToUpdate) {
|
|
|
|
if (req.body[key] !== undefined && typeof req.body[key] === 'string') {
|
|
|
|
payload[key] = req.body[key]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!Object.keys(payload).length) {
|
|
|
|
return res.status(400).send('No valid fields to update')
|
|
|
|
}
|
|
|
|
req.series.set(payload)
|
|
|
|
if (req.series.changed()) {
|
|
|
|
await req.series.save()
|
|
|
|
SocketAuthority.emitter('series_updated', req.series.toOldJSON())
|
2022-09-28 00:48:45 +02:00
|
|
|
}
|
2024-09-01 22:26:43 +02:00
|
|
|
res.json(req.series.toOldJSON())
|
2022-09-28 00:48:45 +02:00
|
|
|
}
|
|
|
|
|
2024-08-11 22:15:34 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
* @param {NextFunction} next
|
|
|
|
*/
|
2023-08-13 22:10:26 +02:00
|
|
|
async middleware(req, res, next) {
|
2024-09-01 22:08:56 +02:00
|
|
|
const series = await Database.seriesModel.findByPk(req.params.id)
|
2022-03-13 01:50:31 +01:00
|
|
|
if (!series) return res.sendStatus(404)
|
|
|
|
|
2023-07-08 00:59:17 +02:00
|
|
|
/**
|
|
|
|
* Filter out any library items not accessible to user
|
|
|
|
*/
|
2024-08-11 23:07:29 +02:00
|
|
|
const libraryItems = await libraryItemsBookFilters.getLibraryItemsForSeries(series, req.user)
|
2023-08-13 22:10:26 +02:00
|
|
|
if (!libraryItems.length) {
|
2024-08-11 23:07:29 +02:00
|
|
|
Logger.warn(`[SeriesController] User "${req.user.username}" attempted to access series "${series.id}" with no accessible books`)
|
2023-08-13 22:10:26 +02:00
|
|
|
return res.sendStatus(404)
|
2023-05-28 15:51:34 +02:00
|
|
|
}
|
|
|
|
|
2024-08-11 23:07:29 +02:00
|
|
|
if (req.method == 'DELETE' && !req.user.canDelete) {
|
|
|
|
Logger.warn(`[SeriesController] User "${req.user.username}" attempted to delete without permission`)
|
2022-03-13 01:50:31 +01:00
|
|
|
return res.sendStatus(403)
|
2024-08-11 23:07:29 +02:00
|
|
|
} else if ((req.method == 'PATCH' || req.method == 'POST') && !req.user.canUpdate) {
|
|
|
|
Logger.warn(`[SeriesController] User "${req.user.username}" attempted to update without permission`)
|
2022-03-13 01:50:31 +01:00
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.series = series
|
2023-08-13 22:10:26 +02:00
|
|
|
req.libraryItemsInSeries = libraryItems
|
2022-03-13 01:50:31 +01:00
|
|
|
next()
|
|
|
|
}
|
2022-03-12 02:46:32 +01:00
|
|
|
}
|
2024-08-11 00:15:21 +02:00
|
|
|
module.exports = new SeriesController()
|