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