]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/account.ts
Begin activitypub
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / account.ts
CommitLineData
e4f97bab
C
1import { param } from 'express-validator/check'
2import * as express from 'express'
3
4import { database as db } from '../../initializers/database'
5import { checkErrors } from './utils'
6import {
7 logger,
8 isUserUsernameValid,
9 isUserPasswordValid,
10 isUserVideoQuotaValid,
11 isUserDisplayNSFWValid,
12 isUserRoleValid,
13 isAccountNameValid
14} from '../../helpers'
15import { AccountInstance } from '../../models'
16
17const 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
31export {
32 localAccountValidator
33}
34
35// ---------------------------------------------------------------------------
36
37function 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}