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