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