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