]>
Commit | Line | Data |
---|---|---|
7ad9b984 C |
1 | import { param, body } from 'express-validator/check' |
2 | import * as express from 'express' | |
3 | import { logger } from '../../helpers/logger' | |
4 | import { areValidationErrors } from './utils' | |
5 | import { isAccountNameWithHostExist } from '../../helpers/custom-validators/accounts' | |
6 | import { UserModel } from '../../models/account/user' | |
7 | import { AccountBlocklistModel } from '../../models/account/account-blocklist' | |
8 | import { isHostValid } from '../../helpers/custom-validators/servers' | |
9 | import { ServerBlocklistModel } from '../../models/server/server-blocklist' | |
10 | ||
11 | const blockAccountByAccountValidator = [ | |
12 | body('accountName').exists().withMessage('Should have an account name with host'), | |
13 | ||
14 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
15 | logger.debug('Checking blockAccountByAccountValidator parameters', { parameters: req.body }) | |
16 | ||
17 | if (areValidationErrors(req, res)) return | |
18 | if (!await isAccountNameWithHostExist(req.body.accountName, res)) return | |
19 | ||
20 | return next() | |
21 | } | |
22 | ] | |
23 | ||
24 | const unblockAccountByAccountValidator = [ | |
25 | param('accountName').exists().withMessage('Should have an account name with host'), | |
26 | ||
27 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
28 | logger.debug('Checking unblockAccountByAccountValidator parameters', { parameters: req.params }) | |
29 | ||
30 | if (areValidationErrors(req, res)) return | |
31 | if (!await isAccountNameWithHostExist(req.params.accountName, res)) return | |
32 | ||
33 | const user = res.locals.oauth.token.User as UserModel | |
34 | const targetAccount = res.locals.account | |
35 | if (!await isUnblockAccountExists(user.Account.id, targetAccount.id, res)) return | |
36 | ||
37 | return next() | |
38 | } | |
39 | ] | |
40 | ||
41 | const unblockServerByAccountValidator = [ | |
42 | param('host').custom(isHostValid).withMessage('Should have an account name with host'), | |
43 | ||
44 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
45 | logger.debug('Checking unblockServerByAccountValidator parameters', { parameters: req.params }) | |
46 | ||
47 | if (areValidationErrors(req, res)) return | |
48 | ||
49 | const user = res.locals.oauth.token.User as UserModel | |
50 | if (!await isUnblockServerExists(user.Account.id, req.params.host, res)) return | |
51 | ||
52 | return next() | |
53 | } | |
54 | ] | |
55 | ||
56 | // --------------------------------------------------------------------------- | |
57 | ||
58 | export { | |
59 | blockAccountByAccountValidator, | |
60 | unblockAccountByAccountValidator, | |
61 | unblockServerByAccountValidator | |
62 | } | |
63 | ||
64 | // --------------------------------------------------------------------------- | |
65 | ||
66 | async function isUnblockAccountExists (accountId: number, targetAccountId: number, res: express.Response) { | |
67 | const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(accountId, targetAccountId) | |
68 | if (!accountBlock) { | |
69 | res.status(404) | |
70 | .send({ error: 'Account block entry not found.' }) | |
71 | .end() | |
72 | ||
73 | return false | |
74 | } | |
75 | ||
76 | res.locals.accountBlock = accountBlock | |
77 | ||
78 | return true | |
79 | } | |
80 | ||
81 | async function isUnblockServerExists (accountId: number, host: string, res: express.Response) { | |
82 | const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(accountId, host) | |
83 | if (!serverBlock) { | |
84 | res.status(404) | |
85 | .send({ error: 'Server block entry not found.' }) | |
86 | .end() | |
87 | ||
88 | return false | |
89 | } | |
90 | ||
91 | res.locals.serverBlock = serverBlock | |
92 | ||
93 | return true | |
94 | } |