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,
71 id: formattedAccount.id,
72 uuid: formattedAccount.uuid,
73 name: formattedAccount.name,
74 displayName: formattedAccount.displayName,
75 url: formattedAccount.url,
76 host: formattedAccount.host,
77 avatar: formattedAccount.avatar
80 id: formattedVideoChannel.id,
81 uuid: formattedVideoChannel.uuid,
82 name: formattedVideoChannel.name,
83 displayName: formattedVideoChannel.displayName,
84 url: formattedVideoChannel.url,
85 host: formattedVideoChannel.host,
86 avatar: formattedVideoChannel.avatar
89 userHistory: userHistory ? {
90 currentTime: userHistory.currentTime
95 if (options.additionalAttributes.state === true) {
98 label: VideoModel.getStateLabel(video.state)
102 if (options.additionalAttributes.waitTranscoding === true) {
103 videoObject.waitTranscoding = video.waitTranscoding
106 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
107 videoObject.scheduledUpdate = {
108 updateAt: video.ScheduleVideoUpdate.updateAt,
109 privacy: video.ScheduleVideoUpdate.privacy || undefined
113 if (options.additionalAttributes.blacklistInfo === true) {
114 videoObject.blacklisted = !!video.VideoBlacklist
115 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
122 function videoModelToFormattedDetailsJSON (video: VideoModel): VideoDetails {
123 const formattedJson = video.toFormattedJSON({
124 additionalAttributes: {
125 scheduledUpdate: true,
130 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
132 const tags = video.Tags ? video.Tags.map(t => t.name) : []
134 const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
136 const detailsJson = {
137 support: video.support,
138 descriptionPath: video.getDescriptionAPIPath(),
139 channel: video.VideoChannel.toFormattedJSON(),
140 account: video.VideoChannel.Account.toFormattedJSON(),
142 commentsEnabled: video.commentsEnabled,
143 waitTranscoding: video.waitTranscoding,
146 label: VideoModel.getStateLabel(video.state)
149 trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs),
155 // Format and sort video files
156 detailsJson.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
158 return Object.assign(formattedJson, detailsJson)
161 function streamingPlaylistsModelToFormattedJSON (video: VideoModel, playlists: VideoStreamingPlaylistModel[]): VideoStreamingPlaylist[] {
162 if (isArray(playlists) === false) return []
166 const redundancies = isArray(playlist.RedundancyVideos)
167 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
173 playlistUrl: playlist.playlistUrl,
174 segmentsSha256Url: playlist.segmentsSha256Url,
176 } as VideoStreamingPlaylist
180 function videoFilesModelToFormattedJSON (video: VideoModel, videoFiles: VideoFileModel[]): VideoFile[] {
181 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
185 let resolutionLabel = videoFile.resolution + 'p'
189 id: videoFile.resolution,
190 label: resolutionLabel
192 magnetUri: video.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
193 size: videoFile.size,
195 torrentUrl: video.getTorrentUrl(videoFile, baseUrlHttp),
196 torrentDownloadUrl: video.getTorrentDownloadUrl(videoFile, baseUrlHttp),
197 fileUrl: video.getVideoFileUrl(videoFile, baseUrlHttp),
198 fileDownloadUrl: video.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
202 if (a.resolution.id < b.resolution.id) return 1
203 if (a.resolution.id === b.resolution.id) return 0
208 function videoModelToActivityPubObject (video: VideoModel): VideoTorrentObject {
209 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
210 if (!video.Tags) video.Tags = []
212 const tag = video.Tags.map(t => ({
213 type: 'Hashtag' as 'Hashtag',
218 if (video.language) {
220 identifier: video.language,
221 name: VideoModel.getLanguageLabel(video.language)
226 if (video.category) {
228 identifier: video.category + '',
229 name: VideoModel.getCategoryLabel(video.category)
236 identifier: video.licence + '',
237 name: VideoModel.getLicenceLabel(video.licence)
241 const url: ActivityUrlObject[] = []
242 for (const file of video.VideoFiles) {
245 mimeType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any,
246 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any,
247 href: video.getVideoFileUrl(file, baseUrlHttp),
248 height: file.resolution,
255 mimeType: 'application/x-bittorrent' as 'application/x-bittorrent',
256 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
257 href: video.getTorrentUrl(file, baseUrlHttp),
258 height: file.resolution
263 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
264 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
265 href: video.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
266 height: file.resolution
270 for (const playlist of (video.VideoStreamingPlaylists || [])) {
271 let tag: (ActivityPlaylistSegmentHashesObject | ActivityPlaylistInfohashesObject)[]
273 tag = playlist.p2pMediaLoaderInfohashes
274 .map(i => ({ type: 'Infohash' as 'Infohash', name: i }))
278 mimeType: 'application/json' as 'application/json',
279 mediaType: 'application/json' as 'application/json',
280 href: playlist.segmentsSha256Url
285 mimeType: 'application/x-mpegURL' as 'application/x-mpegURL',
286 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
287 href: playlist.playlistUrl,
295 mimeType: 'text/html',
296 mediaType: 'text/html',
297 href: CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
300 const subtitleLanguage = []
301 for (const caption of video.VideoCaptions) {
302 subtitleLanguage.push({
303 identifier: caption.language,
304 name: VideoCaptionModel.getLanguageLabel(caption.language)
309 type: 'Video' as 'Video',
312 duration: getActivityStreamDuration(video.duration),
319 sensitive: video.nsfw,
320 waitTranscoding: video.waitTranscoding,
322 commentsEnabled: video.commentsEnabled,
323 published: video.publishedAt.toISOString(),
324 updated: video.updatedAt.toISOString(),
325 mediaType: 'text/markdown',
326 content: video.getTruncatedDescription(),
327 support: video.support,
331 url: video.getThumbnailUrl(baseUrlHttp),
332 mediaType: 'image/jpeg',
333 width: THUMBNAILS_SIZE.width,
334 height: THUMBNAILS_SIZE.height
337 likes: getVideoLikesActivityPubUrl(video),
338 dislikes: getVideoDislikesActivityPubUrl(video),
339 shares: getVideoSharesActivityPubUrl(video),
340 comments: getVideoCommentsActivityPubUrl(video),
344 id: video.VideoChannel.Account.Actor.url
348 id: video.VideoChannel.Actor.url
354 function getActivityStreamDuration (duration: number) {
355 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
356 return 'PT' + duration + 'S'
360 videoModelToFormattedJSON,
361 videoModelToFormattedDetailsJSON,
362 videoFilesModelToFormattedJSON,
363 videoModelToActivityPubObject,
364 getActivityStreamDuration