]>
Commit | Line | Data |
---|---|---|
1 | import express from 'express' | |
2 | import { exists } from '@server/helpers/custom-validators/misc' | |
3 | import { createReqFiles } from '@server/helpers/express-utils' | |
4 | import { getFormattedObjects } from '@server/helpers/utils' | |
5 | import { ASSETS_PATH, MIMETYPES } from '@server/initializers/constants' | |
6 | import { getLocalVideoActivityPubUrl } from '@server/lib/activitypub/url' | |
7 | import { federateVideoIfNeeded } from '@server/lib/activitypub/videos' | |
8 | import { Hooks } from '@server/lib/plugins/hooks' | |
9 | import { buildLocalVideoFromReq, buildVideoThumbnailsFromReq, setVideoTags } from '@server/lib/video' | |
10 | import { | |
11 | videoLiveAddValidator, | |
12 | videoLiveFindReplaySessionValidator, | |
13 | videoLiveGetValidator, | |
14 | videoLiveListSessionsValidator, | |
15 | videoLiveUpdateValidator | |
16 | } from '@server/middlewares/validators/videos/video-live' | |
17 | import { VideoLiveModel } from '@server/models/video/video-live' | |
18 | import { VideoLiveSessionModel } from '@server/models/video/video-live-session' | |
19 | import { MVideoDetails, MVideoFullLight, MVideoLive } from '@server/types/models' | |
20 | import { buildUUID, uuidToShort } from '@shared/extra-utils' | |
21 | import { HttpStatusCode, LiveVideoCreate, LiveVideoLatencyMode, LiveVideoUpdate, UserRight, VideoState } from '@shared/models' | |
22 | import { logger } from '../../../helpers/logger' | |
23 | import { sequelizeTypescript } from '../../../initializers/database' | |
24 | import { updateVideoMiniatureFromExisting } from '../../../lib/thumbnail' | |
25 | import { asyncMiddleware, asyncRetryTransactionMiddleware, authenticate, optionalAuthenticate } from '../../../middlewares' | |
26 | import { VideoModel } from '../../../models/video/video' | |
27 | import { VideoLiveReplaySettingModel } from '@server/models/video/video-live-replay-setting' | |
28 | ||
29 | const liveRouter = express.Router() | |
30 | ||
31 | const reqVideoFileLive = createReqFiles([ 'thumbnailfile', 'previewfile' ], MIMETYPES.IMAGE.MIMETYPE_EXT) | |
32 | ||
33 | liveRouter.post('/live', | |
34 | authenticate, | |
35 | reqVideoFileLive, | |
36 | asyncMiddleware(videoLiveAddValidator), | |
37 | asyncRetryTransactionMiddleware(addLiveVideo) | |
38 | ) | |
39 | ||
40 | liveRouter.get('/live/:videoId/sessions', | |
41 | authenticate, | |
42 | asyncMiddleware(videoLiveGetValidator), | |
43 | videoLiveListSessionsValidator, | |
44 | asyncMiddleware(getLiveVideoSessions) | |
45 | ) | |
46 | ||
47 | liveRouter.get('/live/:videoId', | |
48 | optionalAuthenticate, | |
49 | asyncMiddleware(videoLiveGetValidator), | |
50 | getLiveVideo | |
51 | ) | |
52 | ||
53 | liveRouter.put('/live/:videoId', | |
54 | authenticate, | |
55 | asyncMiddleware(videoLiveGetValidator), | |
56 | videoLiveUpdateValidator, | |
57 | asyncRetryTransactionMiddleware(updateLiveVideo) | |
58 | ) | |
59 | ||
60 | liveRouter.get('/:videoId/live-session', | |
61 | asyncMiddleware(videoLiveFindReplaySessionValidator), | |
62 | getLiveReplaySession | |
63 | ) | |
64 | ||
65 | // --------------------------------------------------------------------------- | |
66 | ||
67 | export { | |
68 | liveRouter | |
69 | } | |
70 | ||
71 | // --------------------------------------------------------------------------- | |
72 | ||
73 | function getLiveVideo (req: express.Request, res: express.Response) { | |
74 | const videoLive = res.locals.videoLive | |
75 | ||
76 | return res.json(videoLive.toFormattedJSON(canSeePrivateLiveInformation(res))) | |
77 | } | |
78 | ||
79 | function getLiveReplaySession (req: express.Request, res: express.Response) { | |
80 | const session = res.locals.videoLiveSession | |
81 | ||
82 | return res.json(session.toFormattedJSON()) | |
83 | } | |
84 | ||
85 | async function getLiveVideoSessions (req: express.Request, res: express.Response) { | |
86 | const videoLive = res.locals.videoLive | |
87 | ||
88 | const data = await VideoLiveSessionModel.listSessionsOfLiveForAPI({ videoId: videoLive.videoId }) | |
89 | ||
90 | return res.json(getFormattedObjects(data, data.length)) | |
91 | } | |
92 | ||
93 | function canSeePrivateLiveInformation (res: express.Response) { | |
94 | const user = res.locals.oauth?.token.User | |
95 | if (!user) return false | |
96 | ||
97 | if (user.hasRight(UserRight.GET_ANY_LIVE)) return true | |
98 | ||
99 | const video = res.locals.videoAll | |
100 | return video.VideoChannel.Account.userId === user.id | |
101 | } | |
102 | ||
103 | async function updateLiveVideo (req: express.Request, res: express.Response) { | |
104 | const body: LiveVideoUpdate = req.body | |
105 | ||
106 | const video = res.locals.videoAll | |
107 | const videoLive = res.locals.videoLive | |
108 | ||
109 | const newReplaySettingModel = await updateReplaySettings(videoLive, body) | |
110 | if (newReplaySettingModel) videoLive.replaySettingId = newReplaySettingModel.id | |
111 | else videoLive.replaySettingId = null | |
112 | ||
113 | if (exists(body.permanentLive)) videoLive.permanentLive = body.permanentLive | |
114 | if (exists(body.latencyMode)) videoLive.latencyMode = body.latencyMode | |
115 | ||
116 | video.VideoLive = await videoLive.save() | |
117 | ||
118 | await federateVideoIfNeeded(video, false) | |
119 | ||
120 | return res.status(HttpStatusCode.NO_CONTENT_204).end() | |
121 | } | |
122 | ||
123 | async function updateReplaySettings (videoLive: MVideoLive, body: LiveVideoUpdate) { | |
124 | if (exists(body.saveReplay)) videoLive.saveReplay = body.saveReplay | |
125 | ||
126 | // The live replay is not saved anymore, destroy the old model if it existed | |
127 | if (!videoLive.saveReplay) { | |
128 | if (videoLive.replaySettingId) { | |
129 | await VideoLiveReplaySettingModel.removeSettings(videoLive.replaySettingId) | |
130 | } | |
131 | ||
132 | return undefined | |
133 | } | |
134 | ||
135 | const settingModel = videoLive.replaySettingId | |
136 | ? await VideoLiveReplaySettingModel.load(videoLive.replaySettingId) | |
137 | : new VideoLiveReplaySettingModel() | |
138 | ||
139 | if (exists(body.replaySettings.privacy)) settingModel.privacy = body.replaySettings.privacy | |
140 | ||
141 | return settingModel.save() | |
142 | } | |
143 | ||
144 | async function addLiveVideo (req: express.Request, res: express.Response) { | |
145 | const videoInfo: LiveVideoCreate = req.body | |
146 | ||
147 | // Prepare data so we don't block the transaction | |
148 | let videoData = buildLocalVideoFromReq(videoInfo, res.locals.videoChannel.id) | |
149 | videoData = await Hooks.wrapObject(videoData, 'filter:api.video.live.video-attribute.result') | |
150 | ||
151 | videoData.isLive = true | |
152 | videoData.state = VideoState.WAITING_FOR_LIVE | |
153 | videoData.duration = 0 | |
154 | ||
155 | const video = new VideoModel(videoData) as MVideoDetails | |
156 | video.url = getLocalVideoActivityPubUrl(video) // We use the UUID, so set the URL after building the object | |
157 | ||
158 | const videoLive = new VideoLiveModel() | |
159 | videoLive.saveReplay = videoInfo.saveReplay || false | |
160 | videoLive.permanentLive = videoInfo.permanentLive || false | |
161 | videoLive.latencyMode = videoInfo.latencyMode || LiveVideoLatencyMode.DEFAULT | |
162 | videoLive.streamKey = buildUUID() | |
163 | ||
164 | const [ thumbnailModel, previewModel ] = await buildVideoThumbnailsFromReq({ | |
165 | video, | |
166 | files: req.files, | |
167 | fallback: type => { | |
168 | return updateVideoMiniatureFromExisting({ | |
169 | inputPath: ASSETS_PATH.DEFAULT_LIVE_BACKGROUND, | |
170 | video, | |
171 | type, | |
172 | automaticallyGenerated: true, | |
173 | keepOriginal: true | |
174 | }) | |
175 | } | |
176 | }) | |
177 | ||
178 | const { videoCreated } = await sequelizeTypescript.transaction(async t => { | |
179 | const sequelizeOptions = { transaction: t } | |
180 | ||
181 | const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight | |
182 | ||
183 | if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t) | |
184 | if (previewModel) await videoCreated.addAndSaveThumbnail(previewModel, t) | |
185 | ||
186 | // Do not forget to add video channel information to the created video | |
187 | videoCreated.VideoChannel = res.locals.videoChannel | |
188 | ||
189 | if (videoLive.saveReplay) { | |
190 | const replaySettings = new VideoLiveReplaySettingModel({ | |
191 | privacy: videoInfo.replaySettings.privacy | |
192 | }) | |
193 | await replaySettings.save(sequelizeOptions) | |
194 | ||
195 | videoLive.replaySettingId = replaySettings.id | |
196 | } | |
197 | ||
198 | videoLive.videoId = videoCreated.id | |
199 | videoCreated.VideoLive = await videoLive.save(sequelizeOptions) | |
200 | ||
201 | await setVideoTags({ video, tags: videoInfo.tags, transaction: t }) | |
202 | ||
203 | await federateVideoIfNeeded(videoCreated, true, t) | |
204 | ||
205 | logger.info('Video live %s with uuid %s created.', videoInfo.name, videoCreated.uuid) | |
206 | ||
207 | return { videoCreated } | |
208 | }) | |
209 | ||
210 | Hooks.runAction('action:api.live-video.created', { video: videoCreated, req, res }) | |
211 | ||
212 | return res.json({ | |
213 | video: { | |
214 | id: videoCreated.id, | |
215 | shortUUID: uuidToShort(videoCreated.uuid), | |
216 | uuid: videoCreated.uuid | |
217 | } | |
218 | }) | |
219 | } |