]>
Commit | Line | Data |
---|---|---|
1 | import { body, param } from 'express-validator' | |
2 | import * as express from 'express' | |
3 | import { logger } from '../../helpers/logger' | |
4 | import { areValidationErrors } from './utils' | |
5 | import { AccountBlocklistModel } from '../../models/account/account-blocklist' | |
6 | import { isHostValid } from '../../helpers/custom-validators/servers' | |
7 | import { ServerBlocklistModel } from '../../models/server/server-blocklist' | |
8 | import { ServerModel } from '../../models/server/server' | |
9 | import { WEBSERVER } from '../../initializers/constants' | |
10 | import { doesAccountNameWithHostExist } from '../../helpers/middlewares' | |
11 | import { getServerActor } from '@server/models/application/application' | |
12 | ||
13 | const blockAccountValidator = [ | |
14 | body('accountName').exists().withMessage('Should have an account name with host'), | |
15 | ||
16 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
17 | logger.debug('Checking blockAccountByAccountValidator parameters', { parameters: req.body }) | |
18 | ||
19 | if (areValidationErrors(req, res)) return | |
20 | if (!await doesAccountNameWithHostExist(req.body.accountName, res)) return | |
21 | ||
22 | const user = res.locals.oauth.token.User | |
23 | const accountToBlock = res.locals.account | |
24 | ||
25 | if (user.Account.id === accountToBlock.id) { | |
26 | res.status(409) | |
27 | .json({ error: 'You cannot block yourself.' }) | |
28 | ||
29 | return | |
30 | } | |
31 | ||
32 | return next() | |
33 | } | |
34 | ] | |
35 | ||
36 | const unblockAccountByAccountValidator = [ | |
37 | param('accountName').exists().withMessage('Should have an account name with host'), | |
38 | ||
39 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
40 | logger.debug('Checking unblockAccountByAccountValidator parameters', { parameters: req.params }) | |
41 | ||
42 | if (areValidationErrors(req, res)) return | |
43 | if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return | |
44 | ||
45 | const user = res.locals.oauth.token.User | |
46 | const targetAccount = res.locals.account | |
47 | if (!await doesUnblockAccountExist(user.Account.id, targetAccount.id, res)) return | |
48 | ||
49 | return next() | |
50 | } | |
51 | ] | |
52 | ||
53 | const unblockAccountByServerValidator = [ | |
54 | param('accountName').exists().withMessage('Should have an account name with host'), | |
55 | ||
56 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
57 | logger.debug('Checking unblockAccountByServerValidator parameters', { parameters: req.params }) | |
58 | ||
59 | if (areValidationErrors(req, res)) return | |
60 | if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return | |
61 | ||
62 | const serverActor = await getServerActor() | |
63 | const targetAccount = res.locals.account | |
64 | if (!await doesUnblockAccountExist(serverActor.Account.id, targetAccount.id, res)) return | |
65 | ||
66 | return next() | |
67 | } | |
68 | ] | |
69 | ||
70 | const blockServerValidator = [ | |
71 | body('host').custom(isHostValid).withMessage('Should have a valid host'), | |
72 | ||
73 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
74 | logger.debug('Checking serverGetValidator parameters', { parameters: req.body }) | |
75 | ||
76 | if (areValidationErrors(req, res)) return | |
77 | ||
78 | const host: string = req.body.host | |
79 | ||
80 | if (host === WEBSERVER.HOST) { | |
81 | return res.status(409) | |
82 | .json({ error: 'You cannot block your own server.' }) | |
83 | } | |
84 | ||
85 | const server = await ServerModel.loadOrCreateByHost(host) | |
86 | ||
87 | res.locals.server = server | |
88 | ||
89 | return next() | |
90 | } | |
91 | ] | |
92 | ||
93 | const unblockServerByAccountValidator = [ | |
94 | param('host').custom(isHostValid).withMessage('Should have an account name with host'), | |
95 | ||
96 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
97 | logger.debug('Checking unblockServerByAccountValidator parameters', { parameters: req.params }) | |
98 | ||
99 | if (areValidationErrors(req, res)) return | |
100 | ||
101 | const user = res.locals.oauth.token.User | |
102 | if (!await doesUnblockServerExist(user.Account.id, req.params.host, res)) return | |
103 | ||
104 | return next() | |
105 | } | |
106 | ] | |
107 | ||
108 | const unblockServerByServerValidator = [ | |
109 | param('host').custom(isHostValid).withMessage('Should have an account name with host'), | |
110 | ||
111 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
112 | logger.debug('Checking unblockServerByServerValidator parameters', { parameters: req.params }) | |
113 | ||
114 | if (areValidationErrors(req, res)) return | |
115 | ||
116 | const serverActor = await getServerActor() | |
117 | if (!await doesUnblockServerExist(serverActor.Account.id, req.params.host, res)) return | |
118 | ||
119 | return next() | |
120 | } | |
121 | ] | |
122 | ||
123 | // --------------------------------------------------------------------------- | |
124 | ||
125 | export { | |
126 | blockServerValidator, | |
127 | blockAccountValidator, | |
128 | unblockAccountByAccountValidator, | |
129 | unblockServerByAccountValidator, | |
130 | unblockAccountByServerValidator, | |
131 | unblockServerByServerValidator | |
132 | } | |
133 | ||
134 | // --------------------------------------------------------------------------- | |
135 | ||
136 | async function doesUnblockAccountExist (accountId: number, targetAccountId: number, res: express.Response) { | |
137 | const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(accountId, targetAccountId) | |
138 | if (!accountBlock) { | |
139 | res.status(404) | |
140 | .json({ error: 'Account block entry not found.' }) | |
141 | ||
142 | return false | |
143 | } | |
144 | ||
145 | res.locals.accountBlock = accountBlock | |
146 | ||
147 | return true | |
148 | } | |
149 | ||
150 | async function doesUnblockServerExist (accountId: number, host: string, res: express.Response) { | |
151 | const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(accountId, host) | |
152 | if (!serverBlock) { | |
153 | res.status(404) | |
154 | .json({ error: 'Server block entry not found.' }) | |
155 | ||
156 | return false | |
157 | } | |
158 | ||
159 | res.locals.serverBlock = serverBlock | |
160 | ||
161 | return true | |
162 | } |