]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/custom-validators/accounts.ts
Improve channel and account SEO
[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'
ad9e39fb 8import { CONFIG } from '../../initializers'
38fa2065 9
350e31d6 10function isAccountNameValid (value: string) {
38fa2065
C
11 return isUserUsernameValid(value)
12}
13
d14a9532
C
14function isAccountIdValid (value: string) {
15 return exists(value)
16}
17
2422c46b
C
18function isAccountDescriptionValid (value: string) {
19 return isUserDescriptionValid(value)
20}
21
d14a9532 22function isAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
3fd3ab2d 23 let promise: Bluebird<AccountModel>
4e50b6a1
C
24
25 if (validator.isInt('' + id)) {
3fd3ab2d 26 promise = AccountModel.load(+id)
38fa2065 27 } else { // UUID
3fd3ab2d 28 promise = AccountModel.loadByUUID('' + id)
38fa2065
C
29 }
30
d14a9532 31 return isAccountExist(promise, res, sendNotFound)
4e50b6a1
C
32}
33
d14a9532 34function isLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
3fd3ab2d 35 const promise = AccountModel.loadLocalByName(name)
4e50b6a1 36
d14a9532 37 return isAccountExist(promise, res, sendNotFound)
4e50b6a1
C
38}
39
d14a9532 40function isAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
92bf2f62 41 return isAccountExist(AccountModel.loadByNameWithHost(nameWithDomain), res, sendNotFound)
e8cb4409
C
42}
43
d14a9532 44async function isAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) {
a2431b7d
C
45 const account = await p
46
47 if (!account) {
d14a9532
C
48 if (sendNotFound === true) {
49 res.status(404)
50 .send({ error: 'Account not found' })
51 .end()
52 }
a2431b7d
C
53
54 return false
55 }
56
57 res.locals.account = account
58
59 return true
38fa2065
C
60}
61
62// ---------------------------------------------------------------------------
63
64export {
d14a9532 65 isAccountIdValid,
a2431b7d
C
66 isAccountIdExist,
67 isLocalAccountNameExist,
2422c46b 68 isAccountDescriptionValid,
e8cb4409 69 isAccountNameWithHostExist,
350e31d6 70 isAccountNameValid
38fa2065 71}