]>
Commit | Line | Data |
---|---|---|
1 | import * as express from 'express' | |
2 | import { body, param } from 'express-validator' | |
3 | import { checkUserCanManageVideo, doesVideoChannelOfAccountExist, doesVideoExist } from '@server/helpers/middlewares/videos' | |
4 | import { VideoLiveModel } from '@server/models/video/video-live' | |
5 | import { ServerErrorCode, UserRight, VideoState } from '@shared/models' | |
6 | import { isBooleanValid, isIdOrUUIDValid, isIdValid, toBooleanOrNull, toIntOrNull } from '../../../helpers/custom-validators/misc' | |
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' | |
13 | import { VideoModel } from '@server/models/video/video' | |
14 | import { Hooks } from '@server/lib/plugins/hooks' | |
15 | import { isLocalLiveVideoAccepted } from '@server/lib/moderation' | |
16 | import { HttpStatusCode } from '@shared/core-utils/miscs/http-error-codes' | |
17 | ||
18 | const 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) => { | |
22 | logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params, user: res.locals.oauth.token.User.username }) | |
23 | ||
24 | if (areValidationErrors(req, res)) return | |
25 | if (!await doesVideoExist(req.params.videoId, res, 'all')) return | |
26 | ||
27 | // Check if the user who did the request is able to get the live info | |
28 | const user = res.locals.oauth.token.User | |
29 | if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res, false)) return | |
30 | ||
31 | const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id) | |
32 | if (!videoLive) return res.sendStatus(HttpStatusCode.NOT_FOUND_404) | |
33 | ||
34 | res.locals.videoLive = videoLive | |
35 | ||
36 | return next() | |
37 | } | |
38 | ] | |
39 | ||
40 | const 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 | ||
48 | body('saveReplay') | |
49 | .optional() | |
50 | .customSanitizer(toBooleanOrNull) | |
51 | .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'), | |
52 | ||
53 | body('permanentLive') | |
54 | .optional() | |
55 | .customSanitizer(toBooleanOrNull) | |
56 | .custom(isBooleanValid).withMessage('Should have a valid permanentLive attribute'), | |
57 | ||
58 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
59 | logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body }) | |
60 | ||
61 | if (areValidationErrors(req, res)) return cleanUpReqFiles(req) | |
62 | ||
63 | if (CONFIG.LIVE.ENABLED !== true) { | |
64 | cleanUpReqFiles(req) | |
65 | ||
66 | return res.status(HttpStatusCode.FORBIDDEN_403) | |
67 | .json({ error: 'Live is not enabled on this instance' }) | |
68 | } | |
69 | ||
70 | if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) { | |
71 | cleanUpReqFiles(req) | |
72 | ||
73 | return res.status(HttpStatusCode.FORBIDDEN_403) | |
74 | .json({ error: 'Saving live replay is not allowed instance' }) | |
75 | } | |
76 | ||
77 | if (req.body.permanentLive && req.body.saveReplay) { | |
78 | cleanUpReqFiles(req) | |
79 | ||
80 | return res.status(HttpStatusCode.BAD_REQUEST_400) | |
81 | .json({ error: 'Cannot set this live as permanent while saving its replay' }) | |
82 | } | |
83 | ||
84 | const user = res.locals.oauth.token.User | |
85 | if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) | |
86 | ||
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 | ||
93 | return res.status(HttpStatusCode.FORBIDDEN_403) | |
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 | ||
107 | return res.status(HttpStatusCode.FORBIDDEN_403) | |
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 | ||
115 | if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req) | |
116 | ||
117 | return next() | |
118 | } | |
119 | ]) | |
120 | ||
121 | const 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 | ||
132 | if (req.body.permanentLive && req.body.saveReplay) { | |
133 | return res.status(HttpStatusCode.BAD_REQUEST_400) | |
134 | .json({ error: 'Cannot set this live as permanent while saving its replay' }) | |
135 | } | |
136 | ||
137 | if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) { | |
138 | return res.status(HttpStatusCode.FORBIDDEN_403) | |
139 | .json({ error: 'Saving live replay is not allowed instance' }) | |
140 | } | |
141 | ||
142 | if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) { | |
143 | return res.status(HttpStatusCode.BAD_REQUEST_400) | |
144 | .json({ error: 'Cannot update a live that has already started' }) | |
145 | } | |
146 | ||
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 | ||
151 | return next() | |
152 | } | |
153 | ] | |
154 | ||
155 | // --------------------------------------------------------------------------- | |
156 | ||
157 | export { | |
158 | videoLiveAddValidator, | |
159 | videoLiveUpdateValidator, | |
160 | videoLiveGetValidator | |
161 | } | |
162 | ||
163 | // --------------------------------------------------------------------------- | |
164 | ||
165 | async 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 | ||
180 | res.status(HttpStatusCode.FORBIDDEN_403) | |
181 | .json({ error: acceptedResult.errorMessage || 'Refused local live video' }) | |
182 | ||
183 | return false | |
184 | } | |
185 | ||
186 | return true | |
187 | } |