]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-channels.ts
Refactor requests
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-channels.ts
1 import * as express from 'express'
2 import { body, param, query } from 'express-validator'
3 import { VIDEO_CHANNELS } from '@server/initializers/constants'
4 import { MChannelAccountDefault, MUser } from '@server/types/models'
5 import { UserRight } from '../../../../shared'
6 import { HttpStatusCode } from '../../../../shared/models/http/http-error-codes'
7 import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor'
8 import { isBooleanValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
9 import {
10 isVideoChannelDescriptionValid,
11 isVideoChannelNameValid,
12 isVideoChannelSupportValid
13 } from '../../../helpers/custom-validators/video-channels'
14 import { logger } from '../../../helpers/logger'
15 import { ActorModel } from '../../../models/actor/actor'
16 import { VideoChannelModel } from '../../../models/video/video-channel'
17 import { areValidationErrors, doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../shared'
18
19 const videoChannelsAddValidator = [
20 body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
21 body('displayName').custom(isVideoChannelNameValid).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 >= VIDEO_CHANNELS.MAX_PER_USER) {
41 res.fail({ message: `You cannot create more than ${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(isVideoChannelNameValid).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 async (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 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
69
70 // We need to make additional checks
71 if (res.locals.videoChannel.Actor.isOwned() === false) {
72 return res.fail({
73 status: HttpStatusCode.FORBIDDEN_403,
74 message: 'Cannot update video channel of another server'
75 })
76 }
77
78 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
79 return res.fail({
80 status: HttpStatusCode.FORBIDDEN_403,
81 message: 'Cannot update video channel of another user'
82 })
83 }
84
85 return next()
86 }
87 ]
88
89 const videoChannelsRemoveValidator = [
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 videoChannelsRemove parameters', { parameters: req.params })
94
95 if (areValidationErrors(req, res)) return
96 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
97
98 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
99 if (!await checkVideoChannelIsNotTheLastOne(res)) return
100
101 return next()
102 }
103 ]
104
105 const videoChannelsNameWithHostValidator = [
106 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
107
108 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
109 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
110
111 if (areValidationErrors(req, res)) return
112
113 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
114
115 return next()
116 }
117 ]
118
119 const localVideoChannelValidator = [
120 param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
121
122 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
123 logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
124
125 if (areValidationErrors(req, res)) return
126 if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return
127
128 return next()
129 }
130 ]
131
132 const videoChannelStatsValidator = [
133 query('withStats')
134 .optional()
135 .customSanitizer(toBooleanOrNull)
136 .custom(isBooleanValid).withMessage('Should have a valid stats flag'),
137
138 (req: express.Request, res: express.Response, next: express.NextFunction) => {
139 if (areValidationErrors(req, res)) return
140 return next()
141 }
142 ]
143
144 const videoChannelsListValidator = [
145 query('search').optional().not().isEmpty().withMessage('Should have a valid search'),
146
147 (req: express.Request, res: express.Response, next: express.NextFunction) => {
148 logger.debug('Checking video channels search query', { parameters: req.query })
149
150 if (areValidationErrors(req, res)) return
151
152 return next()
153 }
154 ]
155
156 // ---------------------------------------------------------------------------
157
158 export {
159 videoChannelsAddValidator,
160 videoChannelsUpdateValidator,
161 videoChannelsRemoveValidator,
162 videoChannelsNameWithHostValidator,
163 videoChannelsListValidator,
164 localVideoChannelValidator,
165 videoChannelStatsValidator
166 }
167
168 // ---------------------------------------------------------------------------
169
170 function checkUserCanDeleteVideoChannel (user: MUser, videoChannel: MChannelAccountDefault, res: express.Response) {
171 if (videoChannel.Actor.isOwned() === false) {
172 res.fail({
173 status: HttpStatusCode.FORBIDDEN_403,
174 message: 'Cannot remove video channel of another server.'
175 })
176 return false
177 }
178
179 // Check if the user can delete the video channel
180 // The user can delete it if s/he is an admin
181 // Or if s/he is the video channel's account
182 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
183 res.fail({
184 status: HttpStatusCode.FORBIDDEN_403,
185 message: 'Cannot remove video channel of another user'
186 })
187 return false
188 }
189
190 return true
191 }
192
193 async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
194 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
195
196 if (count <= 1) {
197 res.fail({
198 status: HttpStatusCode.CONFLICT_409,
199 message: 'Cannot remove the last channel of this user'
200 })
201 return false
202 }
203
204 return true
205 }