]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/accounts.ts
Handle account name in client url
[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
d14a9532 21function isAccountIdExist (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
d14a9532 30 return isAccountExist(promise, res, sendNotFound)
4e50b6a1
C
31}
32
d14a9532 33function isLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
3fd3ab2d 34 const promise = AccountModel.loadLocalByName(name)
4e50b6a1 35
d14a9532 36 return isAccountExist(promise, res, sendNotFound)
4e50b6a1
C
37}
38
d14a9532 39function isAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
e8cb4409
C
40 const [ accountName, host ] = nameWithDomain.split('@')
41
42 let promise: Bluebird<AccountModel>
43 if (!host) promise = AccountModel.loadLocalByName(accountName)
44 else promise = AccountModel.loadLocalByNameAndHost(accountName, host)
45
d14a9532 46 return isAccountExist(promise, res, sendNotFound)
e8cb4409
C
47}
48
d14a9532 49async function isAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) {
a2431b7d
C
50 const account = await p
51
52 if (!account) {
d14a9532
C
53 if (sendNotFound === true) {
54 res.status(404)
55 .send({ error: 'Account not found' })
56 .end()
57 }
a2431b7d
C
58
59 return false
60 }
61
62 res.locals.account = account
63
64 return true
38fa2065
C
65}
66
67// ---------------------------------------------------------------------------
68
69export {
d14a9532 70 isAccountIdValid,
a2431b7d
C
71 isAccountIdExist,
72 isLocalAccountNameExist,
2422c46b 73 isAccountDescriptionValid,
e8cb4409 74 isAccountNameWithHostExist,
350e31d6 75 isAccountNameValid
38fa2065 76}