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

71 lines
1.8 KiB
TypeScript
Raw Normal View History

import * as Bluebird from 'bluebird'
2017-11-27 16:30:46 +00:00
import { Response } from 'express'
2017-11-10 13:48:08 +00:00
import 'express-validator'
2017-11-23 17:04:48 +00:00
import * as validator from 'validator'
2017-12-12 16:53:50 +00:00
import { AccountModel } from '../../models/account/account'
import { isUserDescriptionValid, isUserUsernameValid } from './users'
import { exists } from './misc'
2017-11-10 13:48:08 +00:00
2017-11-14 16:31:26 +00:00
function isAccountNameValid (value: string) {
2017-11-10 13:48:08 +00:00
return isUserUsernameValid(value)
}
function isAccountIdValid (value: string) {
return exists(value)
}
function isAccountDescriptionValid (value: string) {
return isUserDescriptionValid(value)
}
2019-03-19 08:26:50 +00:00
function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
2017-12-12 16:53:50 +00:00
let promise: Bluebird<AccountModel>
if (validator.isInt('' + id)) {
2017-12-12 16:53:50 +00:00
promise = AccountModel.load(+id)
2017-11-10 13:48:08 +00:00
} else { // UUID
2017-12-12 16:53:50 +00:00
promise = AccountModel.loadByUUID('' + id)
2017-11-10 13:48:08 +00:00
}
2019-03-19 08:26:50 +00:00
return doesAccountExist(promise, res, sendNotFound)
}
2019-03-19 08:26:50 +00:00
function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
2017-12-12 16:53:50 +00:00
const promise = AccountModel.loadLocalByName(name)
2019-03-19 08:26:50 +00:00
return doesAccountExist(promise, res, sendNotFound)
}
2019-03-19 08:26:50 +00:00
function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
return doesAccountExist(AccountModel.loadByNameWithHost(nameWithDomain), res, sendNotFound)
2018-02-21 15:44:18 +00:00
}
2019-03-19 08:26:50 +00:00
async function doesAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) {
2017-11-27 16:30:46 +00:00
const account = await p
if (!account) {
if (sendNotFound === true) {
res.status(404)
.send({ error: 'Account not found' })
.end()
}
2017-11-27 16:30:46 +00:00
return false
}
res.locals.account = account
return true
2017-11-10 13:48:08 +00:00
}
// ---------------------------------------------------------------------------
export {
isAccountIdValid,
2019-03-19 08:26:50 +00:00
doesAccountIdExist,
doesLocalAccountNameExist,
isAccountDescriptionValid,
2019-03-19 08:26:50 +00:00
doesAccountNameWithHostExist,
2017-11-14 16:31:26 +00:00
isAccountNameValid
2017-11-10 13:48:08 +00:00
}