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/url'
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 '../../types/models'
23 import { MVideoFileRedundanciesOpt } from '../../types/models/video/video-file'
24 import { VideoFile } from '@shared/models/videos/video-file.model'
25 import { generateMagnetUri } from '@server/helpers/webtorrent'
26 import { extractVideo } from '@server/helpers/video'
28 export type VideoFormattingJSONOptions = {
29 completeDescription?: boolean
30 additionalAttributes: {
32 waitTranscoding?: boolean
33 scheduledUpdate?: boolean
34 blacklistInfo?: boolean
38 function videoModelToFormattedJSON (video: MVideoFormattable, options?: VideoFormattingJSONOptions): Video {
39 const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined
41 const videoObject: Video = {
47 label: VideoModel.getCategoryLabel(video.category)
51 label: VideoModel.getLicenceLabel(video.licence)
55 label: VideoModel.getLanguageLabel(video.language)
59 label: VideoModel.getPrivacyLabel(video.privacy)
63 description: options && options.completeDescription === true
65 : video.getTruncatedDescription(),
67 isLocal: video.isOwned(),
68 duration: video.duration,
71 dislikes: video.dislikes,
72 thumbnailPath: video.getMiniatureStaticPath(),
73 previewPath: video.getPreviewStaticPath(),
74 embedPath: video.getEmbedStaticPath(),
75 createdAt: video.createdAt,
76 updatedAt: video.updatedAt,
77 publishedAt: video.publishedAt,
78 originallyPublishedAt: video.originallyPublishedAt,
80 account: video.VideoChannel.Account.toFormattedSummaryJSON(),
81 channel: video.VideoChannel.toFormattedSummaryJSON(),
83 userHistory: userHistory ? {
84 currentTime: userHistory.currentTime
87 // Can be added by external plugins
88 pluginData: (video as any).pluginData
92 if (options.additionalAttributes.state === true) {
95 label: VideoModel.getStateLabel(video.state)
99 if (options.additionalAttributes.waitTranscoding === true) {
100 videoObject.waitTranscoding = video.waitTranscoding
103 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
104 videoObject.scheduledUpdate = {
105 updateAt: video.ScheduleVideoUpdate.updateAt,
106 privacy: video.ScheduleVideoUpdate.privacy || undefined
110 if (options.additionalAttributes.blacklistInfo === true) {
111 videoObject.blacklisted = !!video.VideoBlacklist
112 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
119 function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails {
120 const formattedJson = video.toFormattedJSON({
121 additionalAttributes: {
122 scheduledUpdate: true,
127 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
129 const tags = video.Tags ? video.Tags.map(t => t.name) : []
131 const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
133 const detailsJson = {
134 support: video.support,
135 descriptionPath: video.getDescriptionAPIPath(),
136 channel: video.VideoChannel.toFormattedJSON(),
137 account: video.VideoChannel.Account.toFormattedJSON(),
139 commentsEnabled: video.commentsEnabled,
140 downloadEnabled: video.downloadEnabled,
141 waitTranscoding: video.waitTranscoding,
144 label: VideoModel.getStateLabel(video.state)
147 trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs),
153 // Format and sort video files
154 detailsJson.files = videoFilesModelToFormattedJSON(video, baseUrlHttp, baseUrlWs, video.VideoFiles)
156 return Object.assign(formattedJson, detailsJson)
159 function streamingPlaylistsModelToFormattedJSON (video: MVideo, playlists: MStreamingPlaylistRedundanciesOpt[]): VideoStreamingPlaylist[] {
160 if (isArray(playlists) === false) return []
162 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
166 const playlistWithVideo = Object.assign(playlist, { Video: video })
168 const redundancies = isArray(playlist.RedundancyVideos)
169 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
172 const files = videoFilesModelToFormattedJSON(playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles)
177 playlistUrl: playlist.playlistUrl,
178 segmentsSha256Url: playlist.segmentsSha256Url,
185 function sortByResolutionDesc (fileA: MVideoFile, fileB: MVideoFile) {
186 if (fileA.resolution < fileB.resolution) return 1
187 if (fileA.resolution === fileB.resolution) return 0
191 function videoFilesModelToFormattedJSON (
192 model: MVideo | MStreamingPlaylistVideo,
195 videoFiles: MVideoFileRedundanciesOpt[]
197 const video = extractVideo(model)
199 return [ ...videoFiles ]
200 .sort(sortByResolutionDesc)
204 id: videoFile.resolution,
205 label: videoFile.resolution + 'p'
207 magnetUri: generateMagnetUri(model, videoFile, baseUrlHttp, baseUrlWs),
208 size: videoFile.size,
210 torrentUrl: model.getTorrentUrl(videoFile, baseUrlHttp),
211 torrentDownloadUrl: model.getTorrentDownloadUrl(videoFile, baseUrlHttp),
212 fileUrl: model.getVideoFileUrl(videoFile, baseUrlHttp),
213 fileDownloadUrl: model.getVideoFileDownloadUrl(videoFile, baseUrlHttp),
214 metadataUrl: video.getVideoFileMetadataUrl(videoFile, baseUrlHttp)
219 function addVideoFilesInAPAcc (
220 acc: ActivityUrlObject[] | ActivityTagObject[],
221 model: MVideoAP | MStreamingPlaylistVideo,
226 const sortedFiles = [ ...files ].sort(sortByResolutionDesc)
228 for (const file of sortedFiles) {
231 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any,
232 href: model.getVideoFileUrl(file, baseUrlHttp),
233 height: file.resolution,
240 rel: [ 'metadata', MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] ],
241 mediaType: 'application/json' as 'application/json',
242 href: extractVideo(model).getVideoFileMetadataUrl(file, baseUrlHttp),
243 height: file.resolution,
249 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
250 href: model.getTorrentUrl(file, baseUrlHttp),
251 height: file.resolution
256 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
257 href: generateMagnetUri(model, file, baseUrlHttp, baseUrlWs),
258 height: file.resolution
263 function videoModelToActivityPubObject (video: MVideoAP): VideoTorrentObject {
264 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
265 if (!video.Tags) video.Tags = []
267 const tag = video.Tags.map(t => ({
268 type: 'Hashtag' as 'Hashtag',
273 if (video.language) {
275 identifier: video.language,
276 name: VideoModel.getLanguageLabel(video.language)
281 if (video.category) {
283 identifier: video.category + '',
284 name: VideoModel.getCategoryLabel(video.category)
291 identifier: video.licence + '',
292 name: VideoModel.getLicenceLabel(video.licence)
296 const url: ActivityUrlObject[] = [
297 // HTML url should be the first element in the array so Mastodon correctly displays the embed
300 mediaType: 'text/html',
301 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
305 addVideoFilesInAPAcc(url, video, baseUrlHttp, baseUrlWs, video.VideoFiles || [])
307 for (const playlist of (video.VideoStreamingPlaylists || [])) {
308 const tag = playlist.p2pMediaLoaderInfohashes
309 .map(i => ({ type: 'Infohash' as 'Infohash', name: i })) as ActivityTagObject[]
313 mediaType: 'application/json' as 'application/json',
314 href: playlist.segmentsSha256Url
317 const playlistWithVideo = Object.assign(playlist, { Video: video })
318 addVideoFilesInAPAcc(tag, playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles || [])
322 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
323 href: playlist.playlistUrl,
328 const subtitleLanguage = []
329 for (const caption of video.VideoCaptions) {
330 subtitleLanguage.push({
331 identifier: caption.language,
332 name: VideoCaptionModel.getLanguageLabel(caption.language),
333 url: caption.getFileUrl(video)
337 const icons = [ video.getMiniature(), video.getPreview() ]
340 type: 'Video' as 'Video',
343 duration: getActivityStreamDuration(video.duration),
350 sensitive: video.nsfw,
351 waitTranscoding: video.waitTranscoding,
353 commentsEnabled: video.commentsEnabled,
354 downloadEnabled: video.downloadEnabled,
355 published: video.publishedAt.toISOString(),
356 originallyPublishedAt: video.originallyPublishedAt ? video.originallyPublishedAt.toISOString() : null,
357 updated: video.updatedAt.toISOString(),
358 mediaType: 'text/markdown',
359 content: video.description,
360 support: video.support,
362 icon: icons.map(i => ({
364 url: i.getFileUrl(video),
365 mediaType: 'image/jpeg',
370 likes: getVideoLikesActivityPubUrl(video),
371 dislikes: getVideoDislikesActivityPubUrl(video),
372 shares: getVideoSharesActivityPubUrl(video),
373 comments: getVideoCommentsActivityPubUrl(video),
377 id: video.VideoChannel.Account.Actor.url
381 id: video.VideoChannel.Actor.url
387 function getActivityStreamDuration (duration: number) {
388 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
389 return 'PT' + duration + 'S'
393 videoModelToFormattedJSON,
394 videoModelToFormattedDetailsJSON,
395 videoFilesModelToFormattedJSON,
396 videoModelToActivityPubObject,
397 getActivityStreamDuration