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