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