]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-format-utils.ts
Merge remote-tracking branch 'weblate/develop' into develop
[github/Chocobozzz/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 '../../types/models'
23 import { MVideoFileRedundanciesOpt } from '../../types/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 // Can be added by external plugins
84 pluginData: (video as any).pluginData
85 }
86
87 if (options) {
88 if (options.additionalAttributes.state === true) {
89 videoObject.state = {
90 id: video.state,
91 label: VideoModel.getStateLabel(video.state)
92 }
93 }
94
95 if (options.additionalAttributes.waitTranscoding === true) {
96 videoObject.waitTranscoding = video.waitTranscoding
97 }
98
99 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
100 videoObject.scheduledUpdate = {
101 updateAt: video.ScheduleVideoUpdate.updateAt,
102 privacy: video.ScheduleVideoUpdate.privacy || undefined
103 }
104 }
105
106 if (options.additionalAttributes.blacklistInfo === true) {
107 videoObject.blacklisted = !!video.VideoBlacklist
108 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
109 }
110 }
111
112 return videoObject
113 }
114
115 function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails {
116 const formattedJson = video.toFormattedJSON({
117 additionalAttributes: {
118 scheduledUpdate: true,
119 blacklistInfo: true
120 }
121 })
122
123 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
124
125 const tags = video.Tags ? video.Tags.map(t => t.name) : []
126
127 const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
128
129 const detailsJson = {
130 support: video.support,
131 descriptionPath: video.getDescriptionAPIPath(),
132 channel: video.VideoChannel.toFormattedJSON(),
133 account: video.VideoChannel.Account.toFormattedJSON(),
134 tags,
135 commentsEnabled: video.commentsEnabled,
136 downloadEnabled: video.downloadEnabled,
137 waitTranscoding: video.waitTranscoding,
138 state: {
139 id: video.state,
140 label: VideoModel.getStateLabel(video.state)
141 },
142
143 trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs),
144
145 files: [],
146 streamingPlaylists
147 }
148
149 // Format and sort video files
150 detailsJson.files = videoFilesModelToFormattedJSON(video, baseUrlHttp, baseUrlWs, video.VideoFiles)
151
152 return Object.assign(formattedJson, detailsJson)
153 }
154
155 function streamingPlaylistsModelToFormattedJSON (video: MVideo, playlists: MStreamingPlaylistRedundanciesOpt[]): VideoStreamingPlaylist[] {
156 if (isArray(playlists) === false) return []
157
158 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
159
160 return playlists
161 .map(playlist => {
162 const playlistWithVideo = Object.assign(playlist, { Video: video })
163
164 const redundancies = isArray(playlist.RedundancyVideos)
165 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
166 : []
167
168 const files = videoFilesModelToFormattedJSON(playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles)
169
170 return {
171 id: playlist.id,
172 type: playlist.type,
173 playlistUrl: playlist.playlistUrl,
174 segmentsSha256Url: playlist.segmentsSha256Url,
175 redundancies,
176 files
177 }
178 })
179 }
180
181 function sortByResolutionDesc (fileA: MVideoFile, fileB: MVideoFile) {
182 if (fileA.resolution < fileB.resolution) return 1
183 if (fileA.resolution === fileB.resolution) return 0
184 return -1
185 }
186
187 function videoFilesModelToFormattedJSON (
188 model: MVideo | MStreamingPlaylistVideo,
189 baseUrlHttp: string,
190 baseUrlWs: string,
191 videoFiles: MVideoFileRedundanciesOpt[]
192 ): VideoFile[] {
193 const video = extractVideo(model)
194
195 return [ ...videoFiles ]
196 .sort(sortByResolutionDesc)
197 .map(videoFile => {
198 return {
199 resolution: {
200 id: videoFile.resolution,
201 label: videoFile.resolution + 'p'
202 },
203 magnetUri: generateMagnetUri(model, videoFile, baseUrlHttp, baseUrlWs),
204 size: videoFile.size,
205 fps: videoFile.fps,
206 torrentUrl: model.getTorrentUrl(videoFile, baseUrlHttp),
207 torrentDownloadUrl: model.getTorrentDownloadUrl(videoFile, baseUrlHttp),
208 fileUrl: model.getVideoFileUrl(videoFile, baseUrlHttp),
209 fileDownloadUrl: model.getVideoFileDownloadUrl(videoFile, baseUrlHttp),
210 metadataUrl: video.getVideoFileMetadataUrl(videoFile, baseUrlHttp)
211 } as VideoFile
212 })
213 }
214
215 function addVideoFilesInAPAcc (
216 acc: ActivityUrlObject[] | ActivityTagObject[],
217 model: MVideoAP | MStreamingPlaylistVideo,
218 baseUrlHttp: string,
219 baseUrlWs: string,
220 files: MVideoFile[]
221 ) {
222 const sortedFiles = [ ...files ].sort(sortByResolutionDesc)
223
224 for (const file of sortedFiles) {
225 acc.push({
226 type: 'Link',
227 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any,
228 href: model.getVideoFileUrl(file, baseUrlHttp),
229 height: file.resolution,
230 size: file.size,
231 fps: file.fps
232 })
233
234 acc.push({
235 type: 'Link',
236 rel: [ 'metadata', MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] ],
237 mediaType: 'application/json' as 'application/json',
238 href: extractVideo(model).getVideoFileMetadataUrl(file, baseUrlHttp),
239 height: file.resolution,
240 fps: file.fps
241 })
242
243 acc.push({
244 type: 'Link',
245 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
246 href: model.getTorrentUrl(file, baseUrlHttp),
247 height: file.resolution
248 })
249
250 acc.push({
251 type: 'Link',
252 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
253 href: generateMagnetUri(model, file, baseUrlHttp, baseUrlWs),
254 height: file.resolution
255 })
256 }
257 }
258
259 function videoModelToActivityPubObject (video: MVideoAP): VideoTorrentObject {
260 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
261 if (!video.Tags) video.Tags = []
262
263 const tag = video.Tags.map(t => ({
264 type: 'Hashtag' as 'Hashtag',
265 name: t.name
266 }))
267
268 let language
269 if (video.language) {
270 language = {
271 identifier: video.language,
272 name: VideoModel.getLanguageLabel(video.language)
273 }
274 }
275
276 let category
277 if (video.category) {
278 category = {
279 identifier: video.category + '',
280 name: VideoModel.getCategoryLabel(video.category)
281 }
282 }
283
284 let licence
285 if (video.licence) {
286 licence = {
287 identifier: video.licence + '',
288 name: VideoModel.getLicenceLabel(video.licence)
289 }
290 }
291
292 const url: ActivityUrlObject[] = [
293 // HTML url should be the first element in the array so Mastodon correctly displays the embed
294 {
295 type: 'Link',
296 mediaType: 'text/html',
297 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
298 }
299 ]
300
301 addVideoFilesInAPAcc(url, video, baseUrlHttp, baseUrlWs, video.VideoFiles || [])
302
303 for (const playlist of (video.VideoStreamingPlaylists || [])) {
304 const tag = playlist.p2pMediaLoaderInfohashes
305 .map(i => ({ type: 'Infohash' as 'Infohash', name: i })) as ActivityTagObject[]
306 tag.push({
307 type: 'Link',
308 name: 'sha256',
309 mediaType: 'application/json' as 'application/json',
310 href: playlist.segmentsSha256Url
311 })
312
313 const playlistWithVideo = Object.assign(playlist, { Video: video })
314 addVideoFilesInAPAcc(tag, playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles || [])
315
316 url.push({
317 type: 'Link',
318 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
319 href: playlist.playlistUrl,
320 tag
321 })
322 }
323
324 const subtitleLanguage = []
325 for (const caption of video.VideoCaptions) {
326 subtitleLanguage.push({
327 identifier: caption.language,
328 name: VideoCaptionModel.getLanguageLabel(caption.language),
329 url: caption.getFileUrl(video)
330 })
331 }
332
333 const icons = [ video.getMiniature(), video.getPreview() ]
334
335 return {
336 type: 'Video' as 'Video',
337 id: video.url,
338 name: video.name,
339 duration: getActivityStreamDuration(video.duration),
340 uuid: video.uuid,
341 tag,
342 category,
343 licence,
344 language,
345 views: video.views,
346 sensitive: video.nsfw,
347 waitTranscoding: video.waitTranscoding,
348 state: video.state,
349 commentsEnabled: video.commentsEnabled,
350 downloadEnabled: video.downloadEnabled,
351 published: video.publishedAt.toISOString(),
352 originallyPublishedAt: video.originallyPublishedAt ? video.originallyPublishedAt.toISOString() : null,
353 updated: video.updatedAt.toISOString(),
354 mediaType: 'text/markdown',
355 content: video.description,
356 support: video.support,
357 subtitleLanguage,
358 icon: icons.map(i => ({
359 type: 'Image',
360 url: i.getFileUrl(video),
361 mediaType: 'image/jpeg',
362 width: i.width,
363 height: i.height
364 })),
365 url,
366 likes: getVideoLikesActivityPubUrl(video),
367 dislikes: getVideoDislikesActivityPubUrl(video),
368 shares: getVideoSharesActivityPubUrl(video),
369 comments: getVideoCommentsActivityPubUrl(video),
370 attributedTo: [
371 {
372 type: 'Person',
373 id: video.VideoChannel.Account.Actor.url
374 },
375 {
376 type: 'Group',
377 id: video.VideoChannel.Actor.url
378 }
379 ]
380 }
381 }
382
383 function getActivityStreamDuration (duration: number) {
384 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
385 return 'PT' + duration + 'S'
386 }
387
388 export {
389 videoModelToFormattedJSON,
390 videoModelToFormattedDetailsJSON,
391 videoFilesModelToFormattedJSON,
392 videoModelToActivityPubObject,
393 getActivityStreamDuration
394 }