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
29 playlistInfo?: boolean
32 function videoModelToFormattedJSON (video: VideoModel, options?: VideoFormattingJSONOptions): Video {
33 const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined
35 const videoObject: Video = {
41 label: VideoModel.getCategoryLabel(video.category)
45 label: VideoModel.getLicenceLabel(video.licence)
49 label: VideoModel.getLanguageLabel(video.language)
53 label: VideoModel.getPrivacyLabel(video.privacy)
56 description: options && options.completeDescription === true ? video.description : video.getTruncatedDescription(),
57 isLocal: video.isOwned(),
58 duration: video.duration,
61 dislikes: video.dislikes,
62 thumbnailPath: video.getMiniatureStaticPath(),
63 previewPath: video.getPreviewStaticPath(),
64 embedPath: video.getEmbedStaticPath(),
65 createdAt: video.createdAt,
66 updatedAt: video.updatedAt,
67 publishedAt: video.publishedAt,
68 originallyPublishedAt: video.originallyPublishedAt,
70 account: video.VideoChannel.Account.toFormattedSummaryJSON(),
71 channel: video.VideoChannel.toFormattedSummaryJSON(),
73 userHistory: userHistory ? {
74 currentTime: userHistory.currentTime
79 if (options.additionalAttributes.state === true) {
82 label: VideoModel.getStateLabel(video.state)
86 if (options.additionalAttributes.waitTranscoding === true) {
87 videoObject.waitTranscoding = video.waitTranscoding
90 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
91 videoObject.scheduledUpdate = {
92 updateAt: video.ScheduleVideoUpdate.updateAt,
93 privacy: video.ScheduleVideoUpdate.privacy || undefined
97 if (options.additionalAttributes.blacklistInfo === true) {
98 videoObject.blacklisted = !!video.VideoBlacklist
99 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
102 if (options.additionalAttributes.playlistInfo === true) {
103 // We filtered on a specific videoId/videoPlaylistId, that is unique
104 const playlistElement = video.VideoPlaylistElements[0]
106 videoObject.playlistElement = {
107 position: playlistElement.position,
108 startTimestamp: playlistElement.startTimestamp,
109 stopTimestamp: playlistElement.stopTimestamp
117 function videoModelToFormattedDetailsJSON (video: VideoModel): VideoDetails {
118 const formattedJson = video.toFormattedJSON({
119 additionalAttributes: {
120 scheduledUpdate: true,
125 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
127 const tags = video.Tags ? video.Tags.map(t => t.name) : []
129 const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
131 const detailsJson = {
132 support: video.support,
133 descriptionPath: video.getDescriptionAPIPath(),
134 channel: video.VideoChannel.toFormattedJSON(),
135 account: video.VideoChannel.Account.toFormattedJSON(),
137 commentsEnabled: video.commentsEnabled,
138 downloadEnabled: video.downloadEnabled,
139 waitTranscoding: video.waitTranscoding,
142 label: VideoModel.getStateLabel(video.state)
145 trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs),
151 // Format and sort video files
152 detailsJson.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
154 return Object.assign(formattedJson, detailsJson)
157 function streamingPlaylistsModelToFormattedJSON (video: VideoModel, playlists: VideoStreamingPlaylistModel[]): VideoStreamingPlaylist[] {
158 if (isArray(playlists) === false) return []
162 const redundancies = isArray(playlist.RedundancyVideos)
163 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
169 playlistUrl: playlist.playlistUrl,
170 segmentsSha256Url: playlist.segmentsSha256Url,
172 } as VideoStreamingPlaylist
176 function videoFilesModelToFormattedJSON (video: VideoModel, videoFiles: VideoFileModel[]): VideoFile[] {
177 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
181 let resolutionLabel = videoFile.resolution + 'p'
185 id: videoFile.resolution,
186 label: resolutionLabel
188 magnetUri: video.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
189 size: videoFile.size,
191 torrentUrl: video.getTorrentUrl(videoFile, baseUrlHttp),
192 torrentDownloadUrl: video.getTorrentDownloadUrl(videoFile, baseUrlHttp),
193 fileUrl: video.getVideoFileUrl(videoFile, baseUrlHttp),
194 fileDownloadUrl: video.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
198 if (a.resolution.id < b.resolution.id) return 1
199 if (a.resolution.id === b.resolution.id) return 0
204 function videoModelToActivityPubObject (video: VideoModel): VideoTorrentObject {
205 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
206 if (!video.Tags) video.Tags = []
208 const tag = video.Tags.map(t => ({
209 type: 'Hashtag' as 'Hashtag',
214 if (video.language) {
216 identifier: video.language,
217 name: VideoModel.getLanguageLabel(video.language)
222 if (video.category) {
224 identifier: video.category + '',
225 name: VideoModel.getCategoryLabel(video.category)
232 identifier: video.licence + '',
233 name: VideoModel.getLicenceLabel(video.licence)
237 const url: ActivityUrlObject[] = []
238 for (const file of video.VideoFiles) {
241 mimeType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any,
242 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any,
243 href: video.getVideoFileUrl(file, baseUrlHttp),
244 height: file.resolution,
251 mimeType: 'application/x-bittorrent' as 'application/x-bittorrent',
252 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
253 href: video.getTorrentUrl(file, baseUrlHttp),
254 height: file.resolution
259 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
260 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
261 href: video.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
262 height: file.resolution
266 for (const playlist of (video.VideoStreamingPlaylists || [])) {
267 let tag: (ActivityPlaylistSegmentHashesObject | ActivityPlaylistInfohashesObject)[]
269 tag = playlist.p2pMediaLoaderInfohashes
270 .map(i => ({ type: 'Infohash' as 'Infohash', name: i }))
274 mimeType: 'application/json' as 'application/json',
275 mediaType: 'application/json' as 'application/json',
276 href: playlist.segmentsSha256Url
281 mimeType: 'application/x-mpegURL' as 'application/x-mpegURL',
282 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
283 href: playlist.playlistUrl,
291 mimeType: 'text/html',
292 mediaType: 'text/html',
293 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
296 const subtitleLanguage = []
297 for (const caption of video.VideoCaptions) {
298 subtitleLanguage.push({
299 identifier: caption.language,
300 name: VideoCaptionModel.getLanguageLabel(caption.language)
304 const miniature = video.getMiniature()
307 type: 'Video' as 'Video',
310 duration: getActivityStreamDuration(video.duration),
317 sensitive: video.nsfw,
318 waitTranscoding: video.waitTranscoding,
320 commentsEnabled: video.commentsEnabled,
321 downloadEnabled: video.downloadEnabled,
322 published: video.publishedAt.toISOString(),
323 originallyPublishedAt: video.originallyPublishedAt ? video.originallyPublishedAt.toISOString() : null,
324 updated: video.updatedAt.toISOString(),
325 mediaType: 'text/markdown',
326 content: video.getTruncatedDescription(),
327 support: video.support,
331 url: miniature.getFileUrl(),
332 mediaType: 'image/jpeg',
333 width: miniature.width,
334 height: miniature.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