2024-08-12 00:01:25 +02:00
|
|
|
const { Request, Response, NextFunction } = require('express')
|
2023-08-12 22:01:27 +02:00
|
|
|
const Sequelize = require('sequelize')
|
2021-11-22 03:00:40 +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-11-24 22:53:58 +01:00
|
|
|
|
2024-12-15 19:37:01 +01:00
|
|
|
const RssFeedManager = require('../managers/RssFeedManager')
|
2022-11-12 00:13:10 +01:00
|
|
|
const Collection = require('../objects/Collection')
|
2021-11-22 03:00:40 +01:00
|
|
|
|
2024-08-12 00:01:25 +02:00
|
|
|
/**
|
|
|
|
* @typedef RequestUserObject
|
|
|
|
* @property {import('../models/User')} user
|
|
|
|
*
|
|
|
|
* @typedef {Request & RequestUserObject} RequestWithUser
|
|
|
|
*/
|
|
|
|
|
2021-11-22 03:00:40 +01:00
|
|
|
class CollectionController {
|
2024-08-11 00:15:21 +02:00
|
|
|
constructor() {}
|
2021-11-22 03:00:40 +01:00
|
|
|
|
2023-08-12 22:01:27 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/collections
|
|
|
|
* Create new collection
|
2024-08-12 00:01:25 +02:00
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2023-08-12 22:01:27 +02:00
|
|
|
*/
|
2021-11-22 03:00:40 +01:00
|
|
|
async create(req, res) {
|
2023-08-12 00:49:06 +02:00
|
|
|
const newCollection = new Collection()
|
2024-08-11 23:07:29 +02:00
|
|
|
req.body.userId = req.user.id
|
2023-08-12 00:49:06 +02:00
|
|
|
if (!newCollection.setData(req.body)) {
|
2023-08-12 22:01:27 +02:00
|
|
|
return res.status(400).send('Invalid collection data')
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2023-08-12 00:49:06 +02:00
|
|
|
|
2023-08-12 22:01:27 +02:00
|
|
|
// Create collection record
|
2023-08-20 20:34:03 +02:00
|
|
|
await Database.collectionModel.createFromOld(newCollection)
|
2023-08-12 22:01:27 +02:00
|
|
|
|
|
|
|
// Get library items in collection
|
2023-08-20 20:34:03 +02:00
|
|
|
const libraryItemsInCollection = await Database.libraryItemModel.getForCollection(newCollection)
|
2023-08-12 22:01:27 +02:00
|
|
|
|
|
|
|
// Create collectionBook records
|
|
|
|
let order = 1
|
|
|
|
const collectionBooksToAdd = []
|
|
|
|
for (const libraryItemId of newCollection.books) {
|
2024-08-11 00:15:21 +02:00
|
|
|
const libraryItem = libraryItemsInCollection.find((li) => li.id === libraryItemId)
|
2023-08-12 22:01:27 +02:00
|
|
|
if (libraryItem) {
|
|
|
|
collectionBooksToAdd.push({
|
|
|
|
collectionId: newCollection.id,
|
|
|
|
bookId: libraryItem.media.id,
|
|
|
|
order: order++
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (collectionBooksToAdd.length) {
|
|
|
|
await Database.createBulkCollectionBooks(collectionBooksToAdd)
|
|
|
|
}
|
|
|
|
|
2023-08-12 00:49:06 +02:00
|
|
|
const jsonExpanded = newCollection.toJSONExpanded(libraryItemsInCollection)
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('collection_added', jsonExpanded)
|
2021-11-22 03:00:40 +01:00
|
|
|
res.json(jsonExpanded)
|
|
|
|
}
|
|
|
|
|
2024-08-12 00:01:25 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/collections
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2023-07-22 23:18:55 +02:00
|
|
|
async findAll(req, res) {
|
2024-08-11 23:07:29 +02:00
|
|
|
const collectionsExpanded = await Database.collectionModel.getOldCollectionsJsonExpanded(req.user)
|
2022-11-29 18:48:21 +01:00
|
|
|
res.json({
|
2023-08-12 00:49:06 +02:00
|
|
|
collections: collectionsExpanded
|
2022-11-29 18:48:21 +01:00
|
|
|
})
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
|
2024-08-12 00:01:25 +02:00
|
|
|
/**
|
|
|
|
* GET: /api/collections/:id
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2023-07-17 23:48:46 +02:00
|
|
|
async findOne(req, res) {
|
2022-12-28 01:03:31 +01:00
|
|
|
const includeEntities = (req.query.include || '').split(',')
|
|
|
|
|
2024-08-11 23:07:29 +02:00
|
|
|
const collectionExpanded = await req.collection.getOldJsonExpanded(req.user, includeEntities)
|
2023-08-12 22:01:27 +02:00
|
|
|
if (!collectionExpanded) {
|
|
|
|
// This may happen if the user is restricted from all books
|
|
|
|
return res.sendStatus(404)
|
2022-12-28 01:03:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
res.json(collectionExpanded)
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
|
2023-08-12 22:01:27 +02:00
|
|
|
/**
|
|
|
|
* PATCH: /api/collections/:id
|
|
|
|
* Update collection
|
2024-08-12 00:01:25 +02:00
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2023-08-12 22:01:27 +02:00
|
|
|
*/
|
2021-11-22 03:00:40 +01:00
|
|
|
async update(req, res) {
|
2023-08-12 22:01:27 +02:00
|
|
|
let wasUpdated = false
|
|
|
|
|
|
|
|
// Update description and name if defined
|
|
|
|
const collectionUpdatePayload = {}
|
|
|
|
if (req.body.description !== undefined && req.body.description !== req.collection.description) {
|
|
|
|
collectionUpdatePayload.description = req.body.description
|
|
|
|
wasUpdated = true
|
|
|
|
}
|
|
|
|
if (req.body.name !== undefined && req.body.name !== req.collection.name) {
|
|
|
|
collectionUpdatePayload.name = req.body.name
|
|
|
|
wasUpdated = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if (wasUpdated) {
|
|
|
|
await req.collection.update(collectionUpdatePayload)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If books array is passed in then update order in collection
|
2024-12-15 23:56:59 +01:00
|
|
|
let collectionBooksUpdated = false
|
2023-08-12 22:01:27 +02:00
|
|
|
if (req.body.books?.length) {
|
|
|
|
const collectionBooks = await req.collection.getCollectionBooks({
|
|
|
|
include: {
|
2023-08-20 20:34:03 +02:00
|
|
|
model: Database.bookModel,
|
|
|
|
include: Database.libraryItemModel
|
2023-08-12 22:01:27 +02:00
|
|
|
},
|
|
|
|
order: [['order', 'ASC']]
|
|
|
|
})
|
|
|
|
collectionBooks.sort((a, b) => {
|
2024-08-11 00:15:21 +02:00
|
|
|
const aIndex = req.body.books.findIndex((lid) => lid === a.book.libraryItem.id)
|
|
|
|
const bIndex = req.body.books.findIndex((lid) => lid === b.book.libraryItem.id)
|
2023-08-12 22:01:27 +02:00
|
|
|
return aIndex - bIndex
|
|
|
|
})
|
|
|
|
for (let i = 0; i < collectionBooks.length; i++) {
|
|
|
|
if (collectionBooks[i].order !== i + 1) {
|
|
|
|
await collectionBooks[i].update({
|
|
|
|
order: i + 1
|
|
|
|
})
|
2024-12-15 23:56:59 +01:00
|
|
|
collectionBooksUpdated = true
|
2023-08-12 22:01:27 +02:00
|
|
|
}
|
|
|
|
}
|
2024-12-15 23:56:59 +01:00
|
|
|
|
|
|
|
if (collectionBooksUpdated) {
|
|
|
|
req.collection.changed('updatedAt', true)
|
|
|
|
await req.collection.save()
|
|
|
|
wasUpdated = true
|
|
|
|
}
|
2023-08-12 22:01:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const jsonExpanded = await req.collection.getOldJsonExpanded()
|
2021-11-22 03:00:40 +01:00
|
|
|
if (wasUpdated) {
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('collection_updated', jsonExpanded)
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
|
|
|
res.json(jsonExpanded)
|
|
|
|
}
|
|
|
|
|
2024-08-12 00:01:25 +02:00
|
|
|
/**
|
|
|
|
* DELETE: /api/collections/:id
|
|
|
|
*
|
2024-12-15 19:37:01 +01:00
|
|
|
* @this {import('../routers/ApiRouter')}
|
|
|
|
*
|
2024-08-12 00:01:25 +02:00
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
*/
|
2021-11-22 03:00:40 +01:00
|
|
|
async delete(req, res) {
|
2023-08-12 22:01:27 +02:00
|
|
|
const jsonExpanded = await req.collection.getOldJsonExpanded()
|
2022-12-31 21:08:34 +01:00
|
|
|
|
|
|
|
// Close rss feed - remove from db and emit socket event
|
2024-12-15 19:37:01 +01:00
|
|
|
await RssFeedManager.closeFeedForEntityId(req.collection.id)
|
2023-08-12 22:01:27 +02:00
|
|
|
|
|
|
|
await req.collection.destroy()
|
2022-12-31 21:08:34 +01:00
|
|
|
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('collection_removed', jsonExpanded)
|
2021-11-22 03:00:40 +01:00
|
|
|
res.sendStatus(200)
|
|
|
|
}
|
|
|
|
|
2023-08-12 22:01:27 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/collections/:id/book
|
|
|
|
* Add a single book to a collection
|
|
|
|
* Req.body { id: <library item id> }
|
2024-08-12 00:01:25 +02:00
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2023-08-12 22:01:27 +02:00
|
|
|
*/
|
2021-11-22 03:00:40 +01:00
|
|
|
async addBook(req, res) {
|
2023-08-20 20:34:03 +02:00
|
|
|
const libraryItem = await Database.libraryItemModel.getOldById(req.body.id)
|
2022-03-13 01:50:31 +01:00
|
|
|
if (!libraryItem) {
|
2023-08-12 22:01:27 +02:00
|
|
|
return res.status(404).send('Book not found')
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2023-08-12 22:01:27 +02:00
|
|
|
if (libraryItem.libraryId !== req.collection.libraryId) {
|
|
|
|
return res.status(400).send('Book in different library')
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2023-08-12 22:01:27 +02:00
|
|
|
|
|
|
|
// Check if book is already in collection
|
|
|
|
const collectionBooks = await req.collection.getCollectionBooks()
|
2024-08-11 00:15:21 +02:00
|
|
|
if (collectionBooks.some((cb) => cb.bookId === libraryItem.media.id)) {
|
2023-08-12 22:01:27 +02:00
|
|
|
return res.status(400).send('Book already in collection')
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-12 22:01:27 +02:00
|
|
|
// Create collectionBook record
|
2023-08-20 20:34:03 +02:00
|
|
|
await Database.collectionBookModel.create({
|
2023-08-12 22:01:27 +02:00
|
|
|
collectionId: req.collection.id,
|
2023-07-05 01:14:44 +02:00
|
|
|
bookId: libraryItem.media.id,
|
2023-08-12 22:01:27 +02:00
|
|
|
order: collectionBooks.length + 1
|
|
|
|
})
|
|
|
|
const jsonExpanded = await req.collection.getOldJsonExpanded()
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('collection_updated', jsonExpanded)
|
2021-11-22 03:00:40 +01:00
|
|
|
res.json(jsonExpanded)
|
|
|
|
}
|
|
|
|
|
2023-08-12 22:01:27 +02:00
|
|
|
/**
|
|
|
|
* DELETE: /api/collections/:id/book/:bookId
|
|
|
|
* Remove a single book from a collection. Re-order books
|
|
|
|
* TODO: bookId is actually libraryItemId. Clients need updating to use bookId
|
2024-08-12 00:01:25 +02:00
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2023-08-12 22:01:27 +02:00
|
|
|
*/
|
2021-11-22 03:00:40 +01:00
|
|
|
async removeBook(req, res) {
|
2023-08-20 20:34:03 +02:00
|
|
|
const libraryItem = await Database.libraryItemModel.getOldById(req.params.bookId)
|
2023-07-05 01:14:44 +02:00
|
|
|
if (!libraryItem) {
|
|
|
|
return res.sendStatus(404)
|
|
|
|
}
|
|
|
|
|
2023-08-12 22:01:27 +02:00
|
|
|
// Get books in collection ordered
|
|
|
|
const collectionBooks = await req.collection.getCollectionBooks({
|
|
|
|
order: [['order', 'ASC']]
|
|
|
|
})
|
|
|
|
|
|
|
|
let jsonExpanded = null
|
2024-08-11 00:15:21 +02:00
|
|
|
const collectionBookToRemove = collectionBooks.find((cb) => cb.bookId === libraryItem.media.id)
|
2023-08-12 22:01:27 +02:00
|
|
|
if (collectionBookToRemove) {
|
|
|
|
// Remove collection book record
|
|
|
|
await collectionBookToRemove.destroy()
|
|
|
|
|
|
|
|
// Update order on collection books
|
|
|
|
let order = 1
|
|
|
|
for (const collectionBook of collectionBooks) {
|
|
|
|
if (collectionBook.bookId === libraryItem.media.id) continue
|
|
|
|
if (collectionBook.order !== order) {
|
|
|
|
await collectionBook.update({
|
|
|
|
order
|
|
|
|
})
|
|
|
|
}
|
|
|
|
order++
|
|
|
|
}
|
|
|
|
|
|
|
|
jsonExpanded = await req.collection.getOldJsonExpanded()
|
2022-11-24 22:53:58 +01:00
|
|
|
SocketAuthority.emitter('collection_updated', jsonExpanded)
|
2023-08-12 22:01:27 +02:00
|
|
|
} else {
|
|
|
|
jsonExpanded = await req.collection.getOldJsonExpanded()
|
2021-11-27 23:01:53 +01:00
|
|
|
}
|
2023-08-12 22:01:27 +02:00
|
|
|
res.json(jsonExpanded)
|
2021-11-27 23:01:53 +01:00
|
|
|
}
|
|
|
|
|
2023-08-12 22:01:27 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/collections/:id/batch/add
|
|
|
|
* Add multiple books to collection
|
|
|
|
* Req.body { books: <Array of library item ids> }
|
2024-08-12 00:01:25 +02:00
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2023-08-12 22:01:27 +02:00
|
|
|
*/
|
2021-11-27 23:01:53 +01:00
|
|
|
async addBatch(req, res) {
|
2023-08-12 22:01:27 +02:00
|
|
|
// filter out invalid libraryItemIds
|
2024-08-11 00:15:21 +02:00
|
|
|
const bookIdsToAdd = (req.body.books || []).filter((b) => !!b && typeof b == 'string')
|
2023-08-12 22:01:27 +02:00
|
|
|
if (!bookIdsToAdd.length) {
|
2021-11-27 23:01:53 +01:00
|
|
|
return res.status(500).send('Invalid request body')
|
|
|
|
}
|
2023-08-12 22:01:27 +02:00
|
|
|
|
|
|
|
// Get library items associated with ids
|
2023-08-20 20:34:03 +02:00
|
|
|
const libraryItems = await Database.libraryItemModel.findAll({
|
2023-08-12 22:01:27 +02:00
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
[Sequelize.Op.in]: bookIdsToAdd
|
|
|
|
}
|
|
|
|
},
|
|
|
|
include: {
|
2023-08-20 20:34:03 +02:00
|
|
|
model: Database.bookModel
|
2023-08-12 22:01:27 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Get collection books already in collection
|
|
|
|
const collectionBooks = await req.collection.getCollectionBooks()
|
|
|
|
|
|
|
|
let order = collectionBooks.length + 1
|
2023-07-05 01:14:44 +02:00
|
|
|
const collectionBooksToAdd = []
|
|
|
|
let hasUpdated = false
|
|
|
|
|
2023-08-12 22:01:27 +02:00
|
|
|
// Check and set new collection books to add
|
|
|
|
for (const libraryItem of libraryItems) {
|
2024-08-11 00:15:21 +02:00
|
|
|
if (!collectionBooks.some((cb) => cb.bookId === libraryItem.media.id)) {
|
2023-07-05 01:14:44 +02:00
|
|
|
collectionBooksToAdd.push({
|
2023-08-12 22:01:27 +02:00
|
|
|
collectionId: req.collection.id,
|
2023-07-05 01:14:44 +02:00
|
|
|
bookId: libraryItem.media.id,
|
|
|
|
order: order++
|
|
|
|
})
|
2021-11-27 23:01:53 +01:00
|
|
|
hasUpdated = true
|
2023-08-12 22:01:27 +02:00
|
|
|
} else {
|
|
|
|
Logger.warn(`[CollectionController] addBatch: Library item ${libraryItem.id} already in collection`)
|
2021-11-27 23:01:53 +01:00
|
|
|
}
|
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-12 22:01:27 +02:00
|
|
|
let jsonExpanded = null
|
2021-11-27 23:01:53 +01:00
|
|
|
if (hasUpdated) {
|
2023-07-05 01:14:44 +02:00
|
|
|
await Database.createBulkCollectionBooks(collectionBooksToAdd)
|
2023-08-12 22:01:27 +02:00
|
|
|
jsonExpanded = await req.collection.getOldJsonExpanded()
|
|
|
|
SocketAuthority.emitter('collection_updated', jsonExpanded)
|
|
|
|
} else {
|
|
|
|
jsonExpanded = await req.collection.getOldJsonExpanded()
|
2021-11-27 23:01:53 +01:00
|
|
|
}
|
2023-08-12 22:01:27 +02:00
|
|
|
res.json(jsonExpanded)
|
2021-11-27 23:01:53 +01:00
|
|
|
}
|
|
|
|
|
2023-08-12 22:01:27 +02:00
|
|
|
/**
|
|
|
|
* POST: /api/collections/:id/batch/remove
|
|
|
|
* Remove multiple books from collection
|
|
|
|
* Req.body { books: <Array of library item ids> }
|
2024-08-12 00:01:25 +02:00
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
2023-08-12 22:01:27 +02:00
|
|
|
*/
|
2021-11-27 23:01:53 +01:00
|
|
|
async removeBatch(req, res) {
|
2023-08-12 22:01:27 +02:00
|
|
|
// filter out invalid libraryItemIds
|
2024-08-11 00:15:21 +02:00
|
|
|
const bookIdsToRemove = (req.body.books || []).filter((b) => !!b && typeof b == 'string')
|
2023-08-12 22:01:27 +02:00
|
|
|
if (!bookIdsToRemove.length) {
|
2021-11-27 23:01:53 +01:00
|
|
|
return res.status(500).send('Invalid request body')
|
|
|
|
}
|
2023-07-05 01:14:44 +02:00
|
|
|
|
2023-08-12 22:01:27 +02:00
|
|
|
// Get library items associated with ids
|
2023-08-20 20:34:03 +02:00
|
|
|
const libraryItems = await Database.libraryItemModel.findAll({
|
2023-08-12 22:01:27 +02:00
|
|
|
where: {
|
|
|
|
id: {
|
|
|
|
[Sequelize.Op.in]: bookIdsToRemove
|
|
|
|
}
|
|
|
|
},
|
|
|
|
include: {
|
2023-08-20 20:34:03 +02:00
|
|
|
model: Database.bookModel
|
2023-08-12 22:01:27 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
// Get collection books already in collection
|
|
|
|
const collectionBooks = await req.collection.getCollectionBooks({
|
|
|
|
order: [['order', 'ASC']]
|
|
|
|
})
|
|
|
|
|
|
|
|
// Remove collection books and update order
|
|
|
|
let order = 1
|
|
|
|
let hasUpdated = false
|
|
|
|
for (const collectionBook of collectionBooks) {
|
2024-08-11 00:15:21 +02:00
|
|
|
if (libraryItems.some((li) => li.media.id === collectionBook.bookId)) {
|
2023-08-12 22:01:27 +02:00
|
|
|
await collectionBook.destroy()
|
|
|
|
hasUpdated = true
|
|
|
|
continue
|
|
|
|
} else if (collectionBook.order !== order) {
|
|
|
|
await collectionBook.update({
|
|
|
|
order
|
|
|
|
})
|
2021-11-27 23:01:53 +01:00
|
|
|
hasUpdated = true
|
|
|
|
}
|
2023-08-12 22:01:27 +02:00
|
|
|
order++
|
2021-11-27 23:01:53 +01:00
|
|
|
}
|
2023-08-12 22:01:27 +02:00
|
|
|
|
|
|
|
let jsonExpanded = await req.collection.getOldJsonExpanded()
|
2021-11-27 23:01:53 +01:00
|
|
|
if (hasUpdated) {
|
2023-08-12 22:01:27 +02:00
|
|
|
SocketAuthority.emitter('collection_updated', jsonExpanded)
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2023-08-12 22:01:27 +02:00
|
|
|
res.json(jsonExpanded)
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2022-08-31 22:46:10 +02:00
|
|
|
|
2024-08-12 00:01:25 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @param {RequestWithUser} req
|
|
|
|
* @param {Response} res
|
|
|
|
* @param {NextFunction} next
|
|
|
|
*/
|
2023-07-22 23:18:55 +02:00
|
|
|
async middleware(req, res, next) {
|
2022-08-31 22:46:10 +02:00
|
|
|
if (req.params.id) {
|
2023-08-20 20:34:03 +02:00
|
|
|
const collection = await Database.collectionModel.findByPk(req.params.id)
|
2022-08-31 22:46:10 +02:00
|
|
|
if (!collection) {
|
|
|
|
return res.status(404).send('Collection not found')
|
|
|
|
}
|
|
|
|
req.collection = collection
|
|
|
|
}
|
|
|
|
|
2024-08-11 23:07:29 +02:00
|
|
|
if (req.method == 'DELETE' && !req.user.canDelete) {
|
|
|
|
Logger.warn(`[CollectionController] User "${req.user.username}" attempted to delete without permission`)
|
2022-08-31 22:46:10 +02: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(`[CollectionController] User "${req.user.username}" attempted to update without permission`)
|
2022-08-31 22:46:10 +02:00
|
|
|
return res.sendStatus(403)
|
|
|
|
}
|
|
|
|
|
|
|
|
next()
|
|
|
|
}
|
2021-11-22 03:00:40 +01:00
|
|
|
}
|
2024-08-11 00:15:21 +02:00
|
|
|
module.exports = new CollectionController()
|