]>
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' |
6dd9de95 | 9 | import { WEBSERVER } from '../../initializers/constants' |
3e753302 | 10 | import { doesAccountNameWithHostExist } from '../../helpers/middlewares' |
8dc8a34e | 11 | import { getServerActor } from '@server/models/application/application' |
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) | |
444c0a0e | 27 | .json({ error: 'You cannot block yourself.' }) |
af5767ff C |
28 | |
29 | return | |
30 | } | |
31 | ||
7ad9b984 C |
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 | |
0f6acda1 | 43 | if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return |
7ad9b984 | 44 | |
dae86118 | 45 | const user = res.locals.oauth.token.User |
7ad9b984 | 46 | const targetAccount = res.locals.account |
0f6acda1 | 47 | if (!await doesUnblockAccountExist(user.Account.id, targetAccount.id, res)) return |
7ad9b984 C |
48 | |
49 | return next() | |
50 | } | |
51 | ] | |
52 | ||
b44164bb C |
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 | |
0f6acda1 | 60 | if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return |
b44164bb C |
61 | |
62 | const serverActor = await getServerActor() | |
63 | const targetAccount = res.locals.account | |
0f6acda1 | 64 | if (!await doesUnblockAccountExist(serverActor.Account.id, targetAccount.id, res)) return |
b44164bb C |
65 | |
66 | return next() | |
67 | } | |
68 | ] | |
69 | ||
70 | const blockServerValidator = [ | |
af5767ff C |
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 | ||
6dd9de95 | 80 | if (host === WEBSERVER.HOST) { |
af5767ff | 81 | return res.status(409) |
444c0a0e | 82 | .json({ error: 'You cannot block your own server.' }) |
af5767ff C |
83 | } |
84 | ||
80fdaf06 | 85 | const server = await ServerModel.loadOrCreateByHost(host) |
af5767ff C |
86 | |
87 | res.locals.server = server | |
88 | ||
89 | return next() | |
90 | } | |
91 | ] | |
92 | ||
7ad9b984 C |
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 | ||
dae86118 | 101 | const user = res.locals.oauth.token.User |
0f6acda1 | 102 | if (!await doesUnblockServerExist(user.Account.id, req.params.host, res)) return |
7ad9b984 C |
103 | |
104 | return next() | |
105 | } | |
106 | ] | |
107 | ||
b44164bb C |
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() | |
0f6acda1 | 117 | if (!await doesUnblockServerExist(serverActor.Account.id, req.params.host, res)) return |
b44164bb C |
118 | |
119 | return next() | |
120 | } | |
121 | ] | |
122 | ||
7ad9b984 C |
123 | // --------------------------------------------------------------------------- |
124 | ||
125 | export { | |
b44164bb C |
126 | blockServerValidator, |
127 | blockAccountValidator, | |
7ad9b984 | 128 | unblockAccountByAccountValidator, |
b44164bb C |
129 | unblockServerByAccountValidator, |
130 | unblockAccountByServerValidator, | |
131 | unblockServerByServerValidator | |
7ad9b984 C |
132 | } |
133 | ||
134 | // --------------------------------------------------------------------------- | |
135 | ||
0f6acda1 | 136 | async function doesUnblockAccountExist (accountId: number, targetAccountId: number, res: express.Response) { |
7ad9b984 C |
137 | const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(accountId, targetAccountId) |
138 | if (!accountBlock) { | |
139 | res.status(404) | |
444c0a0e | 140 | .json({ error: 'Account block entry not found.' }) |
7ad9b984 C |
141 | |
142 | return false | |
143 | } | |
144 | ||
145 | res.locals.accountBlock = accountBlock | |
146 | ||
147 | return true | |
148 | } | |
149 | ||
0f6acda1 | 150 | async function doesUnblockServerExist (accountId: number, host: string, res: express.Response) { |
7ad9b984 C |
151 | const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(accountId, host) |
152 | if (!serverBlock) { | |
153 | res.status(404) | |
444c0a0e | 154 | .json({ error: 'Server block entry not found.' }) |
7ad9b984 C |
155 | |
156 | return false | |
157 | } | |
158 | ||
159 | res.locals.serverBlock = serverBlock | |
160 | ||
161 | return true | |
162 | } |