PeerTube/server/middlewares/validators/bulk.ts

39 lines
1.3 KiB
TypeScript
Raw Normal View History

2021-08-27 14:32:44 +02:00
import express from 'express'
2020-05-14 16:56:15 +02:00
import { body } from 'express-validator'
import { isBulkRemoveCommentsOfScopeValid } from '@server/helpers/custom-validators/bulk'
2021-07-16 14:27:30 +02:00
import { HttpStatusCode, UserRight } from '@shared/models'
2020-05-14 16:56:15 +02:00
import { BulkRemoveCommentsOfBody } from '@shared/models/bulk/bulk-remove-comments-of-body.model'
import { areValidationErrors, doesAccountNameWithHostExist } from './shared'
2020-05-14 16:56:15 +02:00
const bulkRemoveCommentsOfValidator = [
body('accountName')
.exists(),
2020-05-14 16:56:15 +02:00
body('scope')
.custom(isBulkRemoveCommentsOfScopeValid),
2020-05-14 16:56:15 +02:00
async (req: express.Request, res: express.Response, next: express.NextFunction) => {
if (areValidationErrors(req, res)) return
if (!await doesAccountNameWithHostExist(req.body.accountName, res)) return
const user = res.locals.oauth.token.User
const body = req.body as BulkRemoveCommentsOfBody
if (body.scope === 'instance' && user.hasRight(UserRight.REMOVE_ANY_VIDEO_COMMENT) !== true) {
return res.fail({
status: HttpStatusCode.FORBIDDEN_403,
message: 'User cannot remove any comments of this instance.'
2020-05-14 16:56:15 +02:00
})
}
return next()
}
]
// ---------------------------------------------------------------------------
export {
bulkRemoveCommentsOfValidator
}
// ---------------------------------------------------------------------------