]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/middlewares/accounts.ts
Merge branch 'release/2.3.0' into develop
[github/Chocobozzz/PeerTube.git] / server / helpers / middlewares / accounts.ts
1 import { Response } from 'express'
2 import { AccountModel } from '../../models/account/account'
3 import * as Bluebird from 'bluebird'
4 import { MAccountDefault } from '../../types/models'
5
6 function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) {
7 const promise = AccountModel.load(parseInt(id + '', 10))
8
9 return doesAccountExist(promise, res, sendNotFound)
10 }
11
12 function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
13 const promise = AccountModel.loadLocalByName(name)
14
15 return doesAccountExist(promise, res, sendNotFound)
16 }
17
18 function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
19 const promise = AccountModel.loadByNameWithHost(nameWithDomain)
20
21 return doesAccountExist(promise, res, sendNotFound)
22 }
23
24 async function doesAccountExist (p: Bluebird<MAccountDefault>, res: Response, sendNotFound: boolean) {
25 const account = await p
26
27 if (!account) {
28 if (sendNotFound === true) {
29 res.status(404)
30 .send({ error: 'Account not found' })
31 .end()
32 }
33
34 return false
35 }
36
37 res.locals.account = account
38
39 return true
40 }
41
42 // ---------------------------------------------------------------------------
43
44 export {
45 doesAccountIdExist,
46 doesLocalAccountNameExist,
47 doesAccountNameWithHostExist,
48 doesAccountExist
49 }