diff options
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 | } | ||