2017-11-27 07:44:51 -06:00
|
|
|
import * as Bluebird from 'bluebird'
|
2017-11-10 07:48:08 -06:00
|
|
|
import * as express from 'express'
|
|
|
|
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 { logger } from '../logger'
|
|
|
|
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 07:44:51 -06:00
|
|
|
function checkAccountIdExists (id: number | string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) {
|
|
|
|
let promise: Bluebird<AccountInstance>
|
|
|
|
|
|
|
|
if (validator.isInt('' + id)) {
|
2017-11-10 07:48:08 -06:00
|
|
|
promise = db.Account.load(+id)
|
|
|
|
} else { // UUID
|
2017-11-27 07:44:51 -06:00
|
|
|
promise = db.Account.loadByUUID('' + id)
|
2017-11-10 07:48:08 -06:00
|
|
|
}
|
|
|
|
|
2017-11-27 07:44:51 -06:00
|
|
|
return checkAccountExists(promise, res, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkLocalAccountNameExists (name: string, res: express.Response, callback: (err: Error, account: AccountInstance) => any) {
|
|
|
|
const p = db.Account.loadLocalByName(name)
|
|
|
|
|
|
|
|
return checkAccountExists(p, res, callback)
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkAccountExists (p: Bluebird<AccountInstance>, res: express.Response, callback: (err: Error, account: AccountInstance) => any) {
|
|
|
|
p.then(account => {
|
2017-11-10 07:48:08 -06:00
|
|
|
if (!account) {
|
|
|
|
return res.status(404)
|
2017-11-27 07:44:51 -06:00
|
|
|
.send({ error: 'Account not found' })
|
2017-11-10 07:48:08 -06:00
|
|
|
.end()
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.account = account
|
2017-11-27 07:44:51 -06:00
|
|
|
return callback(null, account)
|
2017-11-14 10:31:26 -06:00
|
|
|
})
|
2017-11-27 07:44:51 -06:00
|
|
|
.catch(err => {
|
|
|
|
logger.error('Error in account request validator.', err)
|
|
|
|
return res.sendStatus(500)
|
|
|
|
})
|
2017-11-10 07:48:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2017-11-27 07:44:51 -06:00
|
|
|
checkAccountIdExists,
|
|
|
|
checkLocalAccountNameExists,
|
2017-11-14 10:31:26 -06:00
|
|
|
isAccountNameValid
|
2017-11-10 07:48:08 -06:00
|
|
|
}
|