]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-channels.ts
Made the video channels limit (per user) server-wide configurable (#4491)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-channels.ts
1 import express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { MChannelAccountDefault, MUser } from '@server/types/models'
4 import { UserRight } from '../../../../shared'
5 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
6 import { isBooleanValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
7 import {
8 isVideoChannelDescriptionValid,
9 isVideoChannelDisplayNameValid,
10 isVideoChannelSupportValid,
11 isVideoChannelUsernameValid
12 } from '../../../helpers/custom-validators/video-channels'
13 import { logger } from '../../../helpers/logger'
14 import { ActorModel } from '../../../models/actor/actor'
15 import { VideoChannelModel } from '../../../models/video/video-channel'
16 import { areValidationErrors, doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../shared'
17 import { CONFIG } from '@server/initializers/config'
18
19 const videoChannelsAddValidator = [
20 body('name').custom(isVideoChannelUsernameValid).withMessage('Should have a valid channel name'),
21 body('displayName').custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
22 body('description').optional().custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
23 body('support').optional().custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
24
25 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
26 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
27
28 if (areValidationErrors(req, res)) return
29
30 const actor = await ActorModel.loadLocalByName(req.body.name)
31 if (actor) {
32 res.fail({
33 status: HttpStatusCode.CONFLICT_409,
34 message: 'Another actor (account/channel) with this name on this instance already exists or has already existed.'
35 })
36 return false
37 }
38
39 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
40 if (count >= CONFIG.VIDEO_CHANNELS.MAX_PER_USER) {
41 res.fail({ message: `You cannot create more than ${CONFIG.VIDEO_CHANNELS.MAX_PER_USER} channels` })
42 return false
43 }
44
45 return next()
46 }
47 ]
48
49 const videoChannelsUpdateValidator = [
50 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
51 body('displayName')
52 .optional()
53 .custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid display name'),
54 body('description')
55 .optional()
56 .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
57 body('support')
58 .optional()
59 .custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
60 body('bulkVideosSupportUpdate')
61 .optional()
62 .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
63
64 (req: express.Request, res: express.Response, next: express.NextFunction) => {
65 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
66
67 if (areValidationErrors(req, res)) return
68
69 return next()
70 }
71 ]
72
73 const videoChannelsRemoveValidator = [
74 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
75
76 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
77 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
78
79 if (areValidationErrors(req, res)) return
80 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
81
82 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
83 if (!await checkVideoChannelIsNotTheLastOne(res)) return
84
85 return next()
86 }
87 ]
88
89 const videoChannelsNameWithHostValidator = [
90 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
91
92 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
93 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
94
95 if (areValidationErrors(req, res)) return
96
97 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
98
99 return next()
100 }
101 ]
102
103 const localVideoChannelValidator = [
104 param('name').custom(isVideoChannelDisplayNameValid).withMessage('Should have a valid video channel name'),
105
106 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
107 logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
108
109 if (areValidationErrors(req, res)) return
110 if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return
111
112 return next()
113 }
114 ]
115
116 const videoChannelStatsValidator = [
117 query('withStats')
118 .optional()
119 .customSanitizer(toBooleanOrNull)
120 .custom(isBooleanValid).withMessage('Should have a valid stats flag'),
121
122 (req: express.Request, res: express.Response, next: express.NextFunction) => {
123 if (areValidationErrors(req, res)) return
124 return next()
125 }
126 ]
127
128 const videoChannelsListValidator = [
129 query('search').optional().not().isEmpty().withMessage('Should have a valid search'),
130
131 (req: express.Request, res: express.Response, next: express.NextFunction) => {
132 logger.debug('Checking video channels search query', { parameters: req.query })
133
134 if (areValidationErrors(req, res)) return
135
136 return next()
137 }
138 ]
139
140 // ---------------------------------------------------------------------------
141
142 export {
143 videoChannelsAddValidator,
144 videoChannelsUpdateValidator,
145 videoChannelsRemoveValidator,
146 videoChannelsNameWithHostValidator,
147 videoChannelsListValidator,
148 localVideoChannelValidator,
149 videoChannelStatsValidator
150 }
151
152 // ---------------------------------------------------------------------------
153
154 function checkUserCanDeleteVideoChannel (user: MUser, videoChannel: MChannelAccountDefault, res: express.Response) {
155 if (videoChannel.Actor.isOwned() === false) {
156 res.fail({
157 status: HttpStatusCode.FORBIDDEN_403,
158 message: 'Cannot remove video channel of another server.'
159 })
160 return false
161 }
162
163 // Check if the user can delete the video channel
164 // The user can delete it if s/he is an admin
165 // Or if s/he is the video channel's account
166 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
167 res.fail({
168 status: HttpStatusCode.FORBIDDEN_403,
169 message: 'Cannot remove video channel of another user'
170 })
171 return false
172 }
173
174 return true
175 }
176
177 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
178 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
179
180 if (count <= 1) {
181 res.fail({
182 status: HttpStatusCode.CONFLICT_409,
183 message: 'Cannot remove the last channel of this user'
184 })
185 return false
186 }
187
188 return true
189 }