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