]>
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' |
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 | ||
453e83ea | 25 | async function doesAccountExist (p: Bluebird<MAccountDefault>, res: Response, sendNotFound: boolean) { |
3e753302 C |
26 | const account = await p |
27 | ||
28 | if (!account) { | |
29 | if (sendNotFound === true) { | |
30 | res.status(404) | |
31 | .send({ error: 'Account not found' }) | |
32 | .end() | |
33 | } | |
34 | ||
35 | return false | |
36 | } | |
37 | ||
38 | res.locals.account = account | |
39 | ||
40 | return true | |
41 | } | |
42 | ||
afff310e RK |
43 | async function doesUserFeedTokenCorrespond (id: number | string, token: string, res: Response) { |
44 | const user = await UserModel.loadById(parseInt(id + '', 10)) | |
45 | ||
46 | if (token !== user.feedToken) { | |
47 | res.status(401) | |
48 | .send({ error: 'User and token mismatch' }) | |
49 | .end() | |
50 | ||
51 | return false | |
52 | } | |
53 | ||
54 | res.locals.user = user | |
55 | ||
56 | return true | |
57 | } | |
58 | ||
3e753302 C |
59 | // --------------------------------------------------------------------------- |
60 | ||
61 | export { | |
62 | doesAccountIdExist, | |
63 | doesLocalAccountNameExist, | |
64 | doesAccountNameWithHostExist, | |
afff310e RK |
65 | doesAccountExist, |
66 | doesUserFeedTokenCorrespond | |
3e753302 | 67 | } |