Misc cleanup
[oweals/peertube.git] / server / helpers / custom-validators / accounts.ts
1 import * as Promise from 'bluebird'
2 import * as express from 'express'
3 import 'express-validator'
4 import * as validator from 'validator'
5 import { database as db } from '../../initializers'
6 import { AccountInstance } from '../../models'
7 import { logger } from '../logger'
8 import { isUserUsernameValid } from './users'
9
10 function isAccountNameValid (value: string) {
11   return isUserUsernameValid(value)
12 }
13
14 function checkVideoAccountExists (id: string, res: express.Response, callback: () => void) {
15   let promise: Promise<AccountInstance>
16   if (validator.isInt(id)) {
17     promise = db.Account.load(+id)
18   } else { // UUID
19     promise = db.Account.loadByUUID(id)
20   }
21
22   promise.then(account => {
23     if (!account) {
24       return res.status(404)
25         .json({ error: 'Video account not found' })
26         .end()
27     }
28
29     res.locals.account = account
30     callback()
31   })
32   .catch(err => {
33     logger.error('Error in video account request validator.', err)
34     return res.sendStatus(500)
35   })
36 }
37
38 // ---------------------------------------------------------------------------
39
40 export {
41   checkVideoAccountExists,
42   isAccountNameValid
43 }