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