aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/actor.ts
blob: 99b529dd60200108ce9c1ff996cd698b1b4ca726 (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import * as express from 'express'
import { param } from 'express-validator'
import { isActorNameValid } from '../../helpers/custom-validators/actor'
import { logger } from '../../helpers/logger'
import { areValidationErrors } from './utils'
import {
  doesAccountNameWithHostExist,
  doesLocalAccountNameExist,
  doesVideoChannelNameWithHostExist,
  doesLocalVideoChannelNameExist
} from '../../helpers/middlewares'
import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'

const localActorValidator = [
  param('actorName').custom(isActorNameValid).withMessage('Should have a valid actor name'),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    logger.debug('Checking localActorValidator parameters', { parameters: req.params })

    if (areValidationErrors(req, res)) return

    const isAccount = await doesLocalAccountNameExist(req.params.actorName, res, false)
    const isVideoChannel = await doesLocalVideoChannelNameExist(req.params.actorName, res, false)

    if (!isAccount || !isVideoChannel) {
      res.status(HttpStatusCode.NOT_FOUND_404)
         .json({ error: 'Actor not found' })
    }

    return next()
  }
]

const actorNameWithHostGetValidator = [
  param('actorName').exists().withMessage('Should have an actor name with host'),

  async (req: express.Request, res: express.Response, next: express.NextFunction) => {
    logger.debug('Checking actorNameWithHostGetValidator parameters', { parameters: req.params })

    if (areValidationErrors(req, res)) return

    const isAccount = await doesAccountNameWithHostExist(req.params.actorName, res, false)
    const isVideoChannel = await doesVideoChannelNameWithHostExist(req.params.actorName, res, false)

    if (!isAccount && !isVideoChannel) {
      res.status(HttpStatusCode.NOT_FOUND_404)
         .json({ error: 'Actor not found' })
    }

    return next()
  }
]

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

export {
  localActorValidator,
  actorNameWithHostGetValidator
}