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