]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/blocklist.ts
Support short uuid for GET video/playlist
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / blocklist.ts
CommitLineData
7ad9b984 1import * as express from 'express'
10363c74
C
2import { body, param } from 'express-validator'
3import { getServerActor } from '@server/models/application/application'
4import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
5import { isHostValid } from '../../helpers/custom-validators/servers'
7ad9b984 6import { logger } from '../../helpers/logger'
10363c74 7import { WEBSERVER } from '../../initializers/constants'
7ad9b984 8import { AccountBlocklistModel } from '../../models/account/account-blocklist'
af5767ff 9import { ServerModel } from '../../models/server/server'
10363c74
C
10import { ServerBlocklistModel } from '../../models/server/server-blocklist'
11import { areValidationErrors, doesAccountNameWithHostExist } from './shared'
7ad9b984 12
b44164bb 13const 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) {
76148b27
RK
26 res.fail({
27 status: HttpStatusCode.CONFLICT_409,
28 message: 'You cannot block yourself.'
29 })
af5767ff
C
30 return
31 }
32
7ad9b984
C
33 return next()
34 }
35]
36
37const 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
0f6acda1 44 if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return
7ad9b984 45
dae86118 46 const user = res.locals.oauth.token.User
7ad9b984 47 const targetAccount = res.locals.account
0f6acda1 48 if (!await doesUnblockAccountExist(user.Account.id, targetAccount.id, res)) return
7ad9b984
C
49
50 return next()
51 }
52]
53
b44164bb
C
54const 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
0f6acda1 61 if (!await doesAccountNameWithHostExist(req.params.accountName, res)) return
b44164bb
C
62
63 const serverActor = await getServerActor()
64 const targetAccount = res.locals.account
0f6acda1 65 if (!await doesUnblockAccountExist(serverActor.Account.id, targetAccount.id, res)) return
b44164bb
C
66
67 return next()
68 }
69]
70
71const blockServerValidator = [
af5767ff
C
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
6dd9de95 81 if (host === WEBSERVER.HOST) {
76148b27
RK
82 return res.fail({
83 status: HttpStatusCode.CONFLICT_409,
84 message: 'You cannot block your own server.'
85 })
af5767ff
C
86 }
87
80fdaf06 88 const server = await ServerModel.loadOrCreateByHost(host)
af5767ff
C
89
90 res.locals.server = server
91
92 return next()
93 }
94]
95
7ad9b984
C
96const unblockServerByAccountValidator = [
97 param('host').custom(isHostValid).withMessage('Should have an account name with host'),
98
99 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
100 logger.debug('Checking unblockServerByAccountValidator parameters', { parameters: req.params })
101
102 if (areValidationErrors(req, res)) return
103
dae86118 104 const user = res.locals.oauth.token.User
0f6acda1 105 if (!await doesUnblockServerExist(user.Account.id, req.params.host, res)) return
7ad9b984
C
106
107 return next()
108 }
109]
110
b44164bb
C
111const unblockServerByServerValidator = [
112 param('host').custom(isHostValid).withMessage('Should have an account name with host'),
113
114 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
115 logger.debug('Checking unblockServerByServerValidator parameters', { parameters: req.params })
116
117 if (areValidationErrors(req, res)) return
118
119 const serverActor = await getServerActor()
0f6acda1 120 if (!await doesUnblockServerExist(serverActor.Account.id, req.params.host, res)) return
b44164bb
C
121
122 return next()
123 }
124]
125
7ad9b984
C
126// ---------------------------------------------------------------------------
127
128export {
b44164bb
C
129 blockServerValidator,
130 blockAccountValidator,
7ad9b984 131 unblockAccountByAccountValidator,
b44164bb
C
132 unblockServerByAccountValidator,
133 unblockAccountByServerValidator,
134 unblockServerByServerValidator
7ad9b984
C
135}
136
137// ---------------------------------------------------------------------------
138
0f6acda1 139async function doesUnblockAccountExist (accountId: number, targetAccountId: number, res: express.Response) {
7ad9b984
C
140 const accountBlock = await AccountBlocklistModel.loadByAccountAndTarget(accountId, targetAccountId)
141 if (!accountBlock) {
76148b27
RK
142 res.fail({
143 status: HttpStatusCode.NOT_FOUND_404,
144 message: 'Account block entry not found.'
145 })
7ad9b984
C
146 return false
147 }
148
149 res.locals.accountBlock = accountBlock
7ad9b984
C
150 return true
151}
152
0f6acda1 153async function doesUnblockServerExist (accountId: number, host: string, res: express.Response) {
7ad9b984
C
154 const serverBlock = await ServerBlocklistModel.loadByAccountAndHost(accountId, host)
155 if (!serverBlock) {
76148b27
RK
156 res.fail({
157 status: HttpStatusCode.NOT_FOUND_404,
158 message: 'Server block entry not found.'
159 })
7ad9b984
C
160 return false
161 }
162
163 res.locals.serverBlock = serverBlock
7ad9b984
C
164 return true
165}