]>
Commit | Line | Data |
---|---|---|
c6c0fa6c C |
1 | import * as express from 'express' |
2 | import { body, param } from 'express-validator' | |
3 | import { checkUserCanManageVideo, doesVideoChannelOfAccountExist, doesVideoExist } from '@server/helpers/middlewares/videos' | |
b5b68755 | 4 | import { VideoLiveModel } from '@server/models/video/video-live' |
a056ca48 | 5 | import { ServerErrorCode, UserRight, VideoState } from '@shared/models' |
b5b68755 | 6 | import { isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc' |
c6c0fa6c C |
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' | |
a056ca48 | 13 | import { VideoModel } from '@server/models/video/video' |
3cabf353 C |
14 | import { Hooks } from '@server/lib/plugins/hooks' |
15 | import { isLocalLiveVideoAccepted } from '@server/lib/moderation' | |
2d53be02 | 16 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' |
7dab0bd6 | 17 | import { CONSTRAINTS_FIELDS } from '@server/initializers/constants' |
c6c0fa6c C |
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) => { | |
af4ae64f | 23 | logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params, user: res.locals.oauth.token.User.username }) |
c6c0fa6c C |
24 | |
25 | if (areValidationErrors(req, res)) return | |
26 | if (!await doesVideoExist(req.params.videoId, res, 'all')) return | |
27 | ||
af4ae64f | 28 | // Check if the user who did the request is able to get the live info |
c6c0fa6c | 29 | const user = res.locals.oauth.token.User |
af4ae64f | 30 | if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res, false)) return |
c6c0fa6c C |
31 | |
32 | const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id) | |
76148b27 RK |
33 | if (!videoLive) { |
34 | return res.fail({ | |
35 | status: HttpStatusCode.NOT_FOUND_404, | |
36 | message: 'Live video not found' | |
37 | }) | |
38 | } | |
c6c0fa6c C |
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') | |
7dab0bd6 RK |
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 | ), | |
c6c0fa6c | 55 | |
b5b68755 C |
56 | body('saveReplay') |
57 | .optional() | |
58 | .customSanitizer(toBooleanOrNull) | |
59 | .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'), | |
60 | ||
bb4ba6d9 C |
61 | body('permanentLive') |
62 | .optional() | |
63 | .customSanitizer(toBooleanOrNull) | |
64 | .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'), | |
65 | ||
c6c0fa6c C |
66 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
67 | logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body }) | |
68 | ||
bb4ba6d9 C |
69 | if (areValidationErrors(req, res)) return cleanUpReqFiles(req) |
70 | ||
c6c0fa6c | 71 | if (CONFIG.LIVE.ENABLED !== true) { |
a056ca48 C |
72 | cleanUpReqFiles(req) |
73 | ||
76148b27 RK |
74 | return res.fail({ |
75 | status: HttpStatusCode.FORBIDDEN_403, | |
76 | message: 'Live is not enabled on this instance' | |
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, | |
85 | message: 'Saving live replay is not allowed instance' | |
86 | }) | |
b5b68755 C |
87 | } |
88 | ||
bb4ba6d9 C |
89 | if (req.body.permanentLive && req.body.saveReplay) { |
90 | cleanUpReqFiles(req) | |
91 | ||
76148b27 | 92 | return res.fail({ message: 'Cannot set this live as permanent while saving its replay' }) |
bb4ba6d9 | 93 | } |
c6c0fa6c C |
94 | |
95 | const user = res.locals.oauth.token.User | |
96 | if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) | |
97 | ||
a056ca48 C |
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 | ||
76148b27 RK |
104 | return res.fail({ |
105 | status: HttpStatusCode.FORBIDDEN_403, | |
106 | message: 'Cannot create this live because the max instance lives limit is reached.', | |
3866ea02 | 107 | type: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED |
76148b27 | 108 | }) |
a056ca48 C |
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 | ||
76148b27 RK |
118 | return res.fail({ |
119 | status: HttpStatusCode.FORBIDDEN_403, | |
3866ea02 | 120 | type: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED, |
76148b27 RK |
121 | message: 'Cannot create this live because the max user lives limit is reached.' |
122 | }) | |
a056ca48 C |
123 | } |
124 | } | |
125 | ||
3cabf353 C |
126 | if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req) |
127 | ||
c6c0fa6c C |
128 | return next() |
129 | } | |
130 | ]) | |
131 | ||
b5b68755 C |
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 | ||
bb4ba6d9 | 143 | if (req.body.permanentLive && req.body.saveReplay) { |
76148b27 | 144 | return res.fail({ message: 'Cannot set this live as permanent while saving its replay' }) |
bb4ba6d9 C |
145 | } |
146 | ||
b5b68755 | 147 | if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) { |
76148b27 RK |
148 | return res.fail({ |
149 | status: HttpStatusCode.FORBIDDEN_403, | |
150 | message: 'Saving live replay is not allowed instance' | |
151 | }) | |
b5b68755 C |
152 | } |
153 | ||
154 | if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) { | |
76148b27 | 155 | return res.fail({ message: 'Cannot update a live that has already started' }) |
b5b68755 C |
156 | } |
157 | ||
af4ae64f C |
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 | ||
b5b68755 C |
162 | return next() |
163 | } | |
164 | ] | |
165 | ||
c6c0fa6c C |
166 | // --------------------------------------------------------------------------- |
167 | ||
168 | export { | |
169 | videoLiveAddValidator, | |
b5b68755 | 170 | videoLiveUpdateValidator, |
c6c0fa6c C |
171 | videoLiveGetValidator |
172 | } | |
3cabf353 C |
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 | ||
76148b27 RK |
191 | res.fail({ |
192 | status: HttpStatusCode.FORBIDDEN_403, | |
193 | message: acceptedResult.errorMessage || 'Refused local live video' | |
194 | }) | |
3cabf353 C |
195 | return false |
196 | } | |
197 | ||
198 | return true | |
199 | } |