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 downloadEnabled: video.downloadEnabled,
144 waitTranscoding: video.waitTranscoding,
147 label: VideoModel.getStateLabel(video.state)
150 trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs),
156 // Format and sort video files
157 detailsJson.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
159 return Object.assign(formattedJson, detailsJson)
162 function streamingPlaylistsModelToFormattedJSON (video: VideoModel, playlists: VideoStreamingPlaylistModel[]): VideoStreamingPlaylist[] {
163 if (isArray(playlists) === false) return []
167 const redundancies = isArray(playlist.RedundancyVideos)
168 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
174 playlistUrl: playlist.playlistUrl,
175 segmentsSha256Url: playlist.segmentsSha256Url,
177 } as VideoStreamingPlaylist
181 function videoFilesModelToFormattedJSON (video: VideoModel, videoFiles: VideoFileModel[]): VideoFile[] {
182 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
186 let resolutionLabel = videoFile.resolution + 'p'
190 id: videoFile.resolution,
191 label: resolutionLabel
193 magnetUri: video.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
194 size: videoFile.size,
196 torrentUrl: video.getTorrentUrl(videoFile, baseUrlHttp),
197 torrentDownloadUrl: video.getTorrentDownloadUrl(videoFile, baseUrlHttp),
198 fileUrl: video.getVideoFileUrl(videoFile, baseUrlHttp),
199 fileDownloadUrl: video.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
203 if (a.resolution.id < b.resolution.id) return 1
204 if (a.resolution.id === b.resolution.id) return 0
209 function videoModelToActivityPubObject (video: VideoModel): VideoTorrentObject {
210 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
211 if (!video.Tags) video.Tags = []
213 const tag = video.Tags.map(t => ({
214 type: 'Hashtag' as 'Hashtag',
219 if (video.language) {
221 identifier: video.language,
222 name: VideoModel.getLanguageLabel(video.language)
227 if (video.category) {
229 identifier: video.category + '',
230 name: VideoModel.getCategoryLabel(video.category)
237 identifier: video.licence + '',
238 name: VideoModel.getLicenceLabel(video.licence)
242 const url: ActivityUrlObject[] = []
243 for (const file of video.VideoFiles) {
246 mimeType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any,
247 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any,
248 href: video.getVideoFileUrl(file, baseUrlHttp),
249 height: file.resolution,
256 mimeType: 'application/x-bittorrent' as 'application/x-bittorrent',
257 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
258 href: video.getTorrentUrl(file, baseUrlHttp),
259 height: file.resolution
264 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
265 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
266 href: video.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
267 height: file.resolution
271 for (const playlist of (video.VideoStreamingPlaylists || [])) {
272 let tag: (ActivityPlaylistSegmentHashesObject | ActivityPlaylistInfohashesObject)[]
274 tag = playlist.p2pMediaLoaderInfohashes
275 .map(i => ({ type: 'Infohash' as 'Infohash', name: i }))
279 mimeType: 'application/json' as 'application/json',
280 mediaType: 'application/json' as 'application/json',
281 href: playlist.segmentsSha256Url
286 mimeType: 'application/x-mpegURL' as 'application/x-mpegURL',
287 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
288 href: playlist.playlistUrl,
296 mimeType: 'text/html',
297 mediaType: 'text/html',
298 href: CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
301 const subtitleLanguage = []
302 for (const caption of video.VideoCaptions) {
303 subtitleLanguage.push({
304 identifier: caption.language,
305 name: VideoCaptionModel.getLanguageLabel(caption.language)
310 type: 'Video' as 'Video',
313 duration: getActivityStreamDuration(video.duration),
320 sensitive: video.nsfw,
321 waitTranscoding: video.waitTranscoding,
323 commentsEnabled: video.commentsEnabled,
324 downloadEnabled: video.downloadEnabled,
325 published: video.publishedAt.toISOString(),
326 updated: video.updatedAt.toISOString(),
327 mediaType: 'text/markdown',
328 content: video.getTruncatedDescription(),
329 support: video.support,
333 url: video.getThumbnailUrl(baseUrlHttp),
334 mediaType: 'image/jpeg',
335 width: THUMBNAILS_SIZE.width,
336 height: THUMBNAILS_SIZE.height
339 likes: getVideoLikesActivityPubUrl(video),
340 dislikes: getVideoDislikesActivityPubUrl(video),
341 shares: getVideoSharesActivityPubUrl(video),
342 comments: getVideoCommentsActivityPubUrl(video),
346 id: video.VideoChannel.Account.Actor.url
350 id: video.VideoChannel.Actor.url
356 function getActivityStreamDuration (duration: number) {
357 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
358 return 'PT' + duration + 'S'
362 videoModelToFormattedJSON,
363 videoModelToFormattedDetailsJSON,
364 videoFilesModelToFormattedJSON,
365 videoModelToActivityPubObject,
366 getActivityStreamDuration