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')
|
2022-03-12 02:46:32 +01:00
|
|
|
|
|
|
|
class SeriesController {
|
|
|
|
constructor() { }
|
|
|
|
|
2022-03-13 01:50:31 +01:00
|
|
|
async findOne(req, res) {
|
2022-12-31 23:58:19 +01:00
|
|
|
const include = (req.query.include || '').split(',').map(v => v.trim()).filter(v => !!v)
|
2022-04-25 00:46:21 +02:00
|
|
|
|
2022-12-31 23:58:19 +01:00
|
|
|
const seriesJson = req.series.toJSON()
|
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
|
2022-12-31 23:58:19 +01:00
|
|
|
const libraryItemsFinished = libraryItemsInSeries.filter(li => {
|
|
|
|
const mediaProgress = req.user.getMediaProgress(li.id)
|
2022-04-25 00:46:21 +02:00
|
|
|
return mediaProgress && mediaProgress.isFinished
|
|
|
|
})
|
|
|
|
seriesJson.progress = {
|
|
|
|
libraryItemIds: libraryItemsInSeries.map(li => li.id),
|
|
|
|
libraryItemIdsFinished: libraryItemsFinished.map(li => li.id),
|
|
|
|
isFinished: libraryItemsFinished.length === libraryItemsInSeries.length
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-31 23:58:19 +01:00
|
|
|
if (include.includes('rssfeed')) {
|
|
|
|
const feedObj = this.rssFeedManager.findFeedForEntityId(seriesJson.id)
|
|
|
|
seriesJson.rssFeed = feedObj?.toJSONMinified() || null
|
|
|
|
}
|
|
|
|
|
2022-04-25 00:46:21 +02:00
|
|
|
return res.json(seriesJson)
|
2022-03-13 01:50:31 +01:00
|
|
|
}
|
|
|
|
|
2022-03-12 02:46:32 +01:00
|
|
|
async search(req, res) {
|
|
|
|
var q = (req.query.q || '').toLowerCase()
|
|
|
|
if (!q) return res.json([])
|
|
|
|
var limit = (req.query.limit && !isNaN(req.query.limit)) ? Number(req.query.limit) : 25
|
2023-07-05 01:14:44 +02:00
|
|
|
var series = Database.series.filter(se => se.name.toLowerCase().includes(q))
|
2022-03-12 02:46:32 +01:00
|
|
|
series = series.slice(0, limit)
|
2022-11-29 19:08:40 +01:00
|
|
|
res.json({
|
|
|
|
results: series
|
|
|
|
})
|
2022-03-12 02:46:32 +01:00
|
|
|
}
|
2022-03-13 01:50:31 +01:00
|
|
|
|
2022-09-28 00:48:45 +02:00
|
|
|
async update(req, res) {
|
|
|
|
const hasUpdated = req.series.update(req.body)
|
|
|
|
if (hasUpdated) {
|
2023-07-05 01:14:44 +02:00
|
|
|
await Database.updateSeries(req.series)
|
2022-12-22 23:26:11 +01:00
|
|
|
SocketAuthority.emitter('series_updated', req.series.toJSON())
|
2022-09-28 00:48:45 +02:00
|
|
|
}
|
2022-12-23 14:33:33 +01:00
|
|
|
res.json(req.series.toJSON())
|
2022-09-28 00:48:45 +02:00
|
|
|
}
|
|
|
|
|
2022-03-13 01:50:31 +01:00
|
|
|
middleware(req, res, next) {
|
2023-07-05 01:14:44 +02:00
|
|
|
const series = Database.series.find(se => se.id === req.params.id)
|
2022-03-13 01:50:31 +01:00
|
|
|
if (!series) return res.sendStatus(404)
|
|
|
|
|
2023-07-05 01:14:44 +02:00
|
|
|
const libraryItemsInSeries = Database.libraryItems.filter(li => li.media.metadata.hasSeries?.(series.id))
|
2023-05-28 15:51:34 +02:00
|
|
|
if (libraryItemsInSeries.some(li => !req.user.checkCanAccessLibrary(li.libraryId))) {
|
|
|
|
Logger.warn(`[SeriesController] User attempted to access series "${series.id}" without access to the library`, req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
2022-03-13 01:50:31 +01:00
|
|
|
if (req.method == 'DELETE' && !req.user.canDelete) {
|
|
|
|
Logger.warn(`[SeriesController] User attempted to delete without permission`, req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
} else if ((req.method == 'PATCH' || req.method == 'POST') && !req.user.canUpdate) {
|
|
|
|
Logger.warn('[SeriesController] User attempted to update without permission', req.user)
|
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
req.series = series
|
2023-05-28 15:51:34 +02:00
|
|
|
req.libraryItemsInSeries = libraryItemsInSeries
|
2022-03-13 01:50:31 +01:00
|
|
|
next()
|
|
|
|
}
|
2022-03-12 02:46:32 +01:00
|
|
|
}
|
|
|
|
module.exports = new SeriesController()
|