]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-channels.ts
Live streaming implementation first step
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-channels.ts
CommitLineData
72c7248b 1import * as express from 'express'
747c5628 2import { body, param, query } from 'express-validator'
5a61ffbb 3import { VIDEO_CHANNELS } from '@server/initializers/constants'
26d6bf65 4import { MChannelAccountDefault, MUser } from '@server/types/models'
6e46de09 5import { UserRight } from '../../../../shared'
5a61ffbb
C
6import { isActorPreferredUsernameValid } from '../../../helpers/custom-validators/activitypub/actor'
7import { isBooleanValid, toBooleanOrNull } from '../../../helpers/custom-validators/misc'
4e50b6a1 8import {
7d8e778a 9 isVideoChannelDescriptionValid,
7d8e778a
C
10 isVideoChannelNameValid,
11 isVideoChannelSupportValid
6e46de09
C
12} from '../../../helpers/custom-validators/video-channels'
13import { logger } from '../../../helpers/logger'
5a61ffbb
C
14import { doesLocalVideoChannelNameExist, doesVideoChannelNameWithHostExist } from '../../../helpers/middlewares'
15import { ActorModel } from '../../../models/activitypub/actor'
6e46de09
C
16import { VideoChannelModel } from '../../../models/video/video-channel'
17import { areValidationErrors } from '../utils'
72c7248b 18
72c7248b 19const videoChannelsAddValidator = [
8a19bee1 20 body('name').custom(isActorPreferredUsernameValid).withMessage('Should have a valid channel name'),
08c1efbe 21 body('displayName').custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
2422c46b
C
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'),
72c7248b 24
601527d7 25 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
26 logger.debug('Checking videoChannelsAdd parameters', { parameters: req.body })
27
a2431b7d
C
28 if (areValidationErrors(req, res)) return
29
601527d7
C
30 const actor = await ActorModel.loadLocalByName(req.body.name)
31 if (actor) {
32 res.status(409)
33 .send({ error: 'Another actor (account/channel) with this name on this instance already exists or has already existed.' })
34 .end()
35 return false
36 }
37
a3ce4ae8 38 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
5197732b 39 if (count >= VIDEO_CHANNELS.MAX_PER_USER) {
a3ce4ae8
C
40 res.status(400)
41 .send({ error: `You cannot create more than ${VIDEO_CHANNELS.MAX_PER_USER} channels` })
42 .end()
43 return false
44 }
45
a2431b7d 46 return next()
72c7248b
C
47 }
48]
49
50const videoChannelsUpdateValidator = [
8a19bee1 51 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
7d14d4d2
C
52 body('displayName')
53 .optional()
54 .custom(isVideoChannelNameValid).withMessage('Should have a valid display name'),
55 body('description')
56 .optional()
57 .custom(isVideoChannelDescriptionValid).withMessage('Should have a valid description'),
58 body('support')
59 .optional()
60 .custom(isVideoChannelSupportValid).withMessage('Should have a valid support text'),
61 body('bulkVideosSupportUpdate')
62 .optional()
63 .custom(isBooleanValid).withMessage('Should have a valid bulkVideosSupportUpdate boolean field'),
72c7248b 64
a2431b7d 65 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
66 logger.debug('Checking videoChannelsUpdate parameters', { parameters: req.body })
67
a2431b7d 68 if (areValidationErrors(req, res)) return
0f6acda1 69 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
a2431b7d
C
70
71 // We need to make additional checks
d50acfab 72 if (res.locals.videoChannel.Actor.isOwned() === false) {
a2431b7d
C
73 return res.status(403)
74 .json({ error: 'Cannot update video channel of another server' })
75 .end()
76 }
77
78 if (res.locals.videoChannel.Account.userId !== res.locals.oauth.token.User.id) {
79 return res.status(403)
80 .json({ error: 'Cannot update video channel of another user' })
81 .end()
82 }
83
84 return next()
72c7248b
C
85 }
86]
87
88const videoChannelsRemoveValidator = [
8a19bee1 89 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
72c7248b 90
a2431b7d 91 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
72c7248b
C
92 logger.debug('Checking videoChannelsRemove parameters', { parameters: req.params })
93
a2431b7d 94 if (areValidationErrors(req, res)) return
0f6acda1 95 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
a2431b7d 96
d48ff09d 97 if (!checkUserCanDeleteVideoChannel(res.locals.oauth.token.User, res.locals.videoChannel, res)) return
a2431b7d
C
98 if (!await checkVideoChannelIsNotTheLastOne(res)) return
99
100 return next()
72c7248b
C
101 }
102]
103
8a19bee1
C
104const videoChannelsNameWithHostValidator = [
105 param('nameWithHost').exists().withMessage('Should have an video channel name with host'),
72c7248b 106
a2431b7d 107 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
8a19bee1 108 logger.debug('Checking videoChannelsNameWithHostValidator parameters', { parameters: req.params })
72c7248b 109
a2431b7d 110 if (areValidationErrors(req, res)) return
6b738c7a 111
0f6acda1 112 if (!await doesVideoChannelNameWithHostExist(req.params.nameWithHost, res)) return
a2431b7d
C
113
114 return next()
72c7248b
C
115 }
116]
117
06a05d5f
C
118const localVideoChannelValidator = [
119 param('name').custom(isVideoChannelNameValid).withMessage('Should have a valid video channel name'),
120
121 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
122 logger.debug('Checking localVideoChannelValidator parameters', { parameters: req.params })
123
124 if (areValidationErrors(req, res)) return
0f6acda1 125 if (!await doesLocalVideoChannelNameExist(req.params.name, res)) return
06a05d5f
C
126
127 return next()
128 }
129]
130
747c5628 131const videoChannelStatsValidator = [
5a61ffbb
C
132 query('withStats')
133 .optional()
134 .customSanitizer(toBooleanOrNull)
135 .custom(isBooleanValid).withMessage('Should have a valid stats flag'),
747c5628
RK
136
137 (req: express.Request, res: express.Response, next: express.NextFunction) => {
138 if (areValidationErrors(req, res)) return
139 return next()
140 }
141]
142
72c7248b
C
143// ---------------------------------------------------------------------------
144
145export {
72c7248b
C
146 videoChannelsAddValidator,
147 videoChannelsUpdateValidator,
148 videoChannelsRemoveValidator,
8a19bee1 149 videoChannelsNameWithHostValidator,
747c5628
RK
150 localVideoChannelValidator,
151 videoChannelStatsValidator
72c7248b
C
152}
153
154// ---------------------------------------------------------------------------
155
0283eaac 156function checkUserCanDeleteVideoChannel (user: MUser, videoChannel: MChannelAccountDefault, res: express.Response) {
50d6de9c 157 if (videoChannel.Actor.isOwned() === false) {
a2431b7d 158 res.status(403)
60862425 159 .json({ error: 'Cannot remove video channel of another server.' })
72c7248b 160 .end()
a2431b7d
C
161
162 return false
72c7248b
C
163 }
164
165 // Check if the user can delete the video channel
166 // The user can delete it if s/he is an admin
38fa2065 167 // Or if s/he is the video channel's account
a2431b7d
C
168 if (user.hasRight(UserRight.REMOVE_ANY_VIDEO_CHANNEL) === false && videoChannel.Account.userId !== user.id) {
169 res.status(403)
72c7248b
C
170 .json({ error: 'Cannot remove video channel of another user' })
171 .end()
a2431b7d
C
172
173 return false
72c7248b
C
174 }
175
a2431b7d 176 return true
72c7248b
C
177}
178
a2431b7d 179async function checkVideoChannelIsNotTheLastOne (res: express.Response) {
3fd3ab2d 180 const count = await VideoChannelModel.countByAccount(res.locals.oauth.token.User.Account.id)
a2431b7d
C
181
182 if (count <= 1) {
183 res.status(409)
184 .json({ error: 'Cannot remove the last channel of this user' })
185 .end()
186
187 return false
188 }
189
190 return true
72c7248b 191}