2017-11-27 08:44:51 -05:00
|
|
|
import * as Bluebird from 'bluebird'
|
2017-11-27 11:30:46 -05:00
|
|
|
import { Response } from 'express'
|
2017-11-10 08:48:08 -05:00
|
|
|
import 'express-validator'
|
2017-11-23 12:04:48 -05:00
|
|
|
import * as validator from 'validator'
|
2017-12-12 11:53:50 -05:00
|
|
|
import { AccountModel } from '../../models/account/account'
|
2018-02-15 08:46:26 -05:00
|
|
|
import { isUserDescriptionValid, isUserUsernameValid } from './users'
|
2018-05-24 09:30:28 -04:00
|
|
|
import { exists } from './misc'
|
2018-05-25 03:57:16 -04:00
|
|
|
import { CONFIG } from '../../initializers'
|
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)
|
|
|
|
}
|
|
|
|
|
2018-05-24 09:30:28 -04:00
|
|
|
function isAccountIdValid (value: string) {
|
|
|
|
return exists(value)
|
|
|
|
}
|
|
|
|
|
2018-02-15 08:46:26 -05:00
|
|
|
function isAccountDescriptionValid (value: string) {
|
|
|
|
return isUserDescriptionValid(value)
|
|
|
|
}
|
|
|
|
|
2018-05-24 09:30:28 -04:00
|
|
|
function isAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
|
2017-12-12 11:53:50 -05:00
|
|
|
let promise: Bluebird<AccountModel>
|
2017-11-27 08:44:51 -05:00
|
|
|
|
|
|
|
if (validator.isInt('' + id)) {
|
2017-12-12 11:53:50 -05:00
|
|
|
promise = AccountModel.load(+id)
|
2017-11-10 08:48:08 -05:00
|
|
|
} else { // UUID
|
2017-12-12 11:53:50 -05:00
|
|
|
promise = AccountModel.loadByUUID('' + id)
|
2017-11-10 08:48:08 -05:00
|
|
|
}
|
|
|
|
|
2018-05-24 09:30:28 -04:00
|
|
|
return isAccountExist(promise, res, sendNotFound)
|
2017-11-27 08:44:51 -05:00
|
|
|
}
|
|
|
|
|
2018-05-24 09:30:28 -04:00
|
|
|
function isLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
|
2017-12-12 11:53:50 -05:00
|
|
|
const promise = AccountModel.loadLocalByName(name)
|
2017-11-27 08:44:51 -05:00
|
|
|
|
2018-05-24 09:30:28 -04:00
|
|
|
return isAccountExist(promise, res, sendNotFound)
|
2017-11-27 08:44:51 -05:00
|
|
|
}
|
|
|
|
|
2018-05-24 09:30:28 -04:00
|
|
|
function isAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
|
2018-02-21 10:44:18 -05:00
|
|
|
const [ accountName, host ] = nameWithDomain.split('@')
|
|
|
|
|
|
|
|
let promise: Bluebird<AccountModel>
|
2018-05-25 03:57:16 -04:00
|
|
|
if (!host || host === CONFIG.WEBSERVER.HOST) promise = AccountModel.loadLocalByName(accountName)
|
2018-08-17 09:45:42 -04:00
|
|
|
else promise = AccountModel.loadByNameAndHost(accountName, host)
|
2018-02-21 10:44:18 -05:00
|
|
|
|
2018-05-24 09:30:28 -04:00
|
|
|
return isAccountExist(promise, res, sendNotFound)
|
2018-02-21 10:44:18 -05:00
|
|
|
}
|
|
|
|
|
2018-05-24 09:30:28 -04:00
|
|
|
async function isAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) {
|
2017-11-27 11:30:46 -05:00
|
|
|
const account = await p
|
|
|
|
|
|
|
|
if (!account) {
|
2018-05-24 09:30:28 -04:00
|
|
|
if (sendNotFound === true) {
|
|
|
|
res.status(404)
|
|
|
|
.send({ error: 'Account not found' })
|
|
|
|
.end()
|
|
|
|
}
|
2017-11-27 11:30:46 -05:00
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
res.locals.account = account
|
|
|
|
|
|
|
|
return true
|
2017-11-10 08:48:08 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2018-05-24 09:30:28 -04:00
|
|
|
isAccountIdValid,
|
2017-11-27 11:30:46 -05:00
|
|
|
isAccountIdExist,
|
|
|
|
isLocalAccountNameExist,
|
2018-02-15 08:46:26 -05:00
|
|
|
isAccountDescriptionValid,
|
2018-02-21 10:44:18 -05:00
|
|
|
isAccountNameWithHostExist,
|
2017-11-14 11:31:26 -05:00
|
|
|
isAccountNameValid
|
2017-11-10 08:48:08 -05:00
|
|
|
}
|