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