Begin activitypub
[oweals/peertube.git] / server / middlewares / validators / account.ts
1 import { param } from 'express-validator/check'
2 import * as express from 'express'
3
4 import { database as db } from '../../initializers/database'
5 import { checkErrors } from './utils'
6 import {
7   logger,
8   isUserUsernameValid,
9   isUserPasswordValid,
10   isUserVideoQuotaValid,
11   isUserDisplayNSFWValid,
12   isUserRoleValid,
13   isAccountNameValid
14 } from '../../helpers'
15 import { AccountInstance } from '../../models'
16
17 const localAccountValidator = [
18   param('name').custom(isAccountNameValid).withMessage('Should have a valid account name'),
19
20   (req: express.Request, res: express.Response, next: express.NextFunction) => {
21     logger.debug('Checking localAccountValidator parameters', { parameters: req.params })
22
23     checkErrors(req, res, () => {
24       checkLocalAccountExists(req.params.name, res, next)
25     })
26   }
27 ]
28
29 // ---------------------------------------------------------------------------
30
31 export {
32   localAccountValidator
33 }
34
35 // ---------------------------------------------------------------------------
36
37 function checkLocalAccountExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => void) {
38   db.Account.loadLocalAccountByName(name)
39     .then(account => {
40       if (!account) {
41         return res.status(404)
42           .send({ error: 'Account not found' })
43           .end()
44       }
45
46       res.locals.account = account
47       return callback(null, account)
48     })
49     .catch(err => {
50       logger.error('Error in account request validator.', err)
51       return res.sendStatus(500)
52     })
53 }