]>
Commit | Line | Data |
---|---|---|
41fb13c3 | 1 | import express from 'express' |
80badf49 C |
2 | import { body, param, query } from 'express-validator' |
3 | import { areValidActorHandles } from '@server/helpers/custom-validators/activitypub/actor' | |
4 | import { toArray } from '@server/helpers/custom-validators/misc' | |
10363c74 | 5 | import { getServerActor } from '@server/models/application/application' |
c0e8b12e | 6 | import { HttpStatusCode } from '../../../shared/models/http/http-error-codes' |
80badf49 | 7 | import { isEachUniqueHostValid, isHostValid } from '../../helpers/custom-validators/servers' |
7ad9b984 | 8 | import { logger } from '../../helpers/logger' |
10363c74 | 9 | import { WEBSERVER } from '../../initializers/constants' |
7ad9b984 | 10 | import { AccountBlocklistModel } from '../../models/account/account-blocklist' |
af5767ff | 11 | import { ServerModel } from '../../models/server/server' |
10363c74 C |
12 | import { ServerBlocklistModel } from '../../models/server/server-blocklist' |
13 | import { areValidationErrors, doesAccountNameWithHostExist } from './shared' | |
7ad9b984 | 14 | |
b44164bb | 15 | const blockAccountValidator = [ |
396f6f01 C |
16 | body('accountName') |
17 | .exists(), | |
7ad9b984 C |
18 | |
19 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
20 | logger.debug('Checking blockAccountByAccountValidator parameters', { parameters: req.body }) | |
21 | ||
22 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 23 | if (!await doesAccountNameWithHostExist(req.body.accountName, res)) return |
7ad9b984 | 24 | |
dae86118 | 25 | const user = res.locals.oauth.token.User |
af5767ff C |
26 | const accountToBlock = res.locals.account |
27 | ||
28 | if (user.Account.id === accountToBlock.id) { | |
76148b27 RK |
29 | res.fail({ |
30 | status: HttpStatusCode.CONFLICT_409, | |
31 | message: 'You cannot block yourself.' | |
32 | }) | |
af5767ff C |
33 | return |
34 | } | |
35 | ||
7ad9b984 C |
36 | return next() |
37 | } | |
38 | ] | |
39 | ||
40 | const unblockAccountByAccountValidator = [ | |
396f6f01 C |
41 | param('accountName') |
42 | .exists(), | |
7ad9b984 C |
43 | |
44 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
45 | logger.debug('Checking unblockAccountByAccountValidator parameters', { parameters: req.params }) | |
46 | ||
47 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 48 | if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return |
7ad9b984 | 49 | |
dae86118 | 50 | const user = res.locals.oauth.token.User |
7ad9b984 | 51 | const targetAccount = res.locals.account |
0f6acda1 | 52 | if (!await doesUnblockAccountExist(user.Account.id, targetAccount.id, res)) return |
7ad9b984 C |
53 | |
54 | return next() | |
55 | } | |
56 | ] | |
57 | ||
b44164bb | 58 | const unblockAccountByServerValidator = [ |
396f6f01 C |
59 | param('accountName') |
60 | .exists(), | |
b44164bb C |
61 | |
62 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
63 | logger.debug('Checking unblockAccountByServerValidator parameters', { parameters: req.params }) | |
64 | ||
65 | if (areValidationErrors(req, res)) return | |
0f6acda1 | 66 | if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return |
b44164bb C |
67 | |
68 | const serverActor = await getServerActor() | |
69 | const targetAccount = res.locals.account | |
0f6acda1 | 70 | if (!await doesUnblockAccountExist(serverActor.Account.id, targetAccount.id, res)) return |
b44164bb C |
71 | |
72 | return next() | |
73 | } | |
74 | ] | |
75 | ||
76 | const blockServerValidator = [ | |
396f6f01 C |
77 | body('host') |
78 | .custom(isHostValid), | |
af5767ff C |
79 | |
80 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
81 | logger.debug('Checking serverGetValidator parameters', { parameters: req.body }) | |
82 | ||
83 | if (areValidationErrors(req, res)) return | |
84 | ||
85 | const host: string = req.body.host | |
86 | ||
6dd9de95 | 87 | if (host === WEBSERVER.HOST) { |
76148b27 RK |
88 | return res.fail({ |
89 | status: HttpStatusCode.CONFLICT_409, | |
90 | message: 'You cannot block your own server.' | |
91 | }) | |
af5767ff C |
92 | } |
93 | ||
80fdaf06 | 94 | const server = await ServerModel.loadOrCreateByHost(host) |
af5767ff C |
95 | |
96 | res.locals.server = server | |
97 | ||
98 | return next() | |
99 | } | |
100 | ] | |
101 | ||
7ad9b984 | 102 | const unblockServerByAccountValidator = [ |
396f6f01 C |
103 | param('host') |
104 | .custom(isHostValid), | |
7ad9b984 C |
105 | |
106 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
107 | logger.debug('Checking unblockServerByAccountValidator parameters', { parameters: req.params }) | |
108 | ||
109 | if (areValidationErrors(req, res)) return | |
110 | ||
dae86118 | 111 | const user = res.locals.oauth.token.User |
0f6acda1 | 112 | if (!await doesUnblockServerExist(user.Account.id, req.params.host, res)) return |
7ad9b984 C |
113 | |
114 | return next() | |
115 | } | |
116 | ] | |
117 | ||
b44164bb | 118 | const unblockServerByServerValidator = [ |
396f6f01 C |
119 | param('host') |
120 | .custom(isHostValid), | |
b44164bb C |
121 | |
122 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
123 | logger.debug('Checking unblockServerByServerValidator parameters', { parameters: req.params }) | |
124 | ||
125 | if (areValidationErrors(req, res)) return | |
126 | ||
127 | const serverActor = await getServerActor() | |
0f6acda1 | 128 | if (!await doesUnblockServerExist(serverActor.Account.id, req.params.host, res)) return |
b44164bb C |
129 | |
130 | return next() | |
131 | } | |
132 | ] | |
133 | ||
80badf49 C |
134 | const blocklistStatusValidator = [ |
135 | query('hosts') | |
136 | .optional() | |
137 | .customSanitizer(toArray) | |
138 | .custom(isEachUniqueHostValid).withMessage('Should have a valid hosts array'), | |
139 | ||
140 | query('accounts') | |
141 | .optional() | |
142 | .customSanitizer(toArray) | |
143 | .custom(areValidActorHandles).withMessage('Should have a valid accounts array'), | |
144 | ||
145 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
146 | logger.debug('Checking blocklistStatusValidator parameters', { query: req.query }) | |
147 | ||
148 | if (areValidationErrors(req, res)) return | |
149 | ||
150 | return next() | |
151 | } | |
152 | ] | |
153 | ||
7ad9b984 C |
154 | // --------------------------------------------------------------------------- |
155 | ||
156 | export { | |
b44164bb C |
157 | blockServerValidator, |
158 | blockAccountValidator, | |
7ad9b984 | 159 | unblockAccountByAccountValidator, |
b44164bb C |
160 | unblockServerByAccountValidator, |
161 | unblockAccountByServerValidator, | |
80badf49 C |
162 | unblockServerByServerValidator, |
163 | blocklistStatusValidator | |
7ad9b984 C |
164 | } |
165 | ||
166 | // --------------------------------------------------------------------------- | |
167 | ||
0f6acda1 | 168 | async function doesUnblockAccountExist (accountId: number, targetAccountId: number, res: express.Response) { |
7ad9b984 C |
169 | const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(accountId, targetAccountId) |
170 | if (!accountBlock) { | |
76148b27 RK |
171 | res.fail({ |
172 | status: HttpStatusCode.NOT_FOUND_404, | |
173 | message: 'Account block entry not found.' | |
174 | }) | |
7ad9b984 C |
175 | return false |
176 | } | |
177 | ||
178 | res.locals.accountBlock = accountBlock | |
7ad9b984 C |
179 | return true |
180 | } | |
181 | ||
0f6acda1 | 182 | async function doesUnblockServerExist (accountId: number, host: string, res: express.Response) { |
7ad9b984 C |
183 | const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(accountId, host) |
184 | if (!serverBlock) { | |
76148b27 RK |
185 | res.fail({ |
186 | status: HttpStatusCode.NOT_FOUND_404, | |
187 | message: 'Server block entry not found.' | |
188 | }) | |
7ad9b984 C |
189 | return false |
190 | } | |
191 | ||
192 | res.locals.serverBlock = serverBlock | |
7ad9b984 C |
193 | return true |
194 | } |