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