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 isAccountIdExist (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 isAccountExist(promise, res, sendNotFound)
33 function isLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
34 const promise = AccountModel.loadLocalByName(name)
36 return isAccountExist(promise, res, sendNotFound)
39 function isAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
40 const [ accountName, host ] = nameWithDomain.split('@')
42 let promise: Bluebird<AccountModel>
43 if (!host) promise = AccountModel.loadLocalByName(accountName)
44 else promise = AccountModel.loadLocalByNameAndHost(accountName, host)
46 return isAccountExist(promise, res, sendNotFound)
49 async function isAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) {
50 const account = await p
53 if (sendNotFound === true) {
55 .send({ error: 'Account not found' })
62 res.locals.account = account
67 // ---------------------------------------------------------------------------
72 isLocalAccountNameExist,
73 isAccountDescriptionValid,
74 isAccountNameWithHostExist,