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