]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/models/video/formatter/video-format-utils.ts
Prevent error on highlighted thread
[github/Chocobozzz/PeerTube.git] / server / models / video / formatter / video-format-utils.ts
CommitLineData
90a8bd30 1import { generateMagnetUri } from '@server/helpers/webtorrent'
b2111066 2import { getActivityStreamDuration } from '@server/lib/activitypub/activity'
ce6b3765 3import { tracer } from '@server/lib/opentelemetry/tracing'
0305db28 4import { getLocalVideoFileMetadataUrl } from '@server/lib/video-urls'
b2111066 5import { VideoViewsManager } from '@server/lib/views/video-views-manager'
0628157f 6import { uuidToShort } from '@shared/extra-utils'
b2111066
C
7import {
8 ActivityTagObject,
9 ActivityUrlObject,
10 Video,
11 VideoDetails,
12 VideoFile,
13 VideoInclude,
14 VideoObject,
15 VideosCommonQueryAfterSanitize,
16 VideoStreamingPlaylist
17} from '@shared/models'
e5dbd508 18import { isArray } from '../../../helpers/custom-validators/misc'
7c3a6636
C
19import {
20 MIMETYPES,
21 VIDEO_CATEGORIES,
22 VIDEO_LANGUAGES,
23 VIDEO_LICENCES,
24 VIDEO_PRIVACIES,
25 VIDEO_STATES,
26 WEBSERVER
27} from '../../../initializers/constants'
098eb377 28import {
de94ac86
C
29 getLocalVideoCommentsActivityPubUrl,
30 getLocalVideoDislikesActivityPubUrl,
31 getLocalVideoLikesActivityPubUrl,
32 getLocalVideoSharesActivityPubUrl
e5dbd508 33} from '../../../lib/activitypub/url'
d7a25329 34import {
2760b454 35 MServer,
d7a25329 36 MStreamingPlaylistRedundanciesOpt,
8efc27bf 37 MVideo,
d7a25329
C
38 MVideoAP,
39 MVideoFile,
40 MVideoFormattable,
8efc27bf 41 MVideoFormattableDetails
e5dbd508
C
42} from '../../../types/models'
43import { MVideoFileRedundanciesOpt } from '../../../types/models/video/video-file'
e5dbd508 44import { VideoCaptionModel } from '../video-caption'
098eb377
C
45
46export type VideoFormattingJSONOptions = {
c39e86b8 47 completeDescription?: boolean
2760b454
C
48
49 additionalAttributes?: {
a1587156
C
50 state?: boolean
51 waitTranscoding?: boolean
52 scheduledUpdate?: boolean
098eb377 53 blacklistInfo?: boolean
3c10840f 54 files?: boolean
2760b454 55 blockedOwner?: boolean
098eb377
C
56 }
57}
a1587156 58
2760b454
C
59function 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),
3c10840f 68 files: !!(query.include & VideoInclude.FILES),
2760b454
C
69 blockedOwner: !!(query.include & VideoInclude.BLOCKED_OWNER)
70 }
71 }
72}
73
74function videoModelToFormattedJSON (video: MVideoFormattable, options: VideoFormattingJSONOptions = {}): Video {
ce6b3765
C
75 const span = tracer.startSpan('peertube.VideoModel.toFormattedJSON')
76
6e46de09
C
77 const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined
78
098eb377
C
79 const videoObject: Video = {
80 id: video.id,
81 uuid: video.uuid,
d4a8e7a6
C
82 shortUUID: uuidToShort(video.uuid),
83
ab4001aa
C
84 url: video.url,
85
098eb377
C
86 name: video.name,
87 category: {
88 id: video.category,
7c3a6636 89 label: getCategoryLabel(video.category)
098eb377
C
90 },
91 licence: {
92 id: video.licence,
7c3a6636 93 label: getLicenceLabel(video.licence)
098eb377
C
94 },
95 language: {
96 id: video.language,
7c3a6636 97 label: getLanguageLabel(video.language)
098eb377
C
98 },
99 privacy: {
100 id: video.privacy,
7c3a6636 101 label: getPrivacyLabel(video.privacy)
098eb377
C
102 },
103 nsfw: video.nsfw,
97816649
C
104
105 description: options && options.completeDescription === true
106 ? video.description
107 : video.getTruncatedDescription(),
108
098eb377
C
109 isLocal: video.isOwned(),
110 duration: video.duration,
b2111066 111
098eb377 112 views: video.views,
b2111066
C
113 viewers: VideoViewsManager.Instance.getViewers(video),
114
098eb377
C
115 likes: video.likes,
116 dislikes: video.dislikes,
3acc5084 117 thumbnailPath: video.getMiniatureStaticPath(),
098eb377
C
118 previewPath: video.getPreviewStaticPath(),
119 embedPath: video.getEmbedStaticPath(),
120 createdAt: video.createdAt,
121 updatedAt: video.updatedAt,
122 publishedAt: video.publishedAt,
c8034165 123 originallyPublishedAt: video.originallyPublishedAt,
418d092a 124
c6c0fa6c
C
125 isLive: video.isLive,
126
418d092a
C
127 account: video.VideoChannel.Account.toFormattedSummaryJSON(),
128 channel: video.VideoChannel.toFormattedSummaryJSON(),
6e46de09 129
ba5a8d89
C
130 userHistory: userHistory
131 ? { currentTime: userHistory.currentTime }
132 : undefined,
7294aab0
C
133
134 // Can be added by external plugins
135 pluginData: (video as any).pluginData
098eb377
C
136 }
137
2760b454
C
138 const add = options.additionalAttributes
139 if (add?.state === true) {
140 videoObject.state = {
141 id: video.state,
142 label: getStateLabel(video.state)
098eb377 143 }
2760b454 144 }
098eb377 145
2760b454
C
146 if (add?.waitTranscoding === true) {
147 videoObject.waitTranscoding = video.waitTranscoding
148 }
098eb377 149
2760b454
C
150 if (add?.scheduledUpdate === true && video.ScheduleVideoUpdate) {
151 videoObject.scheduledUpdate = {
152 updateAt: video.ScheduleVideoUpdate.updateAt,
153 privacy: video.ScheduleVideoUpdate.privacy || undefined
098eb377 154 }
2760b454 155 }
098eb377 156
2760b454
C
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())
098eb377
C
167 }
168
3c10840f
C
169 if (add?.files === true) {
170 videoObject.streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
171 videoObject.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
172 }
173
ce6b3765
C
174 span.end()
175
098eb377
C
176 return videoObject
177}
178
1ca9f7c3 179function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails {
ce6b3765
C
180 const span = tracer.startSpan('peertube.VideoModel.toFormattedDetailsJSON')
181
3c10840f 182 const videoJSON = video.toFormattedJSON({
098eb377
C
183 additionalAttributes: {
184 scheduledUpdate: true,
3c10840f
C
185 blacklistInfo: true,
186 files: true
098eb377 187 }
3c10840f 188 }) as Video & Required<Pick<Video, 'files' | 'streamingPlaylists'>>
098eb377 189
96f29c0f 190 const tags = video.Tags ? video.Tags.map(t => t.name) : []
09209296 191
3c10840f 192 const detailsJSON = {
098eb377 193 support: video.support,
96f29c0f 194 descriptionPath: video.getDescriptionAPIPath(),
098eb377
C
195 channel: video.VideoChannel.toFormattedJSON(),
196 account: video.VideoChannel.Account.toFormattedJSON(),
96f29c0f 197 tags,
098eb377 198 commentsEnabled: video.commentsEnabled,
7f2cfe3a 199 downloadEnabled: video.downloadEnabled,
098eb377
C
200 waitTranscoding: video.waitTranscoding,
201 state: {
202 id: video.state,
7c3a6636 203 label: getStateLabel(video.state)
098eb377 204 },
09209296 205
3c10840f 206 trackerUrls: video.getTrackerUrls()
098eb377
C
207 }
208
ce6b3765
C
209 span.end()
210
3c10840f 211 return Object.assign(videoJSON, detailsJSON)
098eb377
C
212}
213
90a8bd30 214function streamingPlaylistsModelToFormattedJSON (
3c10840f 215 video: MVideoFormattable,
90a8bd30
C
216 playlists: MStreamingPlaylistRedundanciesOpt[]
217): VideoStreamingPlaylist[] {
09209296
C
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
d9a2a031 226 const files = videoFilesModelToFormattedJSON(video, playlist.VideoFiles)
d7a25329 227
09209296
C
228 return {
229 id: playlist.id,
230 type: playlist.type,
764b1a14
C
231 playlistUrl: playlist.getMasterPlaylistUrl(video),
232 segmentsSha256Url: playlist.getSha256SegmentsUrl(video),
d7a25329
C
233 redundancies,
234 files
235 }
09209296
C
236 })
237}
238
5072b909
C
239function 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
d7a25329 245function videoFilesModelToFormattedJSON (
3c10840f 246 video: MVideoFormattable,
f66db4d5
C
247 videoFiles: MVideoFileRedundanciesOpt[],
248 includeMagnet = true
d7a25329 249): VideoFile[] {
f66db4d5
C
250 const trackerUrls = includeMagnet
251 ? video.getTrackerUrls()
252 : []
d9a2a031 253
cd162f25 254 return (videoFiles || [])
bd54ad19 255 .filter(f => !f.isLive())
5072b909 256 .sort(sortByResolutionDesc)
098eb377 257 .map(videoFile => {
098eb377
C
258 return {
259 resolution: {
260 id: videoFile.resolution,
2a408c40 261 label: videoFile.resolution === 0 ? 'Audio' : `${videoFile.resolution}p`
098eb377 262 },
90a8bd30 263
c4d12552 264 magnetUri: includeMagnet && videoFile.hasTorrent()
f66db4d5
C
265 ? generateMagnetUri(video, videoFile, trackerUrls)
266 : undefined,
90a8bd30 267
098eb377
C
268 size: videoFile.size,
269 fps: videoFile.fps,
90a8bd30
C
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)
098eb377
C
278 } as VideoFile
279 })
098eb377
C
280}
281
d7a25329
C
282function addVideoFilesInAPAcc (
283 acc: ActivityUrlObject[] | ActivityTagObject[],
8efc27bf 284 video: MVideo,
d7a25329
C
285 files: MVideoFile[]
286) {
d9a2a031
C
287 const trackerUrls = video.getTrackerUrls()
288
cd162f25 289 const sortedFiles = (files || [])
bd54ad19
C
290 .filter(f => !f.isLive())
291 .sort(sortByResolutionDesc)
5072b909
C
292
293 for (const file of sortedFiles) {
d7a25329
C
294 acc.push({
295 type: 'Link',
a1587156 296 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any,
90a8bd30 297 href: file.getFileUrl(video),
d7a25329
C
298 height: file.resolution,
299 size: file.size,
300 fps: file.fps
301 })
302
8319d6ae
RK
303 acc.push({
304 type: 'Link',
305 rel: [ 'metadata', MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] ],
306 mediaType: 'application/json' as 'application/json',
90a8bd30 307 href: getLocalVideoFileMetadataUrl(video, file),
8319d6ae
RK
308 height: file.resolution,
309 fps: file.fps
310 })
311
c4d12552
C
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 }
d7a25329
C
327 }
328}
329
de6310b2 330function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
098eb377
C
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,
7c3a6636 342 name: getLanguageLabel(video.language)
098eb377
C
343 }
344 }
345
346 let category
347 if (video.category) {
348 category = {
349 identifier: video.category + '',
7c3a6636 350 name: getCategoryLabel(video.category)
098eb377
C
351 }
352 }
353
354 let licence
355 if (video.licence) {
356 licence = {
357 identifier: video.licence + '',
7c3a6636 358 name: getLicenceLabel(video.licence)
098eb377
C
359 }
360 }
361
22f18a4a
C
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
d9a2a031 371 addVideoFilesInAPAcc(url, video, video.VideoFiles || [])
098eb377 372
09209296 373 for (const playlist of (video.VideoStreamingPlaylists || [])) {
a1587156
C
374 const tag = playlist.p2pMediaLoaderInfohashes
375 .map(i => ({ type: 'Infohash' as 'Infohash', name: i })) as ActivityTagObject[]
09209296
C
376 tag.push({
377 type: 'Link',
378 name: 'sha256',
09209296 379 mediaType: 'application/json' as 'application/json',
764b1a14 380 href: playlist.getSha256SegmentsUrl(video)
09209296
C
381 })
382
d9a2a031 383 addVideoFilesInAPAcc(tag, video, playlist.VideoFiles || [])
d7a25329 384
09209296
C
385 url.push({
386 type: 'Link',
09209296 387 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
764b1a14 388 href: playlist.getMasterPlaylistUrl(video),
09209296
C
389 tag
390 })
391 }
392
d9a2a031
C
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
098eb377
C
406 const subtitleLanguage = []
407 for (const caption of video.VideoCaptions) {
408 subtitleLanguage.push({
409 identifier: caption.language,
ca6d3622
C
410 name: VideoCaptionModel.getLanguageLabel(caption.language),
411 url: caption.getFileUrl(video)
098eb377
C
412 })
413 }
414
4282dafc 415 const icons = [ video.getMiniature(), video.getPreview() ]
3acc5084 416
098eb377
C
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,
bb4ba6d9 430
098eb377
C
431 state: video.state,
432 commentsEnabled: video.commentsEnabled,
7f2cfe3a 433 downloadEnabled: video.downloadEnabled,
098eb377 434 published: video.publishedAt.toISOString(),
af4ae64f
C
435
436 originallyPublishedAt: video.originallyPublishedAt
437 ? video.originallyPublishedAt.toISOString()
438 : null,
439
098eb377 440 updated: video.updatedAt.toISOString(),
f443a746 441
098eb377 442 mediaType: 'text/markdown',
5cb9f0f4 443 content: video.description,
098eb377 444 support: video.support,
f443a746 445
098eb377 446 subtitleLanguage,
f443a746 447
4282dafc 448 icon: icons.map(i => ({
098eb377 449 type: 'Image',
4282dafc 450 url: i.getFileUrl(video),
098eb377 451 mediaType: 'image/jpeg',
4282dafc
C
452 width: i.width,
453 height: i.height
454 })),
f443a746 455
098eb377 456 url,
f443a746 457
de94ac86
C
458 likes: getLocalVideoLikesActivityPubUrl(video),
459 dislikes: getLocalVideoDislikesActivityPubUrl(video),
460 shares: getLocalVideoSharesActivityPubUrl(video),
461 comments: getLocalVideoCommentsActivityPubUrl(video),
f443a746 462
098eb377
C
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 }
f443a746
C
472 ],
473
474 ...buildLiveAPAttributes(video)
098eb377
C
475 }
476}
477
7c3a6636
C
478function getCategoryLabel (id: number) {
479 return VIDEO_CATEGORIES[id] || 'Misc'
480}
481
482function getLicenceLabel (id: number) {
483 return VIDEO_LICENCES[id] || 'Unknown'
484}
485
486function getLanguageLabel (id: string) {
487 return VIDEO_LANGUAGES[id] || 'Unknown'
488}
489
490function getPrivacyLabel (id: number) {
491 return VIDEO_PRIVACIES[id] || 'Unknown'
492}
493
494function getStateLabel (id: number) {
495 return VIDEO_STATES[id] || 'Unknown'
496}
497
098eb377
C
498export {
499 videoModelToFormattedJSON,
500 videoModelToFormattedDetailsJSON,
501 videoFilesModelToFormattedJSON,
502 videoModelToActivityPubObject,
7c3a6636 503
2760b454
C
504 guessAdditionalAttributesFromQuery,
505
7c3a6636
C
506 getCategoryLabel,
507 getLicenceLabel,
508 getLanguageLabel,
509 getPrivacyLabel,
510 getStateLabel
098eb377 511}
f443a746
C
512
513// ---------------------------------------------------------------------------
514
515function 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}