]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/accounts.ts
Move concurrently in dev package
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / accounts.ts
CommitLineData
4e50b6a1 1import * as Bluebird from 'bluebird'
a2431b7d 2import { Response } from 'express'
38fa2065 3import 'express-validator'
d4f1e94c 4import * as validator from 'validator'
3fd3ab2d 5import { AccountModel } from '../../models/account/account'
2422c46b 6import { isUserDescriptionValid, isUserUsernameValid } from './users'
d14a9532 7import { exists } from './misc'
38fa2065 8
350e31d6 9function isAccountNameValid (value: string) {
38fa2065
C
10 return isUserUsernameValid(value)
11}
12
d14a9532
C
13function isAccountIdValid (value: string) {
14 return exists(value)
15}
16
2422c46b
C
17function isAccountDescriptionValid (value: string) {
18 return isUserDescriptionValid(value)
19}
20
0f6acda1 21function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
3fd3ab2d 22 let promise: Bluebird<AccountModel>
4e50b6a1
C
23
24 if (validator.isInt('' + id)) {
3fd3ab2d 25 promise = AccountModel.load(+id)
38fa2065 26 } else { // UUID
3fd3ab2d 27 promise = AccountModel.loadByUUID('' + id)
38fa2065
C
28 }
29
0f6acda1 30 return doesAccountExist(promise, res, sendNotFound)
4e50b6a1
C
31}
32
0f6acda1 33function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
3fd3ab2d 34 const promise = AccountModel.loadLocalByName(name)
4e50b6a1 35
0f6acda1 36 return doesAccountExist(promise, res, sendNotFound)
4e50b6a1
C
37}
38
0f6acda1
C
39function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
40 return doesAccountExist(AccountModel.loadByNameWithHost(nameWithDomain), res, sendNotFound)
e8cb4409
C
41}
42
0f6acda1 43async function doesAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) {
a2431b7d
C
44 const account = await p
45
46 if (!account) {
d14a9532
C
47 if (sendNotFound === true) {
48 res.status(404)
49 .send({ error: 'Account not found' })
50 .end()
51 }
a2431b7d
C
52
53 return false
54 }
55
56 res.locals.account = account
57
58 return true
38fa2065
C
59}
60
61// ---------------------------------------------------------------------------
62
63export {
d14a9532 64 isAccountIdValid,
0f6acda1
C
65 doesAccountIdExist,
66 doesLocalAccountNameExist,
2422c46b 67 isAccountDescriptionValid,
0f6acda1 68 doesAccountNameWithHostExist,
350e31d6 69 isAccountNameValid
38fa2065 70}