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