aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/middlewares/validators/blocklist.ts
diff options
context:
space:
mode:
authorChocobozzz <me@florianbigard.com>2018-10-12 15:26:04 +0200
committerChocobozzz <me@florianbigard.com>2018-10-16 16:41:36 +0200
commit7ad9b9846c44d198a736183fb186c2039f5236b5 (patch)
tree9c8456882a261c0522efb507f20e323c2741a0f8 /server/middlewares/validators/blocklist.ts
parentdffd5d127f49eb63d2b2b3133aec75ec1d7e4dcb (diff)
downloadPeerTube-7ad9b9846c44d198a736183fb186c2039f5236b5.tar.gz
PeerTube-7ad9b9846c44d198a736183fb186c2039f5236b5.tar.zst
PeerTube-7ad9b9846c44d198a736183fb186c2039f5236b5.zip
Add ability for users to block an account/instance on server side
Diffstat (limited to 'server/middlewares/validators/blocklist.ts')
-rw-r--r--server/middlewares/validators/blocklist.ts94
1 files changed, 94 insertions, 0 deletions
diff --git a/server/middlewares/validators/blocklist.ts b/server/middlewares/validators/blocklist.ts
new file mode 100644
index 000000000..9dbd5e512
--- /dev/null
+++ b/server/middlewares/validators/blocklist.ts
@@ -0,0 +1,94 @@
1import { param, body } from 'express-validator/check'
2import * as express from 'express'
3import { logger } from '../../helpers/logger'
4import { areValidationErrors } from './utils'
5import { isAccountNameWithHostExist } from '../../helpers/custom-validators/accounts'
6import { UserModel } from '../../models/account/user'
7import { AccountBlocklistModel } from '../../models/account/account-blocklist'
8import { isHostValid } from '../../helpers/custom-validators/servers'
9import { ServerBlocklistModel } from '../../models/server/server-blocklist'
10
11const blockAccountByAccountValidator = [
12 body('accountName').exists().withMessage('Should have an account name with host'),
13
14 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
15 logger.debug('Checking blockAccountByAccountValidator parameters', { parameters: req.body })
16
17 if (areValidationErrors(req, res)) return
18 if (!await isAccountNameWithHostExist(req.body.accountName, res)) return
19
20 return next()
21 }
22]
23
24const unblockAccountByAccountValidator = [
25 param('accountName').exists().withMessage('Should have an account name with host'),
26
27 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
28 logger.debug('Checking unblockAccountByAccountValidator parameters', { parameters: req.params })
29
30 if (areValidationErrors(req, res)) return
31 if (!await isAccountNameWithHostExist(req.params.accountName, res)) return
32
33 const user = res.locals.oauth.token.User as UserModel
34 const targetAccount = res.locals.account
35 if (!await isUnblockAccountExists(user.Account.id, targetAccount.id, res)) return
36
37 return next()
38 }
39]
40
41const unblockServerByAccountValidator = [
42 param('host').custom(isHostValid).withMessage('Should have an account name with host'),
43
44 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
45 logger.debug('Checking unblockServerByAccountValidator parameters', { parameters: req.params })
46
47 if (areValidationErrors(req, res)) return
48
49 const user = res.locals.oauth.token.User as UserModel
50 if (!await isUnblockServerExists(user.Account.id, req.params.host, res)) return
51
52 return next()
53 }
54]
55
56// ---------------------------------------------------------------------------
57
58export {
59 blockAccountByAccountValidator,
60 unblockAccountByAccountValidator,
61 unblockServerByAccountValidator
62}
63
64// ---------------------------------------------------------------------------
65
66async function isUnblockAccountExists (accountId: number, targetAccountId: number, res: express.Response) {
67 const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(accountId, targetAccountId)
68 if (!accountBlock) {
69 res.status(404)
70 .send({ error: 'Account block entry not found.' })
71 .end()
72
73 return false
74 }
75
76 res.locals.accountBlock = accountBlock
77
78 return true
79}
80
81async function isUnblockServerExists (accountId: number, host: string, res: express.Response) {
82 const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(accountId, host)
83 if (!serverBlock) {
84 res.status(404)
85 .send({ error: 'Server block entry not found.' })
86 .end()
87
88 return false
89 }
90
91 res.locals.serverBlock = serverBlock
92
93 return true
94}