]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - 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
1 import express from 'express'
2 import { body } from 'express-validator'
3 import { CONSTRAINTS_FIELDS } from '@server/initializers/constants'
4 import { isLocalLiveVideoAccepted } from '@server/lib/moderation'
5 import { Hooks } from '@server/lib/plugins/hooks'
6 import { VideoModel } from '@server/models/video/video'
7 import { VideoLiveModel } from '@server/models/video/video-live'
8 import { HttpStatusCode, ServerErrorCode, UserRight, VideoState } from '@shared/models'
9 import { isBooleanValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc'
10 import { isVideoNameValid } from '../../../helpers/custom-validators/videos'
11 import { cleanUpReqFiles } from '../../../helpers/express-utils'
12 import { logger } from '../../../helpers/logger'
13 import { CONFIG } from '../../../initializers/config'
14 import {
15 areValidationErrors,
16 checkUserCanManageVideo,
17 doesVideoChannelOfAccountExist,
18 doesVideoExist,
19 isValidVideoIdParam
20 } from '../shared'
21 import { getCommonVideoEditAttributes } from './videos'
22
23 const videoLiveGetValidator = [
24 isValidVideoIdParam('videoId'),
25
26 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
27 logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params, user: res.locals.oauth.token.User.username })
28
29 if (areValidationErrors(req, res)) return
30 if (!await doesVideoExist(req.params.videoId, res, 'all')) return
31
32 // Check if the user who did the request is able to get the live info
33 const user = res.locals.oauth.token.User
34 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res, false)) return
35
36 const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id)
37 if (!videoLive) {
38 return res.fail({
39 status: HttpStatusCode.NOT_FOUND_404,
40 message: 'Live video not found'
41 })
42 }
43
44 res.locals.videoLive = videoLive
45
46 return next()
47 }
48 ]
49
50 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
51 body('channelId')
52 .customSanitizer(toIntOrNull)
53 .custom(isIdValid).withMessage('Should have correct video channel id'),
54
55 body('name')
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 ),
59
60 body('saveReplay')
61 .optional()
62 .customSanitizer(toBooleanOrNull)
63 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
64
65 body('permanentLive')
66 .optional()
67 .customSanitizer(toBooleanOrNull)
68 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
69
70 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
71 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
72
73 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
74
75 if (CONFIG.LIVE.ENABLED !== true) {
76 cleanUpReqFiles(req)
77
78 return res.fail({
79 status: HttpStatusCode.FORBIDDEN_403,
80 message: 'Live is not enabled on this instance',
81 type: ServerErrorCode.LIVE_NOT_ENABLED
82 })
83 }
84
85 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
86 cleanUpReqFiles(req)
87
88 return res.fail({
89 status: HttpStatusCode.FORBIDDEN_403,
90 message: 'Saving live replay is not enabled on this instance',
91 type: ServerErrorCode.LIVE_NOT_ALLOWING_REPLAY
92 })
93 }
94
95 if (req.body.permanentLive && req.body.saveReplay) {
96 cleanUpReqFiles(req)
97
98 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
99 }
100
101 const user = res.locals.oauth.token.User
102 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
103
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
110 return res.fail({
111 status: HttpStatusCode.FORBIDDEN_403,
112 message: 'Cannot create this live because the max instance lives limit is reached.',
113 type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED
114 })
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
124 return res.fail({
125 status: HttpStatusCode.FORBIDDEN_403,
126 message: 'Cannot create this live because the max user lives limit is reached.',
127 type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED
128 })
129 }
130 }
131
132 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
133
134 return next()
135 }
136 ])
137
138 const 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
149 if (req.body.permanentLive && req.body.saveReplay) {
150 return res.fail({ message: 'Cannot set this live as permanent while saving its replay' })
151 }
152
153 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
154 return res.fail({
155 status: HttpStatusCode.FORBIDDEN_403,
156 message: 'Saving live replay is not allowed instance'
157 })
158 }
159
160 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
161 return res.fail({ message: 'Cannot update a live that has already started' })
162 }
163
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
168 return next()
169 }
170 ]
171
172 // ---------------------------------------------------------------------------
173
174 export {
175 videoLiveAddValidator,
176 videoLiveUpdateValidator,
177 videoLiveGetValidator
178 }
179
180 // ---------------------------------------------------------------------------
181
182 async 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
197 res.fail({
198 status: HttpStatusCode.FORBIDDEN_403,
199 message: acceptedResult.errorMessage || 'Refused local live video'
200 })
201 return false
202 }
203
204 return true
205 }