]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/account.ts
Fix lint
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / account.ts
CommitLineData
e4f97bab 1import * as express from 'express'
7a7724e6 2import { param } from 'express-validator/check'
79d5caf9 3import { logger } from '../../helpers'
350e31d6 4import { isAccountNameValid } from '../../helpers/custom-validators/accounts'
7a7724e6 5import { database as db } from '../../initializers/database'
e4f97bab 6import { AccountInstance } from '../../models'
7a7724e6 7import { checkErrors } from './utils'
e4f97bab
C
8
9const localAccountValidator = [
350e31d6 10 param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
e4f97bab
C
11
12 (req: express.Request, res: express.Response, next: express.NextFunction) => {
13 logger.debug('Checking localAccountValidator parameters', { parameters: req.params })
14
15 checkErrors(req, res, () => {
16 checkLocalAccountExists(req.params.name, res, next)
17 })
18 }
19]
20
21// ---------------------------------------------------------------------------
22
23export {
24 localAccountValidator
25}
26
27// ---------------------------------------------------------------------------
28
350e31d6
C
29function checkLocalAccountExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => void) {
30 db.Account.loadLocalByName(name)
e4f97bab
C
31 .then(account => {
32 if (!account) {
33 return res.status(404)
34 .send({ error: 'Account not found' })
35 .end()
36 }
37
38 res.locals.account = account
39 return callback(null, account)
40 })
41 .catch(err => {
42 logger.error('Error in account request validator.', err)
43 return res.sendStatus(500)
44 })
45}