]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-format-utils.ts
21f0e0a68b7767f8544a4c0351d5959c149dcd4b
[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'
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/lib/videos'
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 videoFilesModelToFormattedJSON (
179 model: MVideo | MStreamingPlaylistVideo,
180 baseUrlHttp: string,
181 baseUrlWs: string,
182 videoFiles: MVideoFileRedundanciesOpt[]
183 ): VideoFile[] {
184 return videoFiles
185 .map(videoFile => {
186 return {
187 resolution: {
188 id: videoFile.resolution,
189 label: videoFile.resolution + 'p'
190 },
191 magnetUri: generateMagnetUri(model, videoFile, baseUrlHttp, baseUrlWs),
192 size: videoFile.size,
193 fps: videoFile.fps,
194 torrentUrl: model.getTorrentUrl(videoFile, baseUrlHttp),
195 torrentDownloadUrl: model.getTorrentDownloadUrl(videoFile, baseUrlHttp),
196 fileUrl: model.getVideoFileUrl(videoFile, baseUrlHttp),
197 fileDownloadUrl: model.getVideoFileDownloadUrl(videoFile, baseUrlHttp),
198 metadataUrl: videoFile.metadataUrl // only send the metadataUrl and not the metadata over the wire
199 } as VideoFile
200 })
201 .sort((a, b) => {
202 if (a.resolution.id < b.resolution.id) return 1
203 if (a.resolution.id === b.resolution.id) return 0
204 return -1
205 })
206 }
207
208 function addVideoFilesInAPAcc (
209 acc: ActivityUrlObject[] | ActivityTagObject[],
210 model: MVideoAP | MStreamingPlaylistVideo,
211 baseUrlHttp: string,
212 baseUrlWs: string,
213 files: MVideoFile[]
214 ) {
215 for (const file of files) {
216 acc.push({
217 type: 'Link',
218 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any,
219 href: model.getVideoFileUrl(file, baseUrlHttp),
220 height: file.resolution,
221 size: file.size,
222 fps: file.fps
223 })
224
225 acc.push({
226 type: 'Link',
227 rel: [ 'metadata', MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] ],
228 mediaType: 'application/json' as 'application/json',
229 href: extractVideo(model).getVideoFileMetadataUrl(file, baseUrlHttp),
230 height: file.resolution,
231 fps: file.fps
232 })
233
234 acc.push({
235 type: 'Link',
236 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
237 href: model.getTorrentUrl(file, baseUrlHttp),
238 height: file.resolution
239 })
240
241 acc.push({
242 type: 'Link',
243 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
244 href: generateMagnetUri(model, file, baseUrlHttp, baseUrlWs),
245 height: file.resolution
246 })
247 }
248 }
249
250 function videoModelToActivityPubObject (video: MVideoAP): VideoTorrentObject {
251 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
252 if (!video.Tags) video.Tags = []
253
254 const tag = video.Tags.map(t => ({
255 type: 'Hashtag' as 'Hashtag',
256 name: t.name
257 }))
258
259 let language
260 if (video.language) {
261 language = {
262 identifier: video.language,
263 name: VideoModel.getLanguageLabel(video.language)
264 }
265 }
266
267 let category
268 if (video.category) {
269 category = {
270 identifier: video.category + '',
271 name: VideoModel.getCategoryLabel(video.category)
272 }
273 }
274
275 let licence
276 if (video.licence) {
277 licence = {
278 identifier: video.licence + '',
279 name: VideoModel.getLicenceLabel(video.licence)
280 }
281 }
282
283 const url: ActivityUrlObject[] = [
284 // HTML url should be the first element in the array so Mastodon correctly displays the embed
285 {
286 type: 'Link',
287 mediaType: 'text/html',
288 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
289 }
290 ]
291
292 addVideoFilesInAPAcc(url, video, baseUrlHttp, baseUrlWs, video.VideoFiles || [])
293
294 for (const playlist of (video.VideoStreamingPlaylists || [])) {
295 const tag = playlist.p2pMediaLoaderInfohashes
296 .map(i => ({ type: 'Infohash' as 'Infohash', name: i })) as ActivityTagObject[]
297 tag.push({
298 type: 'Link',
299 name: 'sha256',
300 mediaType: 'application/json' as 'application/json',
301 href: playlist.segmentsSha256Url
302 })
303
304 const playlistWithVideo = Object.assign(playlist, { Video: video })
305 addVideoFilesInAPAcc(tag, playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles || [])
306
307 url.push({
308 type: 'Link',
309 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
310 href: playlist.playlistUrl,
311 tag
312 })
313 }
314
315 const subtitleLanguage = []
316 for (const caption of video.VideoCaptions) {
317 subtitleLanguage.push({
318 identifier: caption.language,
319 name: VideoCaptionModel.getLanguageLabel(caption.language),
320 url: caption.getFileUrl(video)
321 })
322 }
323
324 const icons = [ video.getMiniature(), video.getPreview() ]
325
326 return {
327 type: 'Video' as 'Video',
328 id: video.url,
329 name: video.name,
330 duration: getActivityStreamDuration(video.duration),
331 uuid: video.uuid,
332 tag,
333 category,
334 licence,
335 language,
336 views: video.views,
337 sensitive: video.nsfw,
338 waitTranscoding: video.waitTranscoding,
339 state: video.state,
340 commentsEnabled: video.commentsEnabled,
341 downloadEnabled: video.downloadEnabled,
342 published: video.publishedAt.toISOString(),
343 originallyPublishedAt: video.originallyPublishedAt ? video.originallyPublishedAt.toISOString() : null,
344 updated: video.updatedAt.toISOString(),
345 mediaType: 'text/markdown',
346 content: video.getTruncatedDescription(),
347 support: video.support,
348 subtitleLanguage,
349 icon: icons.map(i => ({
350 type: 'Image',
351 url: i.getFileUrl(video),
352 mediaType: 'image/jpeg',
353 width: i.width,
354 height: i.height
355 })),
356 url,
357 likes: getVideoLikesActivityPubUrl(video),
358 dislikes: getVideoDislikesActivityPubUrl(video),
359 shares: getVideoSharesActivityPubUrl(video),
360 comments: getVideoCommentsActivityPubUrl(video),
361 attributedTo: [
362 {
363 type: 'Person',
364 id: video.VideoChannel.Account.Actor.url
365 },
366 {
367 type: 'Group',
368 id: video.VideoChannel.Actor.url
369 }
370 ]
371 }
372 }
373
374 function getActivityStreamDuration (duration: number) {
375 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
376 return 'PT' + duration + 'S'
377 }
378
379 export {
380 videoModelToFormattedJSON,
381 videoModelToFormattedDetailsJSON,
382 videoFilesModelToFormattedJSON,
383 videoModelToActivityPubObject,
384 getActivityStreamDuration
385 }