]>
Commit | Line | Data |
---|---|---|
1 | import { Response } from 'express' | |
2 | import { AccountModel } from '../../models/account/account' | |
3 | import * as Bluebird from 'bluebird' | |
4 | import { MAccountDefault } from '../../types/models' | |
5 | import { UserModel } from '@server/models/account/user' | |
6 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | |
7 | ||
8 | function doesAccountIdExist (id: number | string, res: Response, sendNotFound = true) { | |
9 | const promise = AccountModel.load(parseInt(id + '', 10)) | |
10 | ||
11 | return doesAccountExist(promise, res, sendNotFound) | |
12 | } | |
13 | ||
14 | function doesLocalAccountNameExist (name: string, res: Response, sendNotFound = true) { | |
15 | const promise = AccountModel.loadLocalByName(name) | |
16 | ||
17 | return doesAccountExist(promise, res, sendNotFound) | |
18 | } | |
19 | ||
20 | function doesAccountNameWithHostExist (nameWithDomain: string, res: Response, sendNotFound = true) { | |
21 | const promise = AccountModel.loadByNameWithHost(nameWithDomain) | |
22 | ||
23 | return doesAccountExist(promise, res, sendNotFound) | |
24 | } | |
25 | ||
26 | async function doesAccountExist (p: Bluebird<MAccountDefault>, res: Response, sendNotFound: boolean) { | |
27 | const account = await p | |
28 | ||
29 | if (!account) { | |
30 | if (sendNotFound === true) { | |
31 | res.status(HttpStatusCode.NOT_FOUND_404) | |
32 | .json({ error: 'Account not found' }) | |
33 | } | |
34 | ||
35 | return false | |
36 | } | |
37 | ||
38 | res.locals.account = account | |
39 | ||
40 | return true | |
41 | } | |
42 | ||
43 | async function doesUserFeedTokenCorrespond (id: number, token: string, res: Response) { | |
44 | const user = await UserModel.loadByIdWithChannels(parseInt(id + '', 10)) | |
45 | ||
46 | if (token !== user.feedToken) { | |
47 | res.status(HttpStatusCode.FORBIDDEN_403) | |
48 | .json({ error: 'User and token mismatch' }) | |
49 | ||
50 | return false | |
51 | } | |
52 | ||
53 | res.locals.user = user | |
54 | ||
55 | return true | |
56 | } | |
57 | ||
58 | // --------------------------------------------------------------------------- | |
59 | ||
60 | export { | |
61 | doesAccountIdExist, | |
62 | doesLocalAccountNameExist, | |
63 | doesAccountNameWithHostExist, | |
64 | doesAccountExist, | |
65 | doesUserFeedTokenCorrespond | |
66 | } |