]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/middlewares/accounts.ts
Refactor middleware helpers
[github/Chocobozzz/PeerTube.git] / server / helpers / middlewares / accounts.ts
CommitLineData
3e753302
C
1import { Response } from 'express'
2import { AccountModel } from '../../models/account/account'
3import * as Bluebird from 'bluebird'
4
5function doesAccountIdExist (id: number, res: Response, sendNotFound = true) {
6 const promise = AccountModel.load(id)
7
8 return doesAccountExist(promise, res, sendNotFound)
9}
10
11function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
12 const promise = AccountModel.loadLocalByName(name)
13
14 return doesAccountExist(promise, res, sendNotFound)
15}
16
17function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
18 return doesAccountExist(AccountModel.loadByNameWithHost(nameWithDomain), res, sendNotFound)
19}
20
21async 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
41export {
42 doesAccountIdExist,
43 doesLocalAccountNameExist,
44 doesAccountNameWithHostExist,
45 doesAccountExist
46}