]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/middlewares/validators/videos/video-live.ts
Give moderators access to edit channels (#4608)
[github/Chocobozzz/PeerTube.git] / server / middlewares / validators / videos / video-live.ts
CommitLineData
41fb13c3 1import express from 'express'
d4a8e7a6 2import { body } from 'express-validator'
10363c74
C
3import { CONSTRAINTS_FIELDS } from '@server/initializers/constants'
4import { isLocalLiveVideoAccepted } from '@server/lib/moderation'
5import { Hooks } from '@server/lib/plugins/hooks'
6import { VideoModel } from '@server/models/video/video'
b5b68755 7import { VideoLiveModel } from '@server/models/video/video-live'
4c7e60bc 8import { HttpStatusCode, ServerErrorCode, UserRight, VideoState } from '@shared/models'
d4a8e7a6 9import { isBooleanValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
c6c0fa6c
C
10import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
11import { cleanUpReqFiles } from '../../../helpers/express-utils'
12import { logger } from '../../../helpers/logger'
13import { CONFIG } from '../../../initializers/config'
d4a8e7a6
C
14import {
15 areValidationErrors,
16 checkUserCanManageVideo,
17 doesVideoChannelOfAccountExist,
18 doesVideoExist,
19 isValidVideoIdParam
20} from '../shared'
c6c0fa6c 21import { getCommonVideoEditAttributes } from './videos'
c6c0fa6c
C
22
23const videoLiveGetValidator = [
d4a8e7a6 24 isValidVideoIdParam('videoId'),
c6c0fa6c
C
25
26 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
af4ae64f 27 logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params, user: res.locals.oauth.token.User.username })
c6c0fa6c
C
28
29 if (areValidationErrors(req, res)) return
30 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
31
af4ae64f 32 // Check if the user who did the request is able to get the live info
c6c0fa6c 33 const user = res.locals.oauth.token.User
af4ae64f 34 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res, false)) return
c6c0fa6c
C
35
36 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
76148b27
RK
37 if (!videoLive) {
38 return res.fail({
39 status: HttpStatusCode.NOT_FOUND_404,
40 message: 'Live video not found'
41 })
42 }
c6c0fa6c
C
43
44 res.locals.videoLive = videoLive
45
46 return next()
47 }
48]
49
50const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
51 body('channelId')
52 .customSanitizer(toIntOrNull)
53 .custom(isIdValid).withMessage('Should have correct video channel id'),
54
55 body('name')
7dab0bd6
RK
56 .custom(isVideoNameValid).withMessage(
57 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
58 ),
c6c0fa6c 59
b5b68755
C
60 body('saveReplay')
61 .optional()
62 .customSanitizer(toBooleanOrNull)
63 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
64
bb4ba6d9
C
65 body('permanentLive')
66 .optional()
67 .customSanitizer(toBooleanOrNull)
68 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
69
c6c0fa6c
C
70 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
71 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
72
bb4ba6d9
C
73 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
74
c6c0fa6c 75 if (CONFIG.LIVE.ENABLED !== true) {
a056ca48
C
76 cleanUpReqFiles(req)
77
76148b27
RK
78 return res.fail({
79 status: HttpStatusCode.FORBIDDEN_403,
c756bae0
RK
80 message: 'Live is not enabled on this instance',
81 type: ServerErrorCode.LIVE_NOT_ENABLED
76148b27 82 })
c6c0fa6c
C
83 }
84
b5b68755 85 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
a056ca48
C
86 cleanUpReqFiles(req)
87
76148b27
RK
88 return res.fail({
89 status: HttpStatusCode.FORBIDDEN_403,
c756bae0
RK
90 message: 'Saving live replay is not enabled on this instance',
91 type: ServerErrorCode.LIVE_NOT_ALLOWING_REPLAY
76148b27 92 })
b5b68755
C
93 }
94
bb4ba6d9
C
95 if (req.body.permanentLive && req.body.saveReplay) {
96 cleanUpReqFiles(req)
97
76148b27 98 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
bb4ba6d9 99 }
c6c0fa6c
C
100
101 const user = res.locals.oauth.token.User
102 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
103
a056ca48
C
104 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
105 const totalInstanceLives = await VideoModel.countLocalLives()
106
107 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
108 cleanUpReqFiles(req)
109
76148b27
RK
110 return res.fail({
111 status: HttpStatusCode.FORBIDDEN_403,
112 message: 'Cannot create this live because the max instance lives limit is reached.',
3866ea02 113 type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED
76148b27 114 })
a056ca48
C
115 }
116 }
117
118 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
119 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
120
121 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
122 cleanUpReqFiles(req)
123
76148b27
RK
124 return res.fail({
125 status: HttpStatusCode.FORBIDDEN_403,
c756bae0
RK
126 message: 'Cannot create this live because the max user lives limit is reached.',
127 type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED
76148b27 128 })
a056ca48
C
129 }
130 }
131
3cabf353
C
132 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
133
c6c0fa6c
C
134 return next()
135 }
136])
137
b5b68755
C
138const videoLiveUpdateValidator = [
139 body('saveReplay')
140 .optional()
141 .customSanitizer(toBooleanOrNull)
142 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
143
144 (req: express.Request, res: express.Response, next: express.NextFunction) => {
145 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
146
147 if (areValidationErrors(req, res)) return
148
bb4ba6d9 149 if (req.body.permanentLive && req.body.saveReplay) {
76148b27 150 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
bb4ba6d9
C
151 }
152
b5b68755 153 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
76148b27
RK
154 return res.fail({
155 status: HttpStatusCode.FORBIDDEN_403,
156 message: 'Saving live replay is not allowed instance'
157 })
b5b68755
C
158 }
159
160 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
76148b27 161 return res.fail({ message: 'Cannot update a live that has already started' })
b5b68755
C
162 }
163
af4ae64f
C
164 // Check the user can manage the live
165 const user = res.locals.oauth.token.User
166 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
167
b5b68755
C
168 return next()
169 }
170]
171
c6c0fa6c
C
172// ---------------------------------------------------------------------------
173
174export {
175 videoLiveAddValidator,
b5b68755 176 videoLiveUpdateValidator,
c6c0fa6c
C
177 videoLiveGetValidator
178}
3cabf353
C
179
180// ---------------------------------------------------------------------------
181
182async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
183 // Check we accept this video
184 const acceptParameters = {
185 liveVideoBody: req.body,
186 user: res.locals.oauth.token.User
187 }
188 const acceptedResult = await Hooks.wrapFun(
189 isLocalLiveVideoAccepted,
190 acceptParameters,
191 'filter:api.live-video.create.accept.result'
192 )
193
194 if (!acceptedResult || acceptedResult.accepted !== true) {
195 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
196
76148b27
RK
197 res.fail({
198 status: HttpStatusCode.FORBIDDEN_403,
199 message: acceptedResult.errorMessage || 'Refused local live video'
200 })
3cabf353
C
201 return false
202 }
203
204 return true
205}