]>
Commit | Line | Data |
---|---|---|
1 | import { Video, VideoDetails } from '../../../shared/models/videos' | |
2 | import { VideoModel } from './video' | |
3 | import { ActivityTagObject, ActivityUrlObject, VideoTorrentObject } from '../../../shared/models/activitypub/objects' | |
4 | import { MIMETYPES, WEBSERVER } from '../../initializers/constants' | |
5 | import { VideoCaptionModel } from './video-caption' | |
6 | import { | |
7 | getVideoCommentsActivityPubUrl, | |
8 | getVideoDislikesActivityPubUrl, | |
9 | getVideoLikesActivityPubUrl, | |
10 | getVideoSharesActivityPubUrl | |
11 | } from '../../lib/activitypub' | |
12 | import { isArray } from '../../helpers/custom-validators/misc' | |
13 | import { VideoStreamingPlaylist } from '../../../shared/models/videos/video-streaming-playlist.model' | |
14 | import { | |
15 | MStreamingPlaylistRedundanciesOpt, | |
16 | MStreamingPlaylistVideo, | |
17 | MVideo, | |
18 | MVideoAP, | |
19 | MVideoFile, | |
20 | MVideoFormattable, | |
21 | MVideoFormattableDetails | |
22 | } from '../../typings/models' | |
23 | import { MVideoFileRedundanciesOpt } from '../../typings/models/video/video-file' | |
24 | import { VideoFile } from '@shared/models/videos/video-file.model' | |
25 | import { generateMagnetUri } from '@server/helpers/webtorrent' | |
26 | import { extractVideo } from '@server/lib/videos' | |
27 | ||
28 | export type VideoFormattingJSONOptions = { | |
29 | completeDescription?: boolean | |
30 | additionalAttributes: { | |
31 | state?: boolean | |
32 | waitTranscoding?: boolean | |
33 | scheduledUpdate?: boolean | |
34 | blacklistInfo?: boolean | |
35 | } | |
36 | } | |
37 | ||
38 | function videoModelToFormattedJSON (video: MVideoFormattable, options?: VideoFormattingJSONOptions): Video { | |
39 | const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined | |
40 | ||
41 | const videoObject: Video = { | |
42 | id: video.id, | |
43 | uuid: video.uuid, | |
44 | name: video.name, | |
45 | category: { | |
46 | id: video.category, | |
47 | label: VideoModel.getCategoryLabel(video.category) | |
48 | }, | |
49 | licence: { | |
50 | id: video.licence, | |
51 | label: VideoModel.getLicenceLabel(video.licence) | |
52 | }, | |
53 | language: { | |
54 | id: video.language, | |
55 | label: VideoModel.getLanguageLabel(video.language) | |
56 | }, | |
57 | privacy: { | |
58 | id: video.privacy, | |
59 | label: VideoModel.getPrivacyLabel(video.privacy) | |
60 | }, | |
61 | nsfw: video.nsfw, | |
62 | description: options && options.completeDescription === true ? video.description : video.getTruncatedDescription(), | |
63 | isLocal: video.isOwned(), | |
64 | duration: video.duration, | |
65 | views: video.views, | |
66 | likes: video.likes, | |
67 | dislikes: video.dislikes, | |
68 | thumbnailPath: video.getMiniatureStaticPath(), | |
69 | previewPath: video.getPreviewStaticPath(), | |
70 | embedPath: video.getEmbedStaticPath(), | |
71 | createdAt: video.createdAt, | |
72 | updatedAt: video.updatedAt, | |
73 | publishedAt: video.publishedAt, | |
74 | originallyPublishedAt: video.originallyPublishedAt, | |
75 | ||
76 | account: video.VideoChannel.Account.toFormattedSummaryJSON(), | |
77 | channel: video.VideoChannel.toFormattedSummaryJSON(), | |
78 | ||
79 | userHistory: userHistory ? { | |
80 | currentTime: userHistory.currentTime | |
81 | } : undefined | |
82 | } | |
83 | ||
84 | if (options) { | |
85 | if (options.additionalAttributes.state === true) { | |
86 | videoObject.state = { | |
87 | id: video.state, | |
88 | label: VideoModel.getStateLabel(video.state) | |
89 | } | |
90 | } | |
91 | ||
92 | if (options.additionalAttributes.waitTranscoding === true) { | |
93 | videoObject.waitTranscoding = video.waitTranscoding | |
94 | } | |
95 | ||
96 | if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) { | |
97 | videoObject.scheduledUpdate = { | |
98 | updateAt: video.ScheduleVideoUpdate.updateAt, | |
99 | privacy: video.ScheduleVideoUpdate.privacy || undefined | |
100 | } | |
101 | } | |
102 | ||
103 | if (options.additionalAttributes.blacklistInfo === true) { | |
104 | videoObject.blacklisted = !!video.VideoBlacklist | |
105 | videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null | |
106 | } | |
107 | } | |
108 | ||
109 | return videoObject | |
110 | } | |
111 | ||
112 | function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails { | |
113 | const formattedJson = video.toFormattedJSON({ | |
114 | additionalAttributes: { | |
115 | scheduledUpdate: true, | |
116 | blacklistInfo: true | |
117 | } | |
118 | }) | |
119 | ||
120 | const { baseUrlHttp, baseUrlWs } = video.getBaseUrls() | |
121 | ||
122 | const tags = video.Tags ? video.Tags.map(t => t.name) : [] | |
123 | ||
124 | const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists) | |
125 | ||
126 | const detailsJson = { | |
127 | support: video.support, | |
128 | descriptionPath: video.getDescriptionAPIPath(), | |
129 | channel: video.VideoChannel.toFormattedJSON(), | |
130 | account: video.VideoChannel.Account.toFormattedJSON(), | |
131 | tags, | |
132 | commentsEnabled: video.commentsEnabled, | |
133 | downloadEnabled: video.downloadEnabled, | |
134 | waitTranscoding: video.waitTranscoding, | |
135 | state: { | |
136 | id: video.state, | |
137 | label: VideoModel.getStateLabel(video.state) | |
138 | }, | |
139 | ||
140 | trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs), | |
141 | ||
142 | files: [], | |
143 | streamingPlaylists | |
144 | } | |
145 | ||
146 | // Format and sort video files | |
147 | detailsJson.files = videoFilesModelToFormattedJSON(video, baseUrlHttp, baseUrlWs, video.VideoFiles) | |
148 | ||
149 | return Object.assign(formattedJson, detailsJson) | |
150 | } | |
151 | ||
152 | function streamingPlaylistsModelToFormattedJSON (video: MVideo, playlists: MStreamingPlaylistRedundanciesOpt[]): VideoStreamingPlaylist[] { | |
153 | if (isArray(playlists) === false) return [] | |
154 | ||
155 | const { baseUrlHttp, baseUrlWs } = video.getBaseUrls() | |
156 | ||
157 | return playlists | |
158 | .map(playlist => { | |
159 | const playlistWithVideo = Object.assign(playlist, { Video: video }) | |
160 | ||
161 | const redundancies = isArray(playlist.RedundancyVideos) | |
162 | ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl })) | |
163 | : [] | |
164 | ||
165 | const files = videoFilesModelToFormattedJSON(playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles) | |
166 | ||
167 | return { | |
168 | id: playlist.id, | |
169 | type: playlist.type, | |
170 | playlistUrl: playlist.playlistUrl, | |
171 | segmentsSha256Url: playlist.segmentsSha256Url, | |
172 | redundancies, | |
173 | files | |
174 | } | |
175 | }) | |
176 | } | |
177 | ||
178 | function videoFilesModelToFormattedJSON ( | |
179 | model: MVideo | MStreamingPlaylistVideo, | |
180 | baseUrlHttp: string, | |
181 | baseUrlWs: string, | |
182 | videoFiles: MVideoFileRedundanciesOpt[] | |
183 | ): VideoFile[] { | |
184 | const video = extractVideo(model) | |
185 | ||
186 | return videoFiles | |
187 | .map(videoFile => { | |
188 | return { | |
189 | resolution: { | |
190 | id: videoFile.resolution, | |
191 | label: videoFile.resolution + 'p' | |
192 | }, | |
193 | magnetUri: generateMagnetUri(model, videoFile, baseUrlHttp, baseUrlWs), | |
194 | size: videoFile.size, | |
195 | fps: videoFile.fps, | |
196 | torrentUrl: model.getTorrentUrl(videoFile, baseUrlHttp), | |
197 | torrentDownloadUrl: model.getTorrentDownloadUrl(videoFile, baseUrlHttp), | |
198 | fileUrl: model.getVideoFileUrl(videoFile, baseUrlHttp), | |
199 | fileDownloadUrl: model.getVideoFileDownloadUrl(videoFile, baseUrlHttp), | |
200 | metadataUrl: video.getVideoFileMetadataUrl(videoFile, baseUrlHttp) | |
201 | } as VideoFile | |
202 | }) | |
203 | .sort((a, b) => { | |
204 | if (a.resolution.id < b.resolution.id) return 1 | |
205 | if (a.resolution.id === b.resolution.id) return 0 | |
206 | return -1 | |
207 | }) | |
208 | } | |
209 | ||
210 | function addVideoFilesInAPAcc ( | |
211 | acc: ActivityUrlObject[] | ActivityTagObject[], | |
212 | model: MVideoAP | MStreamingPlaylistVideo, | |
213 | baseUrlHttp: string, | |
214 | baseUrlWs: string, | |
215 | files: MVideoFile[] | |
216 | ) { | |
217 | for (const file of files) { | |
218 | acc.push({ | |
219 | type: 'Link', | |
220 | mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any, | |
221 | href: model.getVideoFileUrl(file, baseUrlHttp), | |
222 | height: file.resolution, | |
223 | size: file.size, | |
224 | fps: file.fps | |
225 | }) | |
226 | ||
227 | acc.push({ | |
228 | type: 'Link', | |
229 | rel: [ 'metadata', MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] ], | |
230 | mediaType: 'application/json' as 'application/json', | |
231 | href: extractVideo(model).getVideoFileMetadataUrl(file, baseUrlHttp), | |
232 | height: file.resolution, | |
233 | fps: file.fps | |
234 | }) | |
235 | ||
236 | acc.push({ | |
237 | type: 'Link', | |
238 | mediaType: 'application/x-bittorrent' as 'application/x-bittorrent', | |
239 | href: model.getTorrentUrl(file, baseUrlHttp), | |
240 | height: file.resolution | |
241 | }) | |
242 | ||
243 | acc.push({ | |
244 | type: 'Link', | |
245 | mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet', | |
246 | href: generateMagnetUri(model, file, baseUrlHttp, baseUrlWs), | |
247 | height: file.resolution | |
248 | }) | |
249 | } | |
250 | } | |
251 | ||
252 | function videoModelToActivityPubObject (video: MVideoAP): VideoTorrentObject { | |
253 | const { baseUrlHttp, baseUrlWs } = video.getBaseUrls() | |
254 | if (!video.Tags) video.Tags = [] | |
255 | ||
256 | const tag = video.Tags.map(t => ({ | |
257 | type: 'Hashtag' as 'Hashtag', | |
258 | name: t.name | |
259 | })) | |
260 | ||
261 | let language | |
262 | if (video.language) { | |
263 | language = { | |
264 | identifier: video.language, | |
265 | name: VideoModel.getLanguageLabel(video.language) | |
266 | } | |
267 | } | |
268 | ||
269 | let category | |
270 | if (video.category) { | |
271 | category = { | |
272 | identifier: video.category + '', | |
273 | name: VideoModel.getCategoryLabel(video.category) | |
274 | } | |
275 | } | |
276 | ||
277 | let licence | |
278 | if (video.licence) { | |
279 | licence = { | |
280 | identifier: video.licence + '', | |
281 | name: VideoModel.getLicenceLabel(video.licence) | |
282 | } | |
283 | } | |
284 | ||
285 | const url: ActivityUrlObject[] = [ | |
286 | // HTML url should be the first element in the array so Mastodon correctly displays the embed | |
287 | { | |
288 | type: 'Link', | |
289 | mediaType: 'text/html', | |
290 | href: WEBSERVER.URL + '/videos/watch/' + video.uuid | |
291 | } | |
292 | ] | |
293 | ||
294 | addVideoFilesInAPAcc(url, video, baseUrlHttp, baseUrlWs, video.VideoFiles || []) | |
295 | ||
296 | for (const playlist of (video.VideoStreamingPlaylists || [])) { | |
297 | const tag = playlist.p2pMediaLoaderInfohashes | |
298 | .map(i => ({ type: 'Infohash' as 'Infohash', name: i })) as ActivityTagObject[] | |
299 | tag.push({ | |
300 | type: 'Link', | |
301 | name: 'sha256', | |
302 | mediaType: 'application/json' as 'application/json', | |
303 | href: playlist.segmentsSha256Url | |
304 | }) | |
305 | ||
306 | const playlistWithVideo = Object.assign(playlist, { Video: video }) | |
307 | addVideoFilesInAPAcc(tag, playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles || []) | |
308 | ||
309 | url.push({ | |
310 | type: 'Link', | |
311 | mediaType: 'application/x-mpegURL' as 'application/x-mpegURL', | |
312 | href: playlist.playlistUrl, | |
313 | tag | |
314 | }) | |
315 | } | |
316 | ||
317 | const subtitleLanguage = [] | |
318 | for (const caption of video.VideoCaptions) { | |
319 | subtitleLanguage.push({ | |
320 | identifier: caption.language, | |
321 | name: VideoCaptionModel.getLanguageLabel(caption.language), | |
322 | url: caption.getFileUrl(video) | |
323 | }) | |
324 | } | |
325 | ||
326 | const icons = [ video.getMiniature(), video.getPreview() ] | |
327 | ||
328 | return { | |
329 | type: 'Video' as 'Video', | |
330 | id: video.url, | |
331 | name: video.name, | |
332 | duration: getActivityStreamDuration(video.duration), | |
333 | uuid: video.uuid, | |
334 | tag, | |
335 | category, | |
336 | licence, | |
337 | language, | |
338 | views: video.views, | |
339 | sensitive: video.nsfw, | |
340 | waitTranscoding: video.waitTranscoding, | |
341 | state: video.state, | |
342 | commentsEnabled: video.commentsEnabled, | |
343 | downloadEnabled: video.downloadEnabled, | |
344 | published: video.publishedAt.toISOString(), | |
345 | originallyPublishedAt: video.originallyPublishedAt ? video.originallyPublishedAt.toISOString() : null, | |
346 | updated: video.updatedAt.toISOString(), | |
347 | mediaType: 'text/markdown', | |
348 | content: video.getTruncatedDescription(), | |
349 | support: video.support, | |
350 | subtitleLanguage, | |
351 | icon: icons.map(i => ({ | |
352 | type: 'Image', | |
353 | url: i.getFileUrl(video), | |
354 | mediaType: 'image/jpeg', | |
355 | width: i.width, | |
356 | height: i.height | |
357 | })), | |
358 | url, | |
359 | likes: getVideoLikesActivityPubUrl(video), | |
360 | dislikes: getVideoDislikesActivityPubUrl(video), | |
361 | shares: getVideoSharesActivityPubUrl(video), | |
362 | comments: getVideoCommentsActivityPubUrl(video), | |
363 | attributedTo: [ | |
364 | { | |
365 | type: 'Person', | |
366 | id: video.VideoChannel.Account.Actor.url | |
367 | }, | |
368 | { | |
369 | type: 'Group', | |
370 | id: video.VideoChannel.Actor.url | |
371 | } | |
372 | ] | |
373 | } | |
374 | } | |
375 | ||
376 | function getActivityStreamDuration (duration: number) { | |
377 | // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration | |
378 | return 'PT' + duration + 'S' | |
379 | } | |
380 | ||
381 | export { | |
382 | videoModelToFormattedJSON, | |
383 | videoModelToFormattedDetailsJSON, | |
384 | videoFilesModelToFormattedJSON, | |
385 | videoModelToActivityPubObject, | |
386 | getActivityStreamDuration | |
387 | } |