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 { CONFIG, MIMETYPES, THUMBNAILS_SIZE } from '../../initializers'
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 formattedAccount = video.VideoChannel.Account.toFormattedJSON()
33 const formattedVideoChannel = video.VideoChannel.toFormattedJSON()
35 const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined
37 const videoObject: Video = {
43 label: VideoModel.getCategoryLabel(video.category)
47 label: VideoModel.getLicenceLabel(video.licence)
51 label: VideoModel.getLanguageLabel(video.language)
55 label: VideoModel.getPrivacyLabel(video.privacy)
58 description: options && options.completeDescription === true ? video.description : video.getTruncatedDescription(),
59 isLocal: video.isOwned(),
60 duration: video.duration,
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 originallyPublishedAt: video.originallyPublishedAt,
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
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
90 userHistory: userHistory ? {
91 currentTime: userHistory.currentTime
96 if (options.additionalAttributes.state === true) {
99 label: VideoModel.getStateLabel(video.state)
103 if (options.additionalAttributes.waitTranscoding === true) {
104 videoObject.waitTranscoding = video.waitTranscoding
107 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
108 videoObject.scheduledUpdate = {
109 updateAt: video.ScheduleVideoUpdate.updateAt,
110 privacy: video.ScheduleVideoUpdate.privacy || undefined
114 if (options.additionalAttributes.blacklistInfo === true) {
115 videoObject.blacklisted = !!video.VideoBlacklist
116 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
123 function videoModelToFormattedDetailsJSON (video: VideoModel): VideoDetails {
124 const formattedJson = video.toFormattedJSON({
125 additionalAttributes: {
126 scheduledUpdate: true,
131 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
133 const tags = video.Tags ? video.Tags.map(t => t.name) : []
135 const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
137 const detailsJson = {
138 support: video.support,
139 descriptionPath: video.getDescriptionAPIPath(),
140 channel: video.VideoChannel.toFormattedJSON(),
141 account: video.VideoChannel.Account.toFormattedJSON(),
143 commentsEnabled: video.commentsEnabled,
144 downloadEnabled: video.downloadEnabled,
145 waitTranscoding: video.waitTranscoding,
148 label: VideoModel.getStateLabel(video.state)
151 trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs),
157 // Format and sort video files
158 detailsJson.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
160 return Object.assign(formattedJson, detailsJson)
163 function streamingPlaylistsModelToFormattedJSON (video: VideoModel, playlists: VideoStreamingPlaylistModel[]): VideoStreamingPlaylist[] {
164 if (isArray(playlists) === false) return []
168 const redundancies = isArray(playlist.RedundancyVideos)
169 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
175 playlistUrl: playlist.playlistUrl,
176 segmentsSha256Url: playlist.segmentsSha256Url,
178 } as VideoStreamingPlaylist
182 function videoFilesModelToFormattedJSON (video: VideoModel, videoFiles: VideoFileModel[]): VideoFile[] {
183 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
187 let resolutionLabel = videoFile.resolution + 'p'
191 id: videoFile.resolution,
192 label: resolutionLabel
194 magnetUri: video.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
195 size: videoFile.size,
197 torrentUrl: video.getTorrentUrl(videoFile, baseUrlHttp),
198 torrentDownloadUrl: video.getTorrentDownloadUrl(videoFile, baseUrlHttp),
199 fileUrl: video.getVideoFileUrl(videoFile, baseUrlHttp),
200 fileDownloadUrl: video.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
204 if (a.resolution.id < b.resolution.id) return 1
205 if (a.resolution.id === b.resolution.id) return 0
210 function videoModelToActivityPubObject (video: VideoModel): VideoTorrentObject {
211 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
212 if (!video.Tags) video.Tags = []
214 const tag = video.Tags.map(t => ({
215 type: 'Hashtag' as 'Hashtag',
220 if (video.language) {
222 identifier: video.language,
223 name: VideoModel.getLanguageLabel(video.language)
228 if (video.category) {
230 identifier: video.category + '',
231 name: VideoModel.getCategoryLabel(video.category)
238 identifier: video.licence + '',
239 name: VideoModel.getLicenceLabel(video.licence)
243 const url: ActivityUrlObject[] = []
244 for (const file of video.VideoFiles) {
247 mimeType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any,
248 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any,
249 href: video.getVideoFileUrl(file, baseUrlHttp),
250 height: file.resolution,
257 mimeType: 'application/x-bittorrent' as 'application/x-bittorrent',
258 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
259 href: video.getTorrentUrl(file, baseUrlHttp),
260 height: file.resolution
265 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
266 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
267 href: video.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
268 height: file.resolution
272 for (const playlist of (video.VideoStreamingPlaylists || [])) {
273 let tag: (ActivityPlaylistSegmentHashesObject | ActivityPlaylistInfohashesObject)[]
275 tag = playlist.p2pMediaLoaderInfohashes
276 .map(i => ({ type: 'Infohash' as 'Infohash', name: i }))
280 mimeType: 'application/json' as 'application/json',
281 mediaType: 'application/json' as 'application/json',
282 href: playlist.segmentsSha256Url
287 mimeType: 'application/x-mpegURL' as 'application/x-mpegURL',
288 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
289 href: playlist.playlistUrl,
297 mimeType: 'text/html',
298 mediaType: 'text/html',
299 href: CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
302 const subtitleLanguage = []
303 for (const caption of video.VideoCaptions) {
304 subtitleLanguage.push({
305 identifier: caption.language,
306 name: VideoCaptionModel.getLanguageLabel(caption.language)
311 type: 'Video' as 'Video',
314 duration: getActivityStreamDuration(video.duration),
321 sensitive: video.nsfw,
322 waitTranscoding: video.waitTranscoding,
324 commentsEnabled: video.commentsEnabled,
325 downloadEnabled: video.downloadEnabled,
326 published: video.publishedAt.toISOString(),
327 originallyPublishedAt: video.originallyPublishedAt ?
328 video.originallyPublishedAt.toISOString() :
330 updated: video.updatedAt.toISOString(),
331 mediaType: 'text/markdown',
332 content: video.getTruncatedDescription(),
333 support: video.support,
337 url: video.getThumbnailUrl(baseUrlHttp),
338 mediaType: 'image/jpeg',
339 width: THUMBNAILS_SIZE.width,
340 height: THUMBNAILS_SIZE.height
343 likes: getVideoLikesActivityPubUrl(video),
344 dislikes: getVideoDislikesActivityPubUrl(video),
345 shares: getVideoSharesActivityPubUrl(video),
346 comments: getVideoCommentsActivityPubUrl(video),
350 id: video.VideoChannel.Account.Actor.url
354 id: video.VideoChannel.Actor.url
360 function getActivityStreamDuration (duration: number) {
361 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
362 return 'PT' + duration + 'S'
366 videoModelToFormattedJSON,
367 videoModelToFormattedDetailsJSON,
368 videoFilesModelToFormattedJSON,
369 videoModelToActivityPubObject,
370 getActivityStreamDuration