1 import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos'
2 import { VideoModel } from './video'
3 import { VideoFileModel } from './video-file'
5 ActivityPlaylistInfohashesObject,
6 ActivityPlaylistSegmentHashesObject,
9 } from '../../../shared/models/activitypub/objects'
10 import { MIMETYPES, WEBSERVER } from '../../initializers/constants'
11 import { VideoCaptionModel } from './video-caption'
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'
22 export type VideoFormattingJSONOptions = {
23 completeDescription?: boolean
24 additionalAttributes: {
26 waitTranscoding?: boolean,
27 scheduledUpdate?: boolean,
28 blacklistInfo?: boolean
31 function videoModelToFormattedJSON (video: VideoModel, options?: VideoFormattingJSONOptions): Video {
32 const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined
34 const videoObject: Video = {
40 label: VideoModel.getCategoryLabel(video.category)
44 label: VideoModel.getLicenceLabel(video.licence)
48 label: VideoModel.getLanguageLabel(video.language)
52 label: VideoModel.getPrivacyLabel(video.privacy)
55 description: options && options.completeDescription === true ? video.description : video.getTruncatedDescription(),
56 isLocal: video.isOwned(),
57 duration: video.duration,
60 dislikes: video.dislikes,
61 thumbnailPath: video.getMiniatureStaticPath(),
62 previewPath: video.getPreviewStaticPath(),
63 embedPath: video.getEmbedStaticPath(),
64 createdAt: video.createdAt,
65 updatedAt: video.updatedAt,
66 publishedAt: video.publishedAt,
67 originallyPublishedAt: video.originallyPublishedAt,
69 account: video.VideoChannel.Account.toFormattedSummaryJSON(),
70 channel: video.VideoChannel.toFormattedSummaryJSON(),
72 userHistory: userHistory ? {
73 currentTime: userHistory.currentTime
78 if (options.additionalAttributes.state === true) {
81 label: VideoModel.getStateLabel(video.state)
85 if (options.additionalAttributes.waitTranscoding === true) {
86 videoObject.waitTranscoding = video.waitTranscoding
89 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
90 videoObject.scheduledUpdate = {
91 updateAt: video.ScheduleVideoUpdate.updateAt,
92 privacy: video.ScheduleVideoUpdate.privacy || undefined
96 if (options.additionalAttributes.blacklistInfo === true) {
97 videoObject.blacklisted = !!video.VideoBlacklist
98 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
105 function videoModelToFormattedDetailsJSON (video: VideoModel): VideoDetails {
106 const formattedJson = video.toFormattedJSON({
107 additionalAttributes: {
108 scheduledUpdate: true,
113 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
115 const tags = video.Tags ? video.Tags.map(t => t.name) : []
117 const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
119 const detailsJson = {
120 support: video.support,
121 descriptionPath: video.getDescriptionAPIPath(),
122 channel: video.VideoChannel.toFormattedJSON(),
123 account: video.VideoChannel.Account.toFormattedJSON(),
125 commentsEnabled: video.commentsEnabled,
126 downloadEnabled: video.downloadEnabled,
127 waitTranscoding: video.waitTranscoding,
130 label: VideoModel.getStateLabel(video.state)
133 trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs),
139 // Format and sort video files
140 detailsJson.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
142 return Object.assign(formattedJson, detailsJson)
145 function streamingPlaylistsModelToFormattedJSON (video: VideoModel, playlists: VideoStreamingPlaylistModel[]): VideoStreamingPlaylist[] {
146 if (isArray(playlists) === false) return []
150 const redundancies = isArray(playlist.RedundancyVideos)
151 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
157 playlistUrl: playlist.playlistUrl,
158 segmentsSha256Url: playlist.segmentsSha256Url,
160 } as VideoStreamingPlaylist
164 function videoFilesModelToFormattedJSON (video: VideoModel, videoFiles: VideoFileModel[]): VideoFile[] {
165 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
169 let resolutionLabel = videoFile.resolution + 'p'
173 id: videoFile.resolution,
174 label: resolutionLabel
176 magnetUri: video.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
177 size: videoFile.size,
179 torrentUrl: video.getTorrentUrl(videoFile, baseUrlHttp),
180 torrentDownloadUrl: video.getTorrentDownloadUrl(videoFile, baseUrlHttp),
181 fileUrl: video.getVideoFileUrl(videoFile, baseUrlHttp),
182 fileDownloadUrl: video.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
186 if (a.resolution.id < b.resolution.id) return 1
187 if (a.resolution.id === b.resolution.id) return 0
192 function videoModelToActivityPubObject (video: VideoModel): VideoTorrentObject {
193 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
194 if (!video.Tags) video.Tags = []
196 const tag = video.Tags.map(t => ({
197 type: 'Hashtag' as 'Hashtag',
202 if (video.language) {
204 identifier: video.language,
205 name: VideoModel.getLanguageLabel(video.language)
210 if (video.category) {
212 identifier: video.category + '',
213 name: VideoModel.getCategoryLabel(video.category)
220 identifier: video.licence + '',
221 name: VideoModel.getLicenceLabel(video.licence)
225 const url: ActivityUrlObject[] = []
226 for (const file of video.VideoFiles) {
229 mimeType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any,
230 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any,
231 href: video.getVideoFileUrl(file, baseUrlHttp),
232 height: file.resolution,
239 mimeType: 'application/x-bittorrent' as 'application/x-bittorrent',
240 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
241 href: video.getTorrentUrl(file, baseUrlHttp),
242 height: file.resolution
247 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
248 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
249 href: video.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
250 height: file.resolution
254 for (const playlist of (video.VideoStreamingPlaylists || [])) {
255 let tag: (ActivityPlaylistSegmentHashesObject | ActivityPlaylistInfohashesObject)[]
257 tag = playlist.p2pMediaLoaderInfohashes
258 .map(i => ({ type: 'Infohash' as 'Infohash', name: i }))
262 mimeType: 'application/json' as 'application/json',
263 mediaType: 'application/json' as 'application/json',
264 href: playlist.segmentsSha256Url
269 mimeType: 'application/x-mpegURL' as 'application/x-mpegURL',
270 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
271 href: playlist.playlistUrl,
279 mimeType: 'text/html',
280 mediaType: 'text/html',
281 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
284 const subtitleLanguage = []
285 for (const caption of video.VideoCaptions) {
286 subtitleLanguage.push({
287 identifier: caption.language,
288 name: VideoCaptionModel.getLanguageLabel(caption.language)
292 const miniature = video.getMiniature()
295 type: 'Video' as 'Video',
298 duration: getActivityStreamDuration(video.duration),
305 sensitive: video.nsfw,
306 waitTranscoding: video.waitTranscoding,
308 commentsEnabled: video.commentsEnabled,
309 downloadEnabled: video.downloadEnabled,
310 published: video.publishedAt.toISOString(),
311 originallyPublishedAt: video.originallyPublishedAt ? video.originallyPublishedAt.toISOString() : null,
312 updated: video.updatedAt.toISOString(),
313 mediaType: 'text/markdown',
314 content: video.getTruncatedDescription(),
315 support: video.support,
319 url: miniature.getFileUrl(),
320 mediaType: 'image/jpeg',
321 width: miniature.width,
322 height: miniature.height
325 likes: getVideoLikesActivityPubUrl(video),
326 dislikes: getVideoDislikesActivityPubUrl(video),
327 shares: getVideoSharesActivityPubUrl(video),
328 comments: getVideoCommentsActivityPubUrl(video),
332 id: video.VideoChannel.Account.Actor.url
336 id: video.VideoChannel.Actor.url
342 function getActivityStreamDuration (duration: number) {
343 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
344 return 'PT' + duration + 'S'
348 videoModelToFormattedJSON,
349 videoModelToFormattedDetailsJSON,
350 videoFilesModelToFormattedJSON,
351 videoModelToActivityPubObject,
352 getActivityStreamDuration