aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/bulk.ts
blob: a1cea8032e3dcbc68c824e15d92123029a4d9f7f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import express from 'express'
import { body } from 'express-validator'
import { isBulkRemoveCommentsOfScopeValid } from '@server/helpers/custom-validators/bulk'
import { HttpStatusCode, UserRight } from '@shared/models'
import { BulkRemoveCommentsOfBody } from '@shared/models/bulk/bulk-remove-comments-of-body.model'
import { areValidationErrors, doesAccountNameWithHostExist } from './shared'

const bulkRemoveCommentsOfValidator = [
  body('accountName')
    .exists(),
  body('scope')
    .custom(isBulkRemoveCommentsOfScopeValid),

  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.'
      })
    }

    return next()
  }
]

// ---------------------------------------------------------------------------

export {
  bulkRemoveCommentsOfValidator
}

// ---------------------------------------------------------------------------