1
0
Fork 0
peertube/server/helpers/custom-validators/accounts.ts

54 lines
1.3 KiB
TypeScript
Raw Normal View History

2017-11-10 08:48:08 -05:00
import * as Promise from 'bluebird'
import * as validator from 'validator'
import * as express from 'express'
import 'express-validator'
import { database as db } from '../../initializers'
import { AccountInstance } from '../../models'
import { logger } from '../logger'
import { isUserUsernameValid } from './users'
2017-11-15 05:00:25 -05:00
import { isHostValid } from './servers'
2017-11-10 08:48:08 -05:00
2017-11-14 11:31:26 -05:00
function isAccountNameValid (value: string) {
2017-11-10 08:48:08 -05:00
return isUserUsernameValid(value)
}
2017-11-13 11:39:41 -05:00
function isAccountNameWithHostValid (value: string) {
const [ name, host ] = value.split('@')
2017-11-14 11:31:26 -05:00
return isAccountNameValid(name) && isHostValid(host)
2017-11-13 11:39:41 -05:00
}
2017-11-10 08:48:08 -05:00
function checkVideoAccountExists (id: string, res: express.Response, callback: () => void) {
let promise: Promise<AccountInstance>
if (validator.isInt(id)) {
promise = db.Account.load(+id)
} else { // UUID
promise = db.Account.loadByUUID(id)
}
promise.then(account => {
if (!account) {
return res.status(404)
.json({ error: 'Video account not found' })
.end()
}
res.locals.account = account
callback()
})
2017-11-14 11:31:26 -05:00
.catch(err => {
logger.error('Error in video account request validator.', err)
return res.sendStatus(500)
})
2017-11-10 08:48:08 -05:00
}
// ---------------------------------------------------------------------------
export {
checkVideoAccountExists,
2017-11-13 11:39:41 -05:00
isAccountNameWithHostValid,
2017-11-14 11:31:26 -05:00
isAccountNameValid
2017-11-10 08:48:08 -05:00
}