PeerTube/server/helpers/custom-validators/accounts.ts

54 lines
1.2 KiB
TypeScript
Raw Normal View History

import * as Bluebird from 'bluebird'
2017-11-27 10:30:46 -06:00
import { Response } from 'express'
2017-11-10 07:48:08 -06:00
import 'express-validator'
2017-11-23 11:04:48 -06:00
import * as validator from 'validator'
2017-11-10 07:48:08 -06:00
import { database as db } from '../../initializers'
import { AccountInstance } from '../../models'
import { isUserUsernameValid } from './users'
2017-11-14 10:31:26 -06:00
function isAccountNameValid (value: string) {
2017-11-10 07:48:08 -06:00
return isUserUsernameValid(value)
}
2017-11-27 10:30:46 -06:00
function isAccountIdExist (id: number | string, res: Response) {
let promise: Bluebird<AccountInstance>
if (validator.isInt('' + id)) {
2017-11-10 07:48:08 -06:00
promise = db.Account.load(+id)
} else { // UUID
promise = db.Account.loadByUUID('' + id)
2017-11-10 07:48:08 -06:00
}
2017-11-27 10:30:46 -06:00
return isAccountExist(promise, res)
}
2017-11-27 10:30:46 -06:00
function isLocalAccountNameExist (name: string, res: Response) {
const promise = db.Account.loadLocalByName(name)
2017-11-27 10:30:46 -06:00
return isAccountExist(promise, res)
}
2017-11-27 10:30:46 -06:00
async function isAccountExist (p: Bluebird<AccountInstance>, res: Response) {
const account = await p
if (!account) {
res.status(404)
.send({ error: 'Account not found' })
.end()
return false
}
res.locals.account = account
return true
2017-11-10 07:48:08 -06:00
}
// ---------------------------------------------------------------------------
export {
2017-11-27 10:30:46 -06:00
isAccountIdExist,
isLocalAccountNameExist,
2017-11-14 10:31:26 -06:00
isAccountNameValid
2017-11-10 07:48:08 -06:00
}