aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/helpers
diff options
context:
space:
mode:
Diffstat (limited to 'server/helpers')
-rw-r--r--server/helpers/custom-validators/accounts.ts28
1 files changed, 18 insertions, 10 deletions
diff --git a/server/helpers/custom-validators/accounts.ts b/server/helpers/custom-validators/accounts.ts
index cc8641d6b..00dea9039 100644
--- a/server/helpers/custom-validators/accounts.ts
+++ b/server/helpers/custom-validators/accounts.ts
@@ -4,16 +4,21 @@ import 'express-validator'
4import * as validator from 'validator' 4import * as validator from 'validator'
5import { AccountModel } from '../../models/account/account' 5import { AccountModel } from '../../models/account/account'
6import { isUserDescriptionValid, isUserUsernameValid } from './users' 6import { isUserDescriptionValid, isUserUsernameValid } from './users'
7import { exists } from './misc'
7 8
8function isAccountNameValid (value: string) { 9function isAccountNameValid (value: string) {
9 return isUserUsernameValid(value) 10 return isUserUsernameValid(value)
10} 11}
11 12
13function isAccountIdValid (value: string) {
14 return exists(value)
15}
16
12function isAccountDescriptionValid (value: string) { 17function isAccountDescriptionValid (value: string) {
13 return isUserDescriptionValid(value) 18 return isUserDescriptionValid(value)
14} 19}
15 20
16function isAccountIdExist (id: number | string, res: Response) { 21function isAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
17 let promise: Bluebird<AccountModel> 22 let promise: Bluebird<AccountModel>
18 23
19 if (validator.isInt('' + id)) { 24 if (validator.isInt('' + id)) {
@@ -22,32 +27,34 @@ function isAccountIdExist (id: number | string, res: Response) {
22 promise = AccountModel.loadByUUID('' + id) 27 promise = AccountModel.loadByUUID('' + id)
23 } 28 }
24 29
25 return isAccountExist(promise, res) 30 return isAccountExist(promise, res, sendNotFound)
26} 31}
27 32
28function isLocalAccountNameExist (name: string, res: Response) { 33function isLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
29 const promise = AccountModel.loadLocalByName(name) 34 const promise = AccountModel.loadLocalByName(name)
30 35
31 return isAccountExist(promise, res) 36 return isAccountExist(promise, res, sendNotFound)
32} 37}
33 38
34function isAccountNameWithHostExist (nameWithDomain: string, res: Response) { 39function isAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
35 const [ accountName, host ] = nameWithDomain.split('@') 40 const [ accountName, host ] = nameWithDomain.split('@')
36 41
37 let promise: Bluebird<AccountModel> 42 let promise: Bluebird<AccountModel>
38 if (!host) promise = AccountModel.loadLocalByName(accountName) 43 if (!host) promise = AccountModel.loadLocalByName(accountName)
39 else promise = AccountModel.loadLocalByNameAndHost(accountName, host) 44 else promise = AccountModel.loadLocalByNameAndHost(accountName, host)
40 45
41 return isAccountExist(promise, res) 46 return isAccountExist(promise, res, sendNotFound)
42} 47}
43 48
44async function isAccountExist (p: Bluebird<AccountModel>, res: Response) { 49async function isAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) {
45 const account = await p 50 const account = await p
46 51
47 if (!account) { 52 if (!account) {
48 res.status(404) 53 if (sendNotFound === true) {
49 .send({ error: 'Account not found' }) 54 res.status(404)
50 .end() 55 .send({ error: 'Account not found' })
56 .end()
57 }
51 58
52 return false 59 return false
53 } 60 }
@@ -60,6 +67,7 @@ async function isAccountExist (p: Bluebird<AccountModel>, res: Response) {
60// --------------------------------------------------------------------------- 67// ---------------------------------------------------------------------------
61 68
62export { 69export {
70 isAccountIdValid,
63 isAccountIdExist, 71 isAccountIdExist,
64 isLocalAccountNameExist, 72 isLocalAccountNameExist,
65 isAccountDescriptionValid, 73 isAccountDescriptionValid,