]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/helpers/middlewares/accounts.ts
Bumped to version v1.4.0
[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
5 function doesAccountIdExist (id: number, res: Response, sendNotFound = true) {
6 const promise = AccountModel.load(id)
7
8 return doesAccountExist(promise, res, sendNotFound)
9 }
10
11 function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
12 const promise = AccountModel.loadLocalByName(name)
13
14 return doesAccountExist(promise, res, sendNotFound)
15 }
16
17 function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
18 return doesAccountExist(AccountModel.loadByNameWithHost(nameWithDomain), res, sendNotFound)
19 }
20
21 async function doesAccountExist (p: Bluebird<AccountModel>, res: Response, sendNotFound: boolean) {
22 const account = await p
23
24 if (!account) {
25 if (sendNotFound === true) {
26 res.status(404)
27 .send({ error: 'Account not found' })
28 .end()
29 }
30
31 return false
32 }
33
34 res.locals.account = account
35
36 return true
37 }
38
39 // ---------------------------------------------------------------------------
40
41 export {
42 doesAccountIdExist,
43 doesLocalAccountNameExist,
44 doesAccountNameWithHostExist,
45 doesAccountExist
46 }