]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame_incremental - server/helpers/custom-validators/accounts.ts
Update iso639 translations for french and deutch
[github/Chocobozzz/PeerTube.git] / server / helpers / custom-validators / accounts.ts
... / ...
CommitLineData
1import * as Bluebird from 'bluebird'
2import { Response } from 'express'
3import 'express-validator'
4import * as validator from 'validator'
5import { AccountModel } from '../../models/account/account'
6import { isUserDescriptionValid, isUserUsernameValid } from './users'
7import { exists } from './misc'
8import { CONFIG } from '../../initializers'
9
10function isAccountNameValid (value: string) {
11 return isUserUsernameValid(value)
12}
13
14function isAccountIdValid (value: string) {
15 return exists(value)
16}
17
18function isAccountDescriptionValid (value: string) {
19 return isUserDescriptionValid(value)
20}
21
22function 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
34function isLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
35 const promise = AccountModel.loadLocalByName(name)
36
37 return isAccountExist(promise, res, sendNotFound)
38}
39
40function isAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
41 const [ accountName, host ] = nameWithDomain.split('@')
42
43 let promise: Bluebird<AccountModel>
44 if (!host || host === CONFIG.WEBSERVER.HOST) promise = AccountModel.loadLocalByName(accountName)
45 else promise = AccountModel.loadLocalByNameAndHost(accountName, host)
46
47 return isAccountExist(promise, res, sendNotFound)
48}
49
50async function isAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) {
51 const account = await p
52
53 if (!account) {
54 if (sendNotFound === true) {
55 res.status(404)
56 .send({ error: 'Account not found' })
57 .end()
58 }
59
60 return false
61 }
62
63 res.locals.account = account
64
65 return true
66}
67
68// ---------------------------------------------------------------------------
69
70export {
71 isAccountIdValid,
72 isAccountIdExist,
73 isLocalAccountNameExist,
74 isAccountDescriptionValid,
75 isAccountNameWithHostExist,
76 isAccountNameValid
77}