diff options
author | Chocobozzz <me@florianbigard.com> | 2019-07-23 10:40:39 +0200 |
---|---|---|
committer | Chocobozzz <chocobozzz@cpy.re> | 2019-07-24 10:58:16 +0200 |
commit | 3e753302d8c911b59971c16a8018df0e1ab78465 (patch) | |
tree | efce7ece3273589228c5c948ea6757b2bdf65429 /server/helpers/middlewares/accounts.ts | |
parent | a8b666e9f1ed002230869606308749614390c82f (diff) | |
download | PeerTube-3e753302d8c911b59971c16a8018df0e1ab78465.tar.gz PeerTube-3e753302d8c911b59971c16a8018df0e1ab78465.tar.zst PeerTube-3e753302d8c911b59971c16a8018df0e1ab78465.zip |
Refactor middleware helpers
Diffstat (limited to 'server/helpers/middlewares/accounts.ts')
-rw-r--r-- | server/helpers/middlewares/accounts.ts | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/server/helpers/middlewares/accounts.ts b/server/helpers/middlewares/accounts.ts new file mode 100644 index 000000000..791022b97 --- /dev/null +++ b/server/helpers/middlewares/accounts.ts | |||
@@ -0,0 +1,46 @@ | |||
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 | } | ||