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'
8 import { CONFIG } from '../../initializers'
10 function isAccountNameValid (value: string) {
11 return isUserUsernameValid(value)
14 function isAccountIdValid (value: string) {
18 function isAccountDescriptionValid (value: string) {
19 return isUserDescriptionValid(value)
22 function isAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
23 let promise: Bluebird<AccountModel>
25 if (validator.isInt('' + id)) {
26 promise = AccountModel.load(+id)
28 promise = AccountModel.loadByUUID('' + id)
31 return isAccountExist(promise, res, sendNotFound)
34 function isLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
35 const promise = AccountModel.loadLocalByName(name)
37 return isAccountExist(promise, res, sendNotFound)
40 function isAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
41 const [ accountName, host ] = nameWithDomain.split('@')
43 let promise: Bluebird<AccountModel>
44 if (!host || host === CONFIG.WEBSERVER.HOST) promise = AccountModel.loadLocalByName(accountName)
45 else promise = AccountModel.loadLocalByNameAndHost(accountName, host)
47 return isAccountExist(promise, res, sendNotFound)
50 async function isAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) {
51 const account = await p
54 if (sendNotFound === true) {
56 .send({ error: 'Account not found' })
63 res.locals.account = account
68 // ---------------------------------------------------------------------------
73 isLocalAccountNameExist,
74 isAccountDescriptionValid,
75 isAccountNameWithHostExist,