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