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