]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/middlewares/validators/videos/video-live.ts
improve api param message for video names and tags
[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) return res.sendStatus(HttpStatusCode.NOT_FOUND_404)
34
35 res.locals.videoLive = videoLive
36
37 return next()
38 }
39 ]
40
41 const videoLiveAddValidator = getCommonVideoEditAttributes().concat([
42 body('channelId')
43 .customSanitizer(toIntOrNull)
44 .custom(isIdValid).withMessage('Should have correct video channel id'),
45
46 body('name')
47 .custom(isVideoNameValid).withMessage(
48 `Should have a video name between ${CONSTRAINTS_FIELDS.VIDEOS.NAME.min} and ${CONSTRAINTS_FIELDS.VIDEOS.NAME.max} characters long`
49 ),
50
51 body('saveReplay')
52 .optional()
53 .customSanitizer(toBooleanOrNull)
54 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
55
56 body('permanentLive')
57 .optional()
58 .customSanitizer(toBooleanOrNull)
59 .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'),
60
61 async (req: express.Request, res: express.Response, next: express.NextFunction) => {
62 logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body })
63
64 if (areValidationErrors(req, res)) return cleanUpReqFiles(req)
65
66 if (CONFIG.LIVE.ENABLED !== true) {
67 cleanUpReqFiles(req)
68
69 return res.status(HttpStatusCode.FORBIDDEN_403)
70 .json({ error: 'Live is not enabled on this instance' })
71 }
72
73 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
74 cleanUpReqFiles(req)
75
76 return res.status(HttpStatusCode.FORBIDDEN_403)
77 .json({ error: 'Saving live replay is not allowed instance' })
78 }
79
80 if (req.body.permanentLive && req.body.saveReplay) {
81 cleanUpReqFiles(req)
82
83 return res.status(HttpStatusCode.BAD_REQUEST_400)
84 .json({ error: 'Cannot set this live as permanent while saving its replay' })
85 }
86
87 const user = res.locals.oauth.token.User
88 if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req)
89
90 if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) {
91 const totalInstanceLives = await VideoModel.countLocalLives()
92
93 if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) {
94 cleanUpReqFiles(req)
95
96 return res.status(HttpStatusCode.FORBIDDEN_403)
97 .json({
98 code: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED,
99 error: 'Cannot create this live because the max instance lives limit is reached.'
100 })
101 }
102 }
103
104 if (CONFIG.LIVE.MAX_USER_LIVES !== -1) {
105 const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id)
106
107 if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) {
108 cleanUpReqFiles(req)
109
110 return res.status(HttpStatusCode.FORBIDDEN_403)
111 .json({
112 code: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED,
113 error: 'Cannot create this live because the max user lives limit is reached.'
114 })
115 }
116 }
117
118 if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req)
119
120 return next()
121 }
122 ])
123
124 const videoLiveUpdateValidator = [
125 body('saveReplay')
126 .optional()
127 .customSanitizer(toBooleanOrNull)
128 .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'),
129
130 (req: express.Request, res: express.Response, next: express.NextFunction) => {
131 logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body })
132
133 if (areValidationErrors(req, res)) return
134
135 if (req.body.permanentLive && req.body.saveReplay) {
136 return res.status(HttpStatusCode.BAD_REQUEST_400)
137 .json({ error: 'Cannot set this live as permanent while saving its replay' })
138 }
139
140 if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) {
141 return res.status(HttpStatusCode.FORBIDDEN_403)
142 .json({ error: 'Saving live replay is not allowed instance' })
143 }
144
145 if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) {
146 return res.status(HttpStatusCode.BAD_REQUEST_400)
147 .json({ error: 'Cannot update a live that has already started' })
148 }
149
150 // Check the user can manage the live
151 const user = res.locals.oauth.token.User
152 if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return
153
154 return next()
155 }
156 ]
157
158 // ---------------------------------------------------------------------------
159
160 export {
161 videoLiveAddValidator,
162 videoLiveUpdateValidator,
163 videoLiveGetValidator
164 }
165
166 // ---------------------------------------------------------------------------
167
168 async function isLiveVideoAccepted (req: express.Request, res: express.Response) {
169 // Check we accept this video
170 const acceptParameters = {
171 liveVideoBody: req.body,
172 user: res.locals.oauth.token.User
173 }
174 const acceptedResult = await Hooks.wrapFun(
175 isLocalLiveVideoAccepted,
176 acceptParameters,
177 'filter:api.live-video.create.accept.result'
178 )
179
180 if (!acceptedResult || acceptedResult.accepted !== true) {
181 logger.info('Refused local live video.', { acceptedResult, acceptParameters })
182
183 res.status(HttpStatusCode.FORBIDDEN_403)
184 .json({ error: acceptedResult.errorMessage || 'Refused local live video' })
185
186 return false
187 }
188
189 return true
190 }