]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/helpers/middlewares/accounts.ts
Stronger model typings
[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'
453e83ea 4import { MAccountDefault } from '../../typings/models'
3e753302
C
5
6function doesAccountIdExist (id: number, res: Response, sendNotFound = true) {
7 const promise = AccountModel.load(id)
8
9 return doesAccountExist(promise, res, sendNotFound)
10}
11
12function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) {
13 const promise = AccountModel.loadLocalByName(name)
14
15 return doesAccountExist(promise, res, sendNotFound)
16}
17
18function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) {
453e83ea
C
19 const promise = AccountModel.loadByNameWithHost(nameWithDomain)
20
21 return doesAccountExist(promise, res, sendNotFound)
3e753302
C
22}
23
453e83ea 24async function doesAccountExist (p: Bluebird<MAccountDefault>, res: Response, sendNotFound: boolean) {
3e753302
C
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
44export {
45 doesAccountIdExist,
46 doesLocalAccountNameExist,
47 doesAccountNameWithHostExist,
48 doesAccountExist
49}