]>
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) | |
2d53be02 | 33 | if (!videoLive) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) |
c6c0fa6c C |
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') | |
7dab0bd6 RK |
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 | ), | |
c6c0fa6c | 50 | |
b5b68755 C |
51 | body('saveReplay') |
52 | .optional() | |
53 | .customSanitizer(toBooleanOrNull) | |
54 | .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'), | |
55 | ||
bb4ba6d9 C |
56 | body('permanentLive') |
57 | .optional() | |
58 | .customSanitizer(toBooleanOrNull) | |
59 | .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'), | |
60 | ||
c6c0fa6c C |
61 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
62 | logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body }) | |
63 | ||
bb4ba6d9 C |
64 | if (areValidationErrors(req, res)) return cleanUpReqFiles(req) |
65 | ||
c6c0fa6c | 66 | if (CONFIG.LIVE.ENABLED !== true) { |
a056ca48 C |
67 | cleanUpReqFiles(req) |
68 | ||
2d53be02 | 69 | return res.status(HttpStatusCode.FORBIDDEN_403) |
c6c0fa6c C |
70 | .json({ error: 'Live is not enabled on this instance' }) |
71 | } | |
72 | ||
b5b68755 | 73 | if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) { |
a056ca48 C |
74 | cleanUpReqFiles(req) |
75 | ||
2d53be02 | 76 | return res.status(HttpStatusCode.FORBIDDEN_403) |
b5b68755 C |
77 | .json({ error: 'Saving live replay is not allowed instance' }) |
78 | } | |
79 | ||
bb4ba6d9 C |
80 | if (req.body.permanentLive && req.body.saveReplay) { |
81 | cleanUpReqFiles(req) | |
82 | ||
2d53be02 | 83 | return res.status(HttpStatusCode.BAD_REQUEST_400) |
bb4ba6d9 C |
84 | .json({ error: 'Cannot set this live as permanent while saving its replay' }) |
85 | } | |
c6c0fa6c C |
86 | |
87 | const user = res.locals.oauth.token.User | |
88 | if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) | |
89 | ||
a056ca48 C |
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 | ||
2d53be02 | 96 | return res.status(HttpStatusCode.FORBIDDEN_403) |
a056ca48 C |
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 | ||
2d53be02 | 110 | return res.status(HttpStatusCode.FORBIDDEN_403) |
a056ca48 C |
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 | ||
3cabf353 C |
118 | if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req) |
119 | ||
c6c0fa6c C |
120 | return next() |
121 | } | |
122 | ]) | |
123 | ||
b5b68755 C |
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 | ||
bb4ba6d9 | 135 | if (req.body.permanentLive && req.body.saveReplay) { |
2d53be02 | 136 | return res.status(HttpStatusCode.BAD_REQUEST_400) |
bb4ba6d9 C |
137 | .json({ error: 'Cannot set this live as permanent while saving its replay' }) |
138 | } | |
139 | ||
b5b68755 | 140 | if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) { |
2d53be02 | 141 | return res.status(HttpStatusCode.FORBIDDEN_403) |
b5b68755 C |
142 | .json({ error: 'Saving live replay is not allowed instance' }) |
143 | } | |
144 | ||
145 | if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) { | |
2d53be02 | 146 | return res.status(HttpStatusCode.BAD_REQUEST_400) |
b5b68755 C |
147 | .json({ error: 'Cannot update a live that has already started' }) |
148 | } | |
149 | ||
af4ae64f C |
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 | ||
b5b68755 C |
154 | return next() |
155 | } | |
156 | ] | |
157 | ||
c6c0fa6c C |
158 | // --------------------------------------------------------------------------- |
159 | ||
160 | export { | |
161 | videoLiveAddValidator, | |
b5b68755 | 162 | videoLiveUpdateValidator, |
c6c0fa6c C |
163 | videoLiveGetValidator |
164 | } | |
3cabf353 C |
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 | ||
2d53be02 | 183 | res.status(HttpStatusCode.FORBIDDEN_403) |
3cabf353 C |
184 | .json({ error: acceptedResult.errorMessage || 'Refused local live video' }) |
185 | ||
186 | return false | |
187 | } | |
188 | ||
189 | return true | |
190 | } |