]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/blocklist.ts
Fix CLI build
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / blocklist.ts
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 .send({ error: 'You cannot block yourself.' })
28 .end()
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(409)
83 .send({ error: 'You cannot block your own server.' })
84 .end()
85 }
86
87 const server = await ServerModel.loadOrCreateByHost(host)
88
89 res.locals.server = server
90
91 return next()
92 }
93 ]
94
95 const unblockServerByAccountValidator = [
96 param('host').custom(isHostValid).withMessage('Should have an account name with host'),
97
98 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
99 logger.debug('Checking unblockServerByAccountValidator parameters', { parameters: req.params })
100
101 if (areValidationErrors(req, res)) return
102
103 const user = res.locals.oauth.token.User
104 if (!await doesUnblockServerExist(user.Account.id, req.params.host, res)) return
105
106 return next()
107 }
108 ]
109
110 const unblockServerByServerValidator = [
111 param('host').custom(isHostValid).withMessage('Should have an account name with host'),
112
113 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
114 logger.debug('Checking unblockServerByServerValidator parameters', { parameters: req.params })
115
116 if (areValidationErrors(req, res)) return
117
118 const serverActor = await getServerActor()
119 if (!await doesUnblockServerExist(serverActor.Account.id, req.params.host, res)) return
120
121 return next()
122 }
123 ]
124
125 // ---------------------------------------------------------------------------
126
127 export {
128 blockServerValidator,
129 blockAccountValidator,
130 unblockAccountByAccountValidator,
131 unblockServerByAccountValidator,
132 unblockAccountByServerValidator,
133 unblockServerByServerValidator
134 }
135
136 // ---------------------------------------------------------------------------
137
138 async function doesUnblockAccountExist (accountId: number, targetAccountId: number, res: express.Response) {
139 const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(accountId, targetAccountId)
140 if (!accountBlock) {
141 res.status(404)
142 .send({ error: 'Account block entry not found.' })
143 .end()
144
145 return false
146 }
147
148 res.locals.accountBlock = accountBlock
149
150 return true
151 }
152
153 async function doesUnblockServerExist (accountId: number, host: string, res: express.Response) {
154 const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(accountId, host)
155 if (!serverBlock) {
156 res.status(404)
157 .send({ error: 'Server block entry not found.' })
158 .end()
159
160 return false
161 }
162
163 res.locals.serverBlock = serverBlock
164
165 return true
166 }