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