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