]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/video-accounts.ts
Handle follow/accept
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / video-accounts.ts
CommitLineData
38fa2065
C
1import * as Promise from 'bluebird'
2import * as validator from 'validator'
3import * as express from 'express'
4import 'express-validator'
5
6import { database as db } from '../../initializers'
7import { AccountInstance } from '../../models'
8import { logger } from '../logger'
9
10import { isUserUsernameValid } from './users'
7a7724e6 11import { isHostValid } from './pods'
38fa2065
C
12
13function isVideoAccountNameValid (value: string) {
14 return isUserUsernameValid(value)
15}
16
7a7724e6
C
17function isAccountNameWithHostValid (value: string) {
18 const [ name, host ] = value.split('@')
19
20 return isVideoAccountNameValid(name) && isHostValid(host)
21}
22
38fa2065
C
23function checkVideoAccountExists (id: string, res: express.Response, callback: () => void) {
24 let promise: Promise<AccountInstance>
25 if (validator.isInt(id)) {
26 promise = db.Account.load(+id)
27 } else { // UUID
28 promise = db.Account.loadByUUID(id)
29 }
30
31 promise.then(account => {
32 if (!account) {
33 return res.status(404)
34 .json({ error: 'Video account not found' })
35 .end()
36 }
37
38 res.locals.account = account
39 callback()
40 })
41 .catch(err => {
42 logger.error('Error in video account request validator.', err)
43 return res.sendStatus(500)
44 })
45}
46
47// ---------------------------------------------------------------------------
48
49export {
50 checkVideoAccountExists,
7a7724e6 51 isAccountNameWithHostValid,
38fa2065
C
52 isVideoAccountNameValid
53}