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

69 lines
1.7 KiB
TypeScript
Raw Normal View History

import * as Bluebird from 'bluebird'
2017-11-27 17:30:46 +01:00
import { Response } from 'express'
2017-11-10 14:48:08 +01:00
import 'express-validator'
2017-11-23 18:04:48 +01:00
import * as validator from 'validator'
2017-12-12 17:53:50 +01:00
import { AccountModel } from '../../models/account/account'
import { isUserDescriptionValid, isUserUsernameValid } from './users'
2017-11-10 14:48:08 +01:00
2017-11-14 17:31:26 +01:00
function isAccountNameValid (value: string) {
2017-11-10 14:48:08 +01:00
return isUserUsernameValid(value)
}
function isAccountDescriptionValid (value: string) {
return isUserDescriptionValid(value)
}
2017-11-27 17:30:46 +01:00
function isAccountIdExist (id: number | string, res: Response) {
2017-12-12 17:53:50 +01:00
let promise: Bluebird<AccountModel>
if (validator.isInt('' + id)) {
2017-12-12 17:53:50 +01:00
promise = AccountModel.load(+id)
2017-11-10 14:48:08 +01:00
} else { // UUID
2017-12-12 17:53:50 +01:00
promise = AccountModel.loadByUUID('' + id)
2017-11-10 14:48:08 +01:00
}
2017-11-27 17:30:46 +01:00
return isAccountExist(promise, res)
}
2017-11-27 17:30:46 +01:00
function isLocalAccountNameExist (name: string, res: Response) {
2017-12-12 17:53:50 +01:00
const promise = AccountModel.loadLocalByName(name)
2017-11-27 17:30:46 +01:00
return isAccountExist(promise, res)
}
2018-02-21 16:44:18 +01:00
function isAccountNameWithHostExist (nameWithDomain: string, res: Response) {
const [ accountName, host ] = nameWithDomain.split('@')
let promise: Bluebird<AccountModel>
if (!host) promise = AccountModel.loadLocalByName(accountName)
else promise = AccountModel.loadLocalByNameAndHost(accountName, host)
return isAccountExist(promise, res)
}
2017-12-12 17:53:50 +01:00
async function isAccountExist (p: Bluebird<AccountModel>, res: Response) {
2017-11-27 17:30:46 +01:00
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 14:48:08 +01:00
}
// ---------------------------------------------------------------------------
export {
2017-11-27 17:30:46 +01:00
isAccountIdExist,
isLocalAccountNameExist,
isAccountDescriptionValid,
2018-02-21 16:44:18 +01:00
isAccountNameWithHostExist,
2017-11-14 17:31:26 +01:00
isAccountNameValid
2017-11-10 14:48:08 +01:00
}