]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/formatter/video-format-utils.ts
Move test functions outside extra-utils
[github/Chocobozzz/PeerTube.git] / server / models / video / formatter / video-format-utils.ts
1 import { generateMagnetUri } from '@server/helpers/webtorrent'
2 import { getLocalVideoFileMetadataUrl } from '@server/lib/video-urls'
3 import { VideoViews } from '@server/lib/video-views'
4 import { uuidToShort } from '@shared/core-utils'
5 import { VideoFile, VideosCommonQueryAfterSanitize } from '@shared/models'
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 if (video.isLive) {
125 videoObject.viewers = VideoViews.Instance.getViewers(video)
126 }
127
128 const add = options.additionalAttributes
129 if (add?.state === true) {
130 videoObject.state = {
131 id: video.state,
132 label: getStateLabel(video.state)
133 }
134 }
135
136 if (add?.waitTranscoding === true) {
137 videoObject.waitTranscoding = video.waitTranscoding
138 }
139
140 if (add?.scheduledUpdate === true && video.ScheduleVideoUpdate) {
141 videoObject.scheduledUpdate = {
142 updateAt: video.ScheduleVideoUpdate.updateAt,
143 privacy: video.ScheduleVideoUpdate.privacy || undefined
144 }
145 }
146
147 if (add?.blacklistInfo === true) {
148 videoObject.blacklisted = !!video.VideoBlacklist
149 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
150 }
151
152 if (add?.blockedOwner === true) {
153 videoObject.blockedOwner = video.VideoChannel.Account.isBlocked()
154
155 const server = video.VideoChannel.Account.Actor.Server as MServer
156 videoObject.blockedServer = !!(server?.isBlocked())
157 }
158
159 if (add?.files === true) {
160 videoObject.streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
161 videoObject.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
162 }
163
164 return videoObject
165 }
166
167 function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails {
168 const videoJSON = video.toFormattedJSON({
169 additionalAttributes: {
170 scheduledUpdate: true,
171 blacklistInfo: true,
172 files: true
173 }
174 }) as Video & Required<Pick<Video, 'files' | 'streamingPlaylists'>>
175
176 const tags = video.Tags ? video.Tags.map(t => t.name) : []
177
178 const detailsJSON = {
179 support: video.support,
180 descriptionPath: video.getDescriptionAPIPath(),
181 channel: video.VideoChannel.toFormattedJSON(),
182 account: video.VideoChannel.Account.toFormattedJSON(),
183 tags,
184 commentsEnabled: video.commentsEnabled,
185 downloadEnabled: video.downloadEnabled,
186 waitTranscoding: video.waitTranscoding,
187 state: {
188 id: video.state,
189 label: getStateLabel(video.state)
190 },
191
192 trackerUrls: video.getTrackerUrls()
193 }
194
195 return Object.assign(videoJSON, detailsJSON)
196 }
197
198 function streamingPlaylistsModelToFormattedJSON (
199 video: MVideoFormattable,
200 playlists: MStreamingPlaylistRedundanciesOpt[]
201 ): VideoStreamingPlaylist[] {
202 if (isArray(playlists) === false) return []
203
204 return playlists
205 .map(playlist => {
206 const redundancies = isArray(playlist.RedundancyVideos)
207 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
208 : []
209
210 const files = videoFilesModelToFormattedJSON(video, playlist.VideoFiles)
211
212 return {
213 id: playlist.id,
214 type: playlist.type,
215 playlistUrl: playlist.getMasterPlaylistUrl(video),
216 segmentsSha256Url: playlist.getSha256SegmentsUrl(video),
217 redundancies,
218 files
219 }
220 })
221 }
222
223 function sortByResolutionDesc (fileA: MVideoFile, fileB: MVideoFile) {
224 if (fileA.resolution < fileB.resolution) return 1
225 if (fileA.resolution === fileB.resolution) return 0
226 return -1
227 }
228
229 function videoFilesModelToFormattedJSON (
230 video: MVideoFormattable,
231 videoFiles: MVideoFileRedundanciesOpt[],
232 includeMagnet = true
233 ): VideoFile[] {
234 const trackerUrls = includeMagnet
235 ? video.getTrackerUrls()
236 : []
237
238 return (videoFiles || [])
239 .filter(f => !f.isLive())
240 .sort(sortByResolutionDesc)
241 .map(videoFile => {
242 return {
243 resolution: {
244 id: videoFile.resolution,
245 label: videoFile.resolution === 0 ? 'Audio' : `${videoFile.resolution}p`
246 },
247
248 magnetUri: includeMagnet && videoFile.hasTorrent()
249 ? generateMagnetUri(video, videoFile, trackerUrls)
250 : undefined,
251
252 size: videoFile.size,
253 fps: videoFile.fps,
254
255 torrentUrl: videoFile.getTorrentUrl(),
256 torrentDownloadUrl: videoFile.getTorrentDownloadUrl(),
257
258 fileUrl: videoFile.getFileUrl(video),
259 fileDownloadUrl: videoFile.getFileDownloadUrl(video),
260
261 metadataUrl: videoFile.metadataUrl ?? getLocalVideoFileMetadataUrl(video, videoFile)
262 } as VideoFile
263 })
264 }
265
266 function addVideoFilesInAPAcc (
267 acc: ActivityUrlObject[] | ActivityTagObject[],
268 video: MVideo,
269 files: MVideoFile[]
270 ) {
271 const trackerUrls = video.getTrackerUrls()
272
273 const sortedFiles = (files || [])
274 .filter(f => !f.isLive())
275 .sort(sortByResolutionDesc)
276
277 for (const file of sortedFiles) {
278 acc.push({
279 type: 'Link',
280 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any,
281 href: file.getFileUrl(video),
282 height: file.resolution,
283 size: file.size,
284 fps: file.fps
285 })
286
287 acc.push({
288 type: 'Link',
289 rel: [ 'metadata', MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] ],
290 mediaType: 'application/json' as 'application/json',
291 href: getLocalVideoFileMetadataUrl(video, file),
292 height: file.resolution,
293 fps: file.fps
294 })
295
296 if (file.hasTorrent()) {
297 acc.push({
298 type: 'Link',
299 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
300 href: file.getTorrentUrl(),
301 height: file.resolution
302 })
303
304 acc.push({
305 type: 'Link',
306 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
307 href: generateMagnetUri(video, file, trackerUrls),
308 height: file.resolution
309 })
310 }
311 }
312 }
313
314 function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
315 if (!video.Tags) video.Tags = []
316
317 const tag = video.Tags.map(t => ({
318 type: 'Hashtag' as 'Hashtag',
319 name: t.name
320 }))
321
322 let language
323 if (video.language) {
324 language = {
325 identifier: video.language,
326 name: getLanguageLabel(video.language)
327 }
328 }
329
330 let category
331 if (video.category) {
332 category = {
333 identifier: video.category + '',
334 name: getCategoryLabel(video.category)
335 }
336 }
337
338 let licence
339 if (video.licence) {
340 licence = {
341 identifier: video.licence + '',
342 name: getLicenceLabel(video.licence)
343 }
344 }
345
346 const url: ActivityUrlObject[] = [
347 // HTML url should be the first element in the array so Mastodon correctly displays the embed
348 {
349 type: 'Link',
350 mediaType: 'text/html',
351 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
352 }
353 ]
354
355 addVideoFilesInAPAcc(url, video, video.VideoFiles || [])
356
357 for (const playlist of (video.VideoStreamingPlaylists || [])) {
358 const tag = playlist.p2pMediaLoaderInfohashes
359 .map(i => ({ type: 'Infohash' as 'Infohash', name: i })) as ActivityTagObject[]
360 tag.push({
361 type: 'Link',
362 name: 'sha256',
363 mediaType: 'application/json' as 'application/json',
364 href: playlist.getSha256SegmentsUrl(video)
365 })
366
367 addVideoFilesInAPAcc(tag, video, playlist.VideoFiles || [])
368
369 url.push({
370 type: 'Link',
371 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
372 href: playlist.getMasterPlaylistUrl(video),
373 tag
374 })
375 }
376
377 for (const trackerUrl of video.getTrackerUrls()) {
378 const rel2 = trackerUrl.startsWith('http')
379 ? 'http'
380 : 'websocket'
381
382 url.push({
383 type: 'Link',
384 name: `tracker-${rel2}`,
385 rel: [ 'tracker', rel2 ],
386 href: trackerUrl
387 })
388 }
389
390 const subtitleLanguage = []
391 for (const caption of video.VideoCaptions) {
392 subtitleLanguage.push({
393 identifier: caption.language,
394 name: VideoCaptionModel.getLanguageLabel(caption.language),
395 url: caption.getFileUrl(video)
396 })
397 }
398
399 const icons = [ video.getMiniature(), video.getPreview() ]
400
401 return {
402 type: 'Video' as 'Video',
403 id: video.url,
404 name: video.name,
405 duration: getActivityStreamDuration(video.duration),
406 uuid: video.uuid,
407 tag,
408 category,
409 licence,
410 language,
411 views: video.views,
412 sensitive: video.nsfw,
413 waitTranscoding: video.waitTranscoding,
414 isLiveBroadcast: video.isLive,
415
416 liveSaveReplay: video.isLive
417 ? video.VideoLive.saveReplay
418 : null,
419
420 permanentLive: video.isLive
421 ? video.VideoLive.permanentLive
422 : null,
423
424 state: video.state,
425 commentsEnabled: video.commentsEnabled,
426 downloadEnabled: video.downloadEnabled,
427 published: video.publishedAt.toISOString(),
428
429 originallyPublishedAt: video.originallyPublishedAt
430 ? video.originallyPublishedAt.toISOString()
431 : null,
432
433 updated: video.updatedAt.toISOString(),
434 mediaType: 'text/markdown',
435 content: video.description,
436 support: video.support,
437 subtitleLanguage,
438 icon: icons.map(i => ({
439 type: 'Image',
440 url: i.getFileUrl(video),
441 mediaType: 'image/jpeg',
442 width: i.width,
443 height: i.height
444 })),
445 url,
446 likes: getLocalVideoLikesActivityPubUrl(video),
447 dislikes: getLocalVideoDislikesActivityPubUrl(video),
448 shares: getLocalVideoSharesActivityPubUrl(video),
449 comments: getLocalVideoCommentsActivityPubUrl(video),
450 attributedTo: [
451 {
452 type: 'Person',
453 id: video.VideoChannel.Account.Actor.url
454 },
455 {
456 type: 'Group',
457 id: video.VideoChannel.Actor.url
458 }
459 ]
460 }
461 }
462
463 function getActivityStreamDuration (duration: number) {
464 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
465 return 'PT' + duration + 'S'
466 }
467
468 function getCategoryLabel (id: number) {
469 return VIDEO_CATEGORIES[id] || 'Misc'
470 }
471
472 function getLicenceLabel (id: number) {
473 return VIDEO_LICENCES[id] || 'Unknown'
474 }
475
476 function getLanguageLabel (id: string) {
477 return VIDEO_LANGUAGES[id] || 'Unknown'
478 }
479
480 function getPrivacyLabel (id: number) {
481 return VIDEO_PRIVACIES[id] || 'Unknown'
482 }
483
484 function getStateLabel (id: number) {
485 return VIDEO_STATES[id] || 'Unknown'
486 }
487
488 export {
489 videoModelToFormattedJSON,
490 videoModelToFormattedDetailsJSON,
491 videoFilesModelToFormattedJSON,
492 videoModelToActivityPubObject,
493 getActivityStreamDuration,
494
495 guessAdditionalAttributesFromQuery,
496
497 getCategoryLabel,
498 getLicenceLabel,
499 getLanguageLabel,
500 getPrivacyLabel,
501 getStateLabel
502 }