]>
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' | |
c6c0fa6c C |
16 | |
17 | const videoLiveGetValidator = [ | |
18 | param('videoId').custom(isIdOrUUIDValid).not().isEmpty().withMessage('Should have a valid videoId'), | |
19 | ||
20 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
af4ae64f | 21 | logger.debug('Checking videoLiveGetValidator parameters', { parameters: req.params, user: res.locals.oauth.token.User.username }) |
c6c0fa6c C |
22 | |
23 | if (areValidationErrors(req, res)) return | |
24 | if (!await doesVideoExist(req.params.videoId, res, 'all')) return | |
25 | ||
af4ae64f | 26 | // Check if the user who did the request is able to get the live info |
c6c0fa6c | 27 | const user = res.locals.oauth.token.User |
af4ae64f | 28 | if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res, false)) return |
c6c0fa6c C |
29 | |
30 | const videoLive = await VideoLiveModel.loadByVideoId(res.locals.videoAll.id) | |
31 | if (!videoLive) return res.sendStatus(404) | |
32 | ||
33 | res.locals.videoLive = videoLive | |
34 | ||
35 | return next() | |
36 | } | |
37 | ] | |
38 | ||
39 | const videoLiveAddValidator = getCommonVideoEditAttributes().concat([ | |
40 | body('channelId') | |
41 | .customSanitizer(toIntOrNull) | |
42 | .custom(isIdValid).withMessage('Should have correct video channel id'), | |
43 | ||
44 | body('name') | |
45 | .custom(isVideoNameValid).withMessage('Should have a valid name'), | |
46 | ||
b5b68755 C |
47 | body('saveReplay') |
48 | .optional() | |
49 | .customSanitizer(toBooleanOrNull) | |
50 | .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'), | |
51 | ||
c6c0fa6c C |
52 | async (req: express.Request, res: express.Response, next: express.NextFunction) => { |
53 | logger.debug('Checking videoLiveAddValidator parameters', { parameters: req.body }) | |
54 | ||
55 | if (CONFIG.LIVE.ENABLED !== true) { | |
a056ca48 C |
56 | cleanUpReqFiles(req) |
57 | ||
c6c0fa6c C |
58 | return res.status(403) |
59 | .json({ error: 'Live is not enabled on this instance' }) | |
60 | } | |
61 | ||
b5b68755 | 62 | if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) { |
a056ca48 C |
63 | cleanUpReqFiles(req) |
64 | ||
b5b68755 C |
65 | return res.status(403) |
66 | .json({ error: 'Saving live replay is not allowed instance' }) | |
67 | } | |
68 | ||
c6c0fa6c C |
69 | if (areValidationErrors(req, res)) return cleanUpReqFiles(req) |
70 | ||
71 | const user = res.locals.oauth.token.User | |
72 | if (!await doesVideoChannelOfAccountExist(req.body.channelId, user, res)) return cleanUpReqFiles(req) | |
73 | ||
a056ca48 C |
74 | if (CONFIG.LIVE.MAX_INSTANCE_LIVES !== -1) { |
75 | const totalInstanceLives = await VideoModel.countLocalLives() | |
76 | ||
77 | if (totalInstanceLives >= CONFIG.LIVE.MAX_INSTANCE_LIVES) { | |
78 | cleanUpReqFiles(req) | |
79 | ||
80 | return res.status(403) | |
81 | .json({ | |
82 | code: ServerErrorCode.MAX_INSTANCE_LIVES_LIMIT_REACHED, | |
83 | error: 'Cannot create this live because the max instance lives limit is reached.' | |
84 | }) | |
85 | } | |
86 | } | |
87 | ||
88 | if (CONFIG.LIVE.MAX_USER_LIVES !== -1) { | |
89 | const totalUserLives = await VideoModel.countLivesOfAccount(user.Account.id) | |
90 | ||
91 | if (totalUserLives >= CONFIG.LIVE.MAX_USER_LIVES) { | |
92 | cleanUpReqFiles(req) | |
93 | ||
94 | return res.status(403) | |
95 | .json({ | |
96 | code: ServerErrorCode.MAX_USER_LIVES_LIMIT_REACHED, | |
97 | error: 'Cannot create this live because the max user lives limit is reached.' | |
98 | }) | |
99 | } | |
100 | } | |
101 | ||
3cabf353 C |
102 | if (!await isLiveVideoAccepted(req, res)) return cleanUpReqFiles(req) |
103 | ||
c6c0fa6c C |
104 | return next() |
105 | } | |
106 | ]) | |
107 | ||
b5b68755 C |
108 | const videoLiveUpdateValidator = [ |
109 | body('saveReplay') | |
110 | .optional() | |
111 | .customSanitizer(toBooleanOrNull) | |
112 | .custom(isBooleanValid).withMessage('Should have a valid saveReplay attribute'), | |
113 | ||
114 | (req: express.Request, res: express.Response, next: express.NextFunction) => { | |
115 | logger.debug('Checking videoLiveUpdateValidator parameters', { parameters: req.body }) | |
116 | ||
117 | if (areValidationErrors(req, res)) return | |
118 | ||
119 | if (CONFIG.LIVE.ALLOW_REPLAY !== true && req.body.saveReplay === true) { | |
120 | return res.status(403) | |
121 | .json({ error: 'Saving live replay is not allowed instance' }) | |
122 | } | |
123 | ||
124 | if (res.locals.videoAll.state !== VideoState.WAITING_FOR_LIVE) { | |
125 | return res.status(400) | |
126 | .json({ error: 'Cannot update a live that has already started' }) | |
127 | } | |
128 | ||
af4ae64f C |
129 | // Check the user can manage the live |
130 | const user = res.locals.oauth.token.User | |
131 | if (!checkUserCanManageVideo(user, res.locals.videoAll, UserRight.GET_ANY_LIVE, res)) return | |
132 | ||
b5b68755 C |
133 | return next() |
134 | } | |
135 | ] | |
136 | ||
c6c0fa6c C |
137 | // --------------------------------------------------------------------------- |
138 | ||
139 | export { | |
140 | videoLiveAddValidator, | |
b5b68755 | 141 | videoLiveUpdateValidator, |
c6c0fa6c C |
142 | videoLiveGetValidator |
143 | } | |
3cabf353 C |
144 | |
145 | // --------------------------------------------------------------------------- | |
146 | ||
147 | async function isLiveVideoAccepted (req: express.Request, res: express.Response) { | |
148 | // Check we accept this video | |
149 | const acceptParameters = { | |
150 | liveVideoBody: req.body, | |
151 | user: res.locals.oauth.token.User | |
152 | } | |
153 | const acceptedResult = await Hooks.wrapFun( | |
154 | isLocalLiveVideoAccepted, | |
155 | acceptParameters, | |
156 | 'filter:api.live-video.create.accept.result' | |
157 | ) | |
158 | ||
159 | if (!acceptedResult || acceptedResult.accepted !== true) { | |
160 | logger.info('Refused local live video.', { acceptedResult, acceptParameters }) | |
161 | ||
162 | res.status(403) | |
163 | .json({ error: acceptedResult.errorMessage || 'Refused local live video' }) | |
164 | ||
165 | return false | |
166 | } | |
167 | ||
168 | return true | |
169 | } |