1 import { Video, VideoDetails } from '../../../shared/models/videos'
2 import { VideoModel } from './video'
3 import { ActivityTagObject, ActivityUrlObject, VideoTorrentObject } from '../../../shared/models/activitypub/objects'
4 import { MIMETYPES, WEBSERVER } from '../../initializers/constants'
5 import { VideoCaptionModel } from './video-caption'
7 getVideoCommentsActivityPubUrl,
8 getVideoDislikesActivityPubUrl,
9 getVideoLikesActivityPubUrl,
10 getVideoSharesActivityPubUrl
11 } from '../../lib/activitypub'
12 import { isArray } from '../../helpers/custom-validators/misc'
13 import { VideoStreamingPlaylist } from '../../../shared/models/videos/video-streaming-playlist.model'
15 MStreamingPlaylistRedundanciesOpt,
16 MStreamingPlaylistVideo,
21 MVideoFormattableDetails
22 } from '../../typings/models'
23 import { MVideoFileRedundanciesOpt } from '../../typings/models/video/video-file'
24 import { VideoFile } from '@shared/models/videos/video-file.model'
25 import { generateMagnetUri } from '@server/helpers/webtorrent'
27 export type VideoFormattingJSONOptions = {
28 completeDescription?: boolean
29 additionalAttributes: {
31 waitTranscoding?: boolean,
32 scheduledUpdate?: boolean,
33 blacklistInfo?: boolean
36 function videoModelToFormattedJSON (video: MVideoFormattable, options?: VideoFormattingJSONOptions): Video {
37 const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined
39 const videoObject: Video = {
45 label: VideoModel.getCategoryLabel(video.category)
49 label: VideoModel.getLicenceLabel(video.licence)
53 label: VideoModel.getLanguageLabel(video.language)
57 label: VideoModel.getPrivacyLabel(video.privacy)
60 description: options && options.completeDescription === true ? video.description : video.getTruncatedDescription(),
61 isLocal: video.isOwned(),
62 duration: video.duration,
65 dislikes: video.dislikes,
66 thumbnailPath: video.getMiniatureStaticPath(),
67 previewPath: video.getPreviewStaticPath(),
68 embedPath: video.getEmbedStaticPath(),
69 createdAt: video.createdAt,
70 updatedAt: video.updatedAt,
71 publishedAt: video.publishedAt,
72 originallyPublishedAt: video.originallyPublishedAt,
74 account: video.VideoChannel.Account.toFormattedSummaryJSON(),
75 channel: video.VideoChannel.toFormattedSummaryJSON(),
77 userHistory: userHistory ? {
78 currentTime: userHistory.currentTime
83 if (options.additionalAttributes.state === true) {
86 label: VideoModel.getStateLabel(video.state)
90 if (options.additionalAttributes.waitTranscoding === true) {
91 videoObject.waitTranscoding = video.waitTranscoding
94 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
95 videoObject.scheduledUpdate = {
96 updateAt: video.ScheduleVideoUpdate.updateAt,
97 privacy: video.ScheduleVideoUpdate.privacy || undefined
101 if (options.additionalAttributes.blacklistInfo === true) {
102 videoObject.blacklisted = !!video.VideoBlacklist
103 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
110 function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails {
111 const formattedJson = video.toFormattedJSON({
112 additionalAttributes: {
113 scheduledUpdate: true,
118 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
120 const tags = video.Tags ? video.Tags.map(t => t.name) : []
122 const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
124 const detailsJson = {
125 support: video.support,
126 descriptionPath: video.getDescriptionAPIPath(),
127 channel: video.VideoChannel.toFormattedJSON(),
128 account: video.VideoChannel.Account.toFormattedJSON(),
130 commentsEnabled: video.commentsEnabled,
131 downloadEnabled: video.downloadEnabled,
132 waitTranscoding: video.waitTranscoding,
135 label: VideoModel.getStateLabel(video.state)
138 trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs),
144 // Format and sort video files
145 detailsJson.files = videoFilesModelToFormattedJSON(video, baseUrlHttp, baseUrlWs, video.VideoFiles)
147 return Object.assign(formattedJson, detailsJson)
150 function streamingPlaylistsModelToFormattedJSON (video: MVideo, playlists: MStreamingPlaylistRedundanciesOpt[]): VideoStreamingPlaylist[] {
151 if (isArray(playlists) === false) return []
153 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
157 const playlistWithVideo = Object.assign(playlist, { Video: video })
159 const redundancies = isArray(playlist.RedundancyVideos)
160 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
163 const files = videoFilesModelToFormattedJSON(playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles)
168 playlistUrl: playlist.playlistUrl,
169 segmentsSha256Url: playlist.segmentsSha256Url,
176 function videoFilesModelToFormattedJSON (
177 model: MVideo | MStreamingPlaylistVideo,
180 videoFiles: MVideoFileRedundanciesOpt[]
184 let resolutionLabel = videoFile.resolution + 'p'
188 id: videoFile.resolution,
189 label: resolutionLabel
191 magnetUri: generateMagnetUri(model, videoFile, baseUrlHttp, baseUrlWs),
192 size: videoFile.size,
194 torrentUrl: model.getTorrentUrl(videoFile, baseUrlHttp),
195 torrentDownloadUrl: model.getTorrentDownloadUrl(videoFile, baseUrlHttp),
196 fileUrl: model.getVideoFileUrl(videoFile, baseUrlHttp),
197 fileDownloadUrl: model.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
201 if (a.resolution.id < b.resolution.id) return 1
202 if (a.resolution.id === b.resolution.id) return 0
207 function addVideoFilesInAPAcc (
208 acc: ActivityUrlObject[] | ActivityTagObject[],
209 model: MVideoAP | MStreamingPlaylistVideo,
214 for (const file of files) {
217 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any,
218 href: model.getVideoFileUrl(file, baseUrlHttp),
219 height: file.resolution,
226 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
227 href: model.getTorrentUrl(file, baseUrlHttp),
228 height: file.resolution
233 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
234 href: generateMagnetUri(model, file, baseUrlHttp, baseUrlWs),
235 height: file.resolution
240 function videoModelToActivityPubObject (video: MVideoAP): VideoTorrentObject {
241 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
242 if (!video.Tags) video.Tags = []
244 const tag = video.Tags.map(t => ({
245 type: 'Hashtag' as 'Hashtag',
250 if (video.language) {
252 identifier: video.language,
253 name: VideoModel.getLanguageLabel(video.language)
258 if (video.category) {
260 identifier: video.category + '',
261 name: VideoModel.getCategoryLabel(video.category)
268 identifier: video.licence + '',
269 name: VideoModel.getLicenceLabel(video.licence)
273 const url: ActivityUrlObject[] = []
274 addVideoFilesInAPAcc(url, video, baseUrlHttp, baseUrlWs, video.VideoFiles || [])
276 for (const playlist of (video.VideoStreamingPlaylists || [])) {
277 let tag: ActivityTagObject[]
279 tag = playlist.p2pMediaLoaderInfohashes
280 .map(i => ({ type: 'Infohash' as 'Infohash', name: i }))
284 mediaType: 'application/json' as 'application/json',
285 href: playlist.segmentsSha256Url
288 const playlistWithVideo = Object.assign(playlist, { Video: video })
289 addVideoFilesInAPAcc(tag, playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles || [])
293 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
294 href: playlist.playlistUrl,
302 mediaType: 'text/html',
303 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
306 const subtitleLanguage = []
307 for (const caption of video.VideoCaptions) {
308 subtitleLanguage.push({
309 identifier: caption.language,
310 name: VideoCaptionModel.getLanguageLabel(caption.language),
311 url: caption.getFileUrl(video)
315 const icons = [ video.getMiniature(), video.getPreview() ]
318 type: 'Video' as 'Video',
321 duration: getActivityStreamDuration(video.duration),
328 sensitive: video.nsfw,
329 waitTranscoding: video.waitTranscoding,
331 commentsEnabled: video.commentsEnabled,
332 downloadEnabled: video.downloadEnabled,
333 published: video.publishedAt.toISOString(),
334 originallyPublishedAt: video.originallyPublishedAt ? video.originallyPublishedAt.toISOString() : null,
335 updated: video.updatedAt.toISOString(),
336 mediaType: 'text/markdown',
337 content: video.getTruncatedDescription(),
338 support: video.support,
340 icon: icons.map(i => ({
342 url: i.getFileUrl(video),
343 mediaType: 'image/jpeg',
348 likes: getVideoLikesActivityPubUrl(video),
349 dislikes: getVideoDislikesActivityPubUrl(video),
350 shares: getVideoSharesActivityPubUrl(video),
351 comments: getVideoCommentsActivityPubUrl(video),
355 id: video.VideoChannel.Account.Actor.url
359 id: video.VideoChannel.Actor.url
365 function getActivityStreamDuration (duration: number) {
366 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
367 return 'PT' + duration + 'S'
371 videoModelToFormattedJSON,
372 videoModelToFormattedDetailsJSON,
373 videoFilesModelToFormattedJSON,
374 videoModelToActivityPubObject,
375 getActivityStreamDuration