2024-08-11 15:15:34 -05:00
|
|
|
const { Request, Response, NextFunction } = require('express')
|
2022-03-11 19:46:32 -06:00
|
|
|
const Logger = require('../Logger')
|
2022-11-24 15:53:58 -06:00
|
|
|
const SocketAuthority = require('../SocketAuthority')
|
2023-07-04 18:14:44 -05:00
|
|
|
const Database = require('../Database')
|
2023-08-13 15:10:26 -05:00
|
|
|
const libraryItemsBookFilters = require('../utils/queries/libraryItemsBookFilters')
|
2022-03-11 19:46:32 -06:00
|
|
|
|
2024-08-11 15:15:34 -05:00
|
|
|
/**
|
|
|
|
* @typedef RequestUserObjects
|
2024-08-11 16:07:29 -05:00
|
|
|
* @property {import('../models/User')} user
|
2024-08-11 15:15:34 -05:00
|
|
|
*
|
|
|
|
* @typedef {Request & RequestUserObjects} RequestWithUser
|
|
|
|
*/
|
|
|
|
|
2022-03-11 19:46:32 -06:00
|
|
|
class SeriesController {
|
2024-08-10 17:15:21 -05:00
|
|
|
constructor() {}
|
2022-03-11 19:46:32 -06:00
|
|
|
|
2023-07-07 17:59:17 -05:00
|
|
|
/**
|
|
|
|
* @deprecated
|
|
|
|
* /api/series/:id
|
2024-08-10 17:15:21 -05:00
|
|
|
*
|
2023-07-07 17:59:17 -05:00
|
|
|
* TODO: Update mobile app to use /api/libraries/:id/series/:seriesId API route instead
|
2023-07-08 09:57:32 -05:00
|
|
|
* Series are not library specific so we need to know what the library id is
|
2024-08-10 17:15:21 -05:00
|
|
|
*
|
2024-08-11 15:15:34 -05:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2023-07-07 17:59:17 -05:00
|
|
|
*/
|
2022-03-12 18:50:31 -06:00
|
|
|
async findOne(req, res) {
|
2024-08-10 17:15:21 -05:00
|
|
|
const include = (req.query.include || '')
|
|
|
|
.split(',')
|
|
|
|
.map((v) => v.trim())
|
|
|
|
.filter((v) => !!v)
|
2022-04-24 17:46:21 -05:00
|
|
|
|
2022-12-31 16:58:19 -06:00
|
|
|
const seriesJson = req.series.toJSON()
|
2022-04-24 17:46:21 -05:00
|
|
|
|
|
|
|
// Add progress map with isFinished flag
|
|
|
|
if (include.includes('progress')) {
|
2023-05-28 08:51:34 -05:00
|
|
|
const libraryItemsInSeries = req.libraryItemsInSeries
|
2024-08-10 17:15:21 -05:00
|
|
|
const libraryItemsFinished = libraryItemsInSeries.filter((li) => {
|
2024-08-11 16:07:29 -05:00
|
|
|
return req.user.getMediaProgress(li.media.id)?.isFinished
|
2022-04-24 17:46:21 -05:00
|
|
|
})
|
|
|
|
seriesJson.progress = {
|
2024-08-10 17:15:21 -05:00
|
|
|
libraryItemIds: libraryItemsInSeries.map((li) => li.id),
|
|
|
|
libraryItemIdsFinished: libraryItemsFinished.map((li) => li.id),
|
2022-04-24 17:46:21 -05:00
|
|
|
isFinished: libraryItemsFinished.length === libraryItemsInSeries.length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-31 16:58:19 -06:00
|
|
|
if (include.includes('rssfeed')) {
|
2023-07-17 16:48:46 -05:00
|
|
|
const feedObj = await this.rssFeedManager.findFeedForEntityId(seriesJson.id)
|
2022-12-31 16:58:19 -06:00
|
|
|
seriesJson.rssFeed = feedObj?.toJSONMinified() || null
|
|
|
|
}
|
|
|
|
|
2023-07-07 17:59:17 -05:00
|
|
|
res.json(seriesJson)
|
2022-03-12 18:50:31 -06:00
|
|
|
}
|
|
|
|
|
2024-08-11 15:15:34 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2022-09-27 17:48:45 -05:00
|
|
|
async update(req, res) {
|
|
|
|
const hasUpdated = req.series.update(req.body)
|
|
|
|
if (hasUpdated) {
|
2023-07-04 18:14:44 -05:00
|
|
|
await Database.updateSeries(req.series)
|
2022-12-22 16:26:11 -06:00
|
|
|
SocketAuthority.emitter('series_updated', req.series.toJSON())
|
2022-09-27 17:48:45 -05:00
|
|
|
}
|
2022-12-23 07:33:33 -06:00
|
|
|
res.json(req.series.toJSON())
|
2022-09-27 17:48:45 -05:00
|
|
|
}
|
|
|
|
|
2024-08-11 15:15:34 -05:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
* @param {NextFunction} next
|
|
|
|
*/
|
2023-08-13 15:10:26 -05:00
|
|
|
async middleware(req, res, next) {
|
2023-09-03 10:49:02 -05:00
|
|
|
const series = await Database.seriesModel.getOldById(req.params.id)
|
2022-03-12 18:50:31 -06:00
|
|
|
if (!series) return res.sendStatus(404)
|
|
|
|
|
2023-07-07 17:59:17 -05:00
|
|
|
/**
|
|
|
|
* Filter out any library items not accessible to user
|
|
|
|
*/
|
2024-08-11 16:07:29 -05:00
|
|
|
const libraryItems = await libraryItemsBookFilters.getLibraryItemsForSeries(series, req.user)
|
2023-08-13 15:10:26 -05:00
|
|
|
if (!libraryItems.length) {
|
2024-08-11 16:07:29 -05:00
|
|
|
Logger.warn(`[SeriesController] User "${req.user.username}" attempted to access series "${series.id}" with no accessible books`)
|
2023-08-13 15:10:26 -05:00
|
|
|
return res.sendStatus(404)
|
2023-05-28 08:51:34 -05:00
|
|
|
}
|
|
|
|
|
2024-08-11 16:07:29 -05:00
|
|
|
if (req.method == 'DELETE' && !req.user.canDelete) {
|
|
|
|
Logger.warn(`[SeriesController] User "${req.user.username}" attempted to delete without permission`)
|
2022-03-12 18:50:31 -06:00
|
|
|
return res.sendStatus(403)
|
2024-08-11 16:07:29 -05: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-12 18:50:31 -06:00
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.series = series
|
2023-08-13 15:10:26 -05:00
|
|
|
req.libraryItemsInSeries = libraryItems
|
2022-03-12 18:50:31 -06:00
|
|
|
next()
|
|
|
|
}
|
2022-03-11 19:46:32 -06:00
|
|
|
}
|
2024-08-10 17:15:21 -05:00
|
|
|
module.exports = new SeriesController()
|