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