1 import * as Bluebird from 'bluebird'
2 import { Response } from 'express'
3 import 'express-validator'
4 import * as validator from 'validator'
5 import { AccountModel } from '../../models/account/account'
6 import { isUserDescriptionValid, isUserUsernameValid } from './users'
7 import { exists } from './misc'
9 function isAccountNameValid (value: string) {
10 return isUserUsernameValid(value)
13 function isAccountIdValid (value: string) {
17 function isAccountDescriptionValid (value: string) {
18 return isUserDescriptionValid(value)
21 function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
22 let promise: Bluebird<AccountModel>
24 if (validator.isInt('' + id)) {
25 promise = AccountModel.load(+id)
27 promise = AccountModel.loadByUUID('' + id)
30 return doesAccountExist(promise, res, sendNotFound)
33 function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
34 const promise = AccountModel.loadLocalByName(name)
36 return doesAccountExist(promise, res, sendNotFound)
39 function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
40 return doesAccountExist(AccountModel.loadByNameWithHost(nameWithDomain), res, sendNotFound)
43 async function doesAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) {
44 const account = await p
47 if (sendNotFound === true) {
49 .send({ error: 'Account not found' })
56 res.locals.account = account
61 // ---------------------------------------------------------------------------
66 doesLocalAccountNameExist,
67 isAccountDescriptionValid,
68 doesAccountNameWithHostExist,