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