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