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