]>
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 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' | |
13 | ||
14 | const blockAccountValidator = [ | |
15 | body('accountName').exists().withMessage('Should have an account name with host'), | |
16 | ||
17 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
18 | logger.debug('Checking blockAccountByAccountValidator parameters', { parameters: req.body }) | |
19 | ||
20 | if (areValidationErrors(req, res)) return | |
21 | if (!await doesAccountNameWithHostExist(req.body.accountName, res)) return | |
22 | ||
23 | const user = res.locals.oauth.token.User | |
24 | const accountToBlock = res.locals.account | |
25 | ||
26 | if (user.Account.id === accountToBlock.id) { | |
27 | res.status(HttpStatusCode.CONFLICT_409) | |
28 | .json({ error: 'You cannot block yourself.' }) | |
29 | ||
30 | return | |
31 | } | |
32 | ||
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 | |
44 | if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return | |
45 | ||
46 | const user = res.locals.oauth.token.User | |
47 | const targetAccount = res.locals.account | |
48 | if (!await doesUnblockAccountExist(user.Account.id, targetAccount.id, res)) return | |
49 | ||
50 | return next() | |
51 | } | |
52 | ] | |
53 | ||
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 | |
61 | if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return | |
62 | ||
63 | const serverActor = await getServerActor() | |
64 | const targetAccount = res.locals.account | |
65 | if (!await doesUnblockAccountExist(serverActor.Account.id, targetAccount.id, res)) return | |
66 | ||
67 | return next() | |
68 | } | |
69 | ] | |
70 | ||
71 | const blockServerValidator = [ | |
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 | ||
81 | if (host === WEBSERVER.HOST) { | |
82 | return res.status(HttpStatusCode.CONFLICT_409) | |
83 | .json({ error: 'You cannot block your own server.' }) | |
84 | } | |
85 | ||
86 | const server = await ServerModel.loadOrCreateByHost(host) | |
87 | ||
88 | res.locals.server = server | |
89 | ||
90 | return next() | |
91 | } | |
92 | ] | |
93 | ||
94 | const unblockServerByAccountValidator = [ | |
95 | param('host').custom(isHostValid).withMessage('Should have an account name with host'), | |
96 | ||
97 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
98 | logger.debug('Checking unblockServerByAccountValidator parameters', { parameters: req.params }) | |
99 | ||
100 | if (areValidationErrors(req, res)) return | |
101 | ||
102 | const user = res.locals.oauth.token.User | |
103 | if (!await doesUnblockServerExist(user.Account.id, req.params.host, res)) return | |
104 | ||
105 | return next() | |
106 | } | |
107 | ] | |
108 | ||
109 | const unblockServerByServerValidator = [ | |
110 | param('host').custom(isHostValid).withMessage('Should have an account name with host'), | |
111 | ||
112 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
113 | logger.debug('Checking unblockServerByServerValidator parameters', { parameters: req.params }) | |
114 | ||
115 | if (areValidationErrors(req, res)) return | |
116 | ||
117 | const serverActor = await getServerActor() | |
118 | if (!await doesUnblockServerExist(serverActor.Account.id, req.params.host, res)) return | |
119 | ||
120 | return next() | |
121 | } | |
122 | ] | |
123 | ||
124 | // --------------------------------------------------------------------------- | |
125 | ||
126 | export { | |
127 | blockServerValidator, | |
128 | blockAccountValidator, | |
129 | unblockAccountByAccountValidator, | |
130 | unblockServerByAccountValidator, | |
131 | unblockAccountByServerValidator, | |
132 | unblockServerByServerValidator | |
133 | } | |
134 | ||
135 | // --------------------------------------------------------------------------- | |
136 | ||
137 | async function doesUnblockAccountExist (accountId: number, targetAccountId: number, res: express.Response) { | |
138 | const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(accountId, targetAccountId) | |
139 | if (!accountBlock) { | |
140 | res.status(HttpStatusCode.NOT_FOUND_404) | |
141 | .json({ error: 'Account block entry not found.' }) | |
142 | ||
143 | return false | |
144 | } | |
145 | ||
146 | res.locals.accountBlock = accountBlock | |
147 | ||
148 | return true | |
149 | } | |
150 | ||
151 | async function doesUnblockServerExist (accountId: number, host: string, res: express.Response) { | |
152 | const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(accountId, host) | |
153 | if (!serverBlock) { | |
154 | res.status(HttpStatusCode.NOT_FOUND_404) | |
155 | .json({ error: 'Server block entry not found.' }) | |
156 | ||
157 | return false | |
158 | } | |
159 | ||
160 | res.locals.serverBlock = serverBlock | |
161 | ||
162 | return true | |
163 | } |