]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/formatter/video-format-utils.ts
Add more tracing
[github/Chocobozzz/PeerTube.git] / server / models / video / formatter / video-format-utils.ts
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 resolution: {
260 id: videoFile.resolution,
261 label: videoFile.resolution === 0 ? 'Audio' : `${videoFile.resolution}p`
262 },
263
264 magnetUri: includeMagnet && videoFile.hasTorrent()
265 ? generateMagnetUri(video, videoFile, trackerUrls)
266 : undefined,
267
268 size: videoFile.size,
269 fps: videoFile.fps,
270
271 torrentUrl: videoFile.getTorrentUrl(),
272 torrentDownloadUrl: videoFile.getTorrentDownloadUrl(),
273
274 fileUrl: videoFile.getFileUrl(video),
275 fileDownloadUrl: videoFile.getFileDownloadUrl(video),
276
277 metadataUrl: videoFile.metadataUrl ?? getLocalVideoFileMetadataUrl(video, videoFile)
278 } as VideoFile
279 })
280 }
281
282 function addVideoFilesInAPAcc (
283 acc: ActivityUrlObject[] | ActivityTagObject[],
284 video: MVideo,
285 files: MVideoFile[]
286 ) {
287 const trackerUrls = video.getTrackerUrls()
288
289 const sortedFiles = (files || [])
290 .filter(f => !f.isLive())
291 .sort(sortByResolutionDesc)
292
293 for (const file of sortedFiles) {
294 acc.push({
295 type: 'Link',
296 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any,
297 href: file.getFileUrl(video),
298 height: file.resolution,
299 size: file.size,
300 fps: file.fps
301 })
302
303 acc.push({
304 type: 'Link',
305 rel: [ 'metadata', MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] ],
306 mediaType: 'application/json' as 'application/json',
307 href: getLocalVideoFileMetadataUrl(video, file),
308 height: file.resolution,
309 fps: file.fps
310 })
311
312 if (file.hasTorrent()) {
313 acc.push({
314 type: 'Link',
315 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
316 href: file.getTorrentUrl(),
317 height: file.resolution
318 })
319
320 acc.push({
321 type: 'Link',
322 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
323 href: generateMagnetUri(video, file, trackerUrls),
324 height: file.resolution
325 })
326 }
327 }
328 }
329
330 function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
331 if (!video.Tags) video.Tags = []
332
333 const tag = video.Tags.map(t => ({
334 type: 'Hashtag' as 'Hashtag',
335 name: t.name
336 }))
337
338 let language
339 if (video.language) {
340 language = {
341 identifier: video.language,
342 name: getLanguageLabel(video.language)
343 }
344 }
345
346 let category
347 if (video.category) {
348 category = {
349 identifier: video.category + '',
350 name: getCategoryLabel(video.category)
351 }
352 }
353
354 let licence
355 if (video.licence) {
356 licence = {
357 identifier: video.licence + '',
358 name: getLicenceLabel(video.licence)
359 }
360 }
361
362 const url: ActivityUrlObject[] = [
363 // HTML url should be the first element in the array so Mastodon correctly displays the embed
364 {
365 type: 'Link',
366 mediaType: 'text/html',
367 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
368 }
369 ]
370
371 addVideoFilesInAPAcc(url, video, video.VideoFiles || [])
372
373 for (const playlist of (video.VideoStreamingPlaylists || [])) {
374 const tag = playlist.p2pMediaLoaderInfohashes
375 .map(i => ({ type: 'Infohash' as 'Infohash', name: i })) as ActivityTagObject[]
376 tag.push({
377 type: 'Link',
378 name: 'sha256',
379 mediaType: 'application/json' as 'application/json',
380 href: playlist.getSha256SegmentsUrl(video)
381 })
382
383 addVideoFilesInAPAcc(tag, video, playlist.VideoFiles || [])
384
385 url.push({
386 type: 'Link',
387 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
388 href: playlist.getMasterPlaylistUrl(video),
389 tag
390 })
391 }
392
393 for (const trackerUrl of video.getTrackerUrls()) {
394 const rel2 = trackerUrl.startsWith('http')
395 ? 'http'
396 : 'websocket'
397
398 url.push({
399 type: 'Link',
400 name: `tracker-${rel2}`,
401 rel: [ 'tracker', rel2 ],
402 href: trackerUrl
403 })
404 }
405
406 const subtitleLanguage = []
407 for (const caption of video.VideoCaptions) {
408 subtitleLanguage.push({
409 identifier: caption.language,
410 name: VideoCaptionModel.getLanguageLabel(caption.language),
411 url: caption.getFileUrl(video)
412 })
413 }
414
415 const icons = [ video.getMiniature(), video.getPreview() ]
416
417 return {
418 type: 'Video' as 'Video',
419 id: video.url,
420 name: video.name,
421 duration: getActivityStreamDuration(video.duration),
422 uuid: video.uuid,
423 tag,
424 category,
425 licence,
426 language,
427 views: video.views,
428 sensitive: video.nsfw,
429 waitTranscoding: video.waitTranscoding,
430
431 state: video.state,
432 commentsEnabled: video.commentsEnabled,
433 downloadEnabled: video.downloadEnabled,
434 published: video.publishedAt.toISOString(),
435
436 originallyPublishedAt: video.originallyPublishedAt
437 ? video.originallyPublishedAt.toISOString()
438 : null,
439
440 updated: video.updatedAt.toISOString(),
441
442 mediaType: 'text/markdown',
443 content: video.description,
444 support: video.support,
445
446 subtitleLanguage,
447
448 icon: icons.map(i => ({
449 type: 'Image',
450 url: i.getFileUrl(video),
451 mediaType: 'image/jpeg',
452 width: i.width,
453 height: i.height
454 })),
455
456 url,
457
458 likes: getLocalVideoLikesActivityPubUrl(video),
459 dislikes: getLocalVideoDislikesActivityPubUrl(video),
460 shares: getLocalVideoSharesActivityPubUrl(video),
461 comments: getLocalVideoCommentsActivityPubUrl(video),
462
463 attributedTo: [
464 {
465 type: 'Person',
466 id: video.VideoChannel.Account.Actor.url
467 },
468 {
469 type: 'Group',
470 id: video.VideoChannel.Actor.url
471 }
472 ],
473
474 ...buildLiveAPAttributes(video)
475 }
476 }
477
478 function getCategoryLabel (id: number) {
479 return VIDEO_CATEGORIES[id] || 'Misc'
480 }
481
482 function getLicenceLabel (id: number) {
483 return VIDEO_LICENCES[id] || 'Unknown'
484 }
485
486 function getLanguageLabel (id: string) {
487 return VIDEO_LANGUAGES[id] || 'Unknown'
488 }
489
490 function getPrivacyLabel (id: number) {
491 return VIDEO_PRIVACIES[id] || 'Unknown'
492 }
493
494 function getStateLabel (id: number) {
495 return VIDEO_STATES[id] || 'Unknown'
496 }
497
498 export {
499 videoModelToFormattedJSON,
500 videoModelToFormattedDetailsJSON,
501 videoFilesModelToFormattedJSON,
502 videoModelToActivityPubObject,
503
504 guessAdditionalAttributesFromQuery,
505
506 getCategoryLabel,
507 getLicenceLabel,
508 getLanguageLabel,
509 getPrivacyLabel,
510 getStateLabel
511 }
512
513 // ---------------------------------------------------------------------------
514
515 function buildLiveAPAttributes (video: MVideoAP) {
516 if (!video.isLive) {
517 return {
518 isLiveBroadcast: false,
519 liveSaveReplay: null,
520 permanentLive: null,
521 latencyMode: null
522 }
523 }
524
525 return {
526 isLiveBroadcast: true,
527 liveSaveReplay: video.VideoLive.saveReplay,
528 permanentLive: video.VideoLive.permanentLive,
529 latencyMode: video.VideoLive.latencyMode
530 }
531 }