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