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