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