1 import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos'
2 import { VideoModel } from './video'
4 ActivityPlaylistInfohashesObject,
5 ActivityPlaylistSegmentHashesObject,
8 } from '../../../shared/models/activitypub/objects'
9 import { MIMETYPES, WEBSERVER } from '../../initializers/constants'
10 import { VideoCaptionModel } from './video-caption'
12 getVideoCommentsActivityPubUrl,
13 getVideoDislikesActivityPubUrl,
14 getVideoLikesActivityPubUrl,
15 getVideoSharesActivityPubUrl
16 } from '../../lib/activitypub'
17 import { isArray } from '../../helpers/custom-validators/misc'
18 import { VideoStreamingPlaylist } from '../../../shared/models/videos/video-streaming-playlist.model'
19 import { MStreamingPlaylistRedundanciesOpt, MVideo, MVideoAP, MVideoFormattable, MVideoFormattableDetails } from '../../typings/models'
20 import { MStreamingPlaylistRedundancies } from '../../typings/models/video/video-streaming-playlist'
21 import { MVideoFileRedundanciesOpt } from '../../typings/models/video/video-file'
23 export type VideoFormattingJSONOptions = {
24 completeDescription?: boolean
25 additionalAttributes: {
27 waitTranscoding?: boolean,
28 scheduledUpdate?: boolean,
29 blacklistInfo?: boolean
32 function videoModelToFormattedJSON (video: MVideoFormattable, 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
106 function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails {
107 const formattedJson = video.toFormattedJSON({
108 additionalAttributes: {
109 scheduledUpdate: true,
114 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
116 const tags = video.Tags ? video.Tags.map(t => t.name) : []
118 const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video.VideoStreamingPlaylists)
120 const detailsJson = {
121 support: video.support,
122 descriptionPath: video.getDescriptionAPIPath(),
123 channel: video.VideoChannel.toFormattedJSON(),
124 account: video.VideoChannel.Account.toFormattedJSON(),
126 commentsEnabled: video.commentsEnabled,
127 downloadEnabled: video.downloadEnabled,
128 waitTranscoding: video.waitTranscoding,
131 label: VideoModel.getStateLabel(video.state)
134 trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs),
140 // Format and sort video files
141 detailsJson.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
143 return Object.assign(formattedJson, detailsJson)
146 function streamingPlaylistsModelToFormattedJSON (playlists: MStreamingPlaylistRedundanciesOpt[]): VideoStreamingPlaylist[] {
147 if (isArray(playlists) === false) return []
151 const redundancies = isArray(playlist.RedundancyVideos)
152 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
158 playlistUrl: playlist.playlistUrl,
159 segmentsSha256Url: playlist.segmentsSha256Url,
161 } as VideoStreamingPlaylist
165 function videoFilesModelToFormattedJSON (video: MVideo, videoFiles: MVideoFileRedundanciesOpt[]): VideoFile[] {
166 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
170 let resolutionLabel = videoFile.resolution + 'p'
174 id: videoFile.resolution,
175 label: resolutionLabel
177 magnetUri: video.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
178 size: videoFile.size,
180 torrentUrl: video.getTorrentUrl(videoFile, baseUrlHttp),
181 torrentDownloadUrl: video.getTorrentDownloadUrl(videoFile, baseUrlHttp),
182 fileUrl: video.getVideoFileUrl(videoFile, baseUrlHttp),
183 fileDownloadUrl: video.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
187 if (a.resolution.id < b.resolution.id) return 1
188 if (a.resolution.id === b.resolution.id) return 0
193 function videoModelToActivityPubObject (video: MVideoAP): VideoTorrentObject {
194 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
195 if (!video.Tags) video.Tags = []
197 const tag = video.Tags.map(t => ({
198 type: 'Hashtag' as 'Hashtag',
203 if (video.language) {
205 identifier: video.language,
206 name: VideoModel.getLanguageLabel(video.language)
211 if (video.category) {
213 identifier: video.category + '',
214 name: VideoModel.getCategoryLabel(video.category)
221 identifier: video.licence + '',
222 name: VideoModel.getLicenceLabel(video.licence)
226 const url: ActivityUrlObject[] = []
227 for (const file of video.VideoFiles) {
230 mimeType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any,
231 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any,
232 href: video.getVideoFileUrl(file, baseUrlHttp),
233 height: file.resolution,
240 mimeType: 'application/x-bittorrent' as 'application/x-bittorrent',
241 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
242 href: video.getTorrentUrl(file, baseUrlHttp),
243 height: file.resolution
248 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
249 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
250 href: video.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
251 height: file.resolution
255 for (const playlist of (video.VideoStreamingPlaylists || [])) {
256 let tag: (ActivityPlaylistSegmentHashesObject | ActivityPlaylistInfohashesObject)[]
258 tag = playlist.p2pMediaLoaderInfohashes
259 .map(i => ({ type: 'Infohash' as 'Infohash', name: i }))
263 mimeType: 'application/json' as 'application/json',
264 mediaType: 'application/json' as 'application/json',
265 href: playlist.segmentsSha256Url
270 mimeType: 'application/x-mpegURL' as 'application/x-mpegURL',
271 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
272 href: playlist.playlistUrl,
280 mimeType: 'text/html',
281 mediaType: 'text/html',
282 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
285 const subtitleLanguage = []
286 for (const caption of video.VideoCaptions) {
287 subtitleLanguage.push({
288 identifier: caption.language,
289 name: VideoCaptionModel.getLanguageLabel(caption.language)
293 const miniature = video.getMiniature()
296 type: 'Video' as 'Video',
299 duration: getActivityStreamDuration(video.duration),
306 sensitive: video.nsfw,
307 waitTranscoding: video.waitTranscoding,
309 commentsEnabled: video.commentsEnabled,
310 downloadEnabled: video.downloadEnabled,
311 published: video.publishedAt.toISOString(),
312 originallyPublishedAt: video.originallyPublishedAt ? video.originallyPublishedAt.toISOString() : null,
313 updated: video.updatedAt.toISOString(),
314 mediaType: 'text/markdown',
315 content: video.getTruncatedDescription(),
316 support: video.support,
320 url: miniature.getFileUrl(),
321 mediaType: 'image/jpeg',
322 width: miniature.width,
323 height: miniature.height
326 likes: getVideoLikesActivityPubUrl(video),
327 dislikes: getVideoDislikesActivityPubUrl(video),
328 shares: getVideoSharesActivityPubUrl(video),
329 comments: getVideoCommentsActivityPubUrl(video),
333 id: video.VideoChannel.Account.Actor.url
337 id: video.VideoChannel.Actor.url
343 function getActivityStreamDuration (duration: number) {
344 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
345 return 'PT' + duration + 'S'
349 videoModelToFormattedJSON,
350 videoModelToFormattedDetailsJSON,
351 videoFilesModelToFormattedJSON,
352 videoModelToActivityPubObject,
353 getActivityStreamDuration