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)
62 description: options && options.completeDescription === true ? video.description : video.getTruncatedDescription(),
63 isLocal: video.isOwned(),
64 duration: video.duration,
67 dislikes: video.dislikes,
68 thumbnailPath: video.getMiniatureStaticPath(),
69 previewPath: video.getPreviewStaticPath(),
70 embedPath: video.getEmbedStaticPath(),
71 createdAt: video.createdAt,
72 updatedAt: video.updatedAt,
73 publishedAt: video.publishedAt,
74 originallyPublishedAt: video.originallyPublishedAt,
76 account: video.VideoChannel.Account.toFormattedSummaryJSON(),
77 channel: video.VideoChannel.toFormattedSummaryJSON(),
79 userHistory: userHistory ? {
80 currentTime: userHistory.currentTime
83 // Can be added by external plugins
84 pluginData: (video as any).pluginData
88 if (options.additionalAttributes.state === true) {
91 label: VideoModel.getStateLabel(video.state)
95 if (options.additionalAttributes.waitTranscoding === true) {
96 videoObject.waitTranscoding = video.waitTranscoding
99 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
100 videoObject.scheduledUpdate = {
101 updateAt: video.ScheduleVideoUpdate.updateAt,
102 privacy: video.ScheduleVideoUpdate.privacy || undefined
106 if (options.additionalAttributes.blacklistInfo === true) {
107 videoObject.blacklisted = !!video.VideoBlacklist
108 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
115 function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails {
116 const formattedJson = video.toFormattedJSON({
117 additionalAttributes: {
118 scheduledUpdate: true,
123 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
125 const tags = video.Tags ? video.Tags.map(t => t.name) : []
127 const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
129 const detailsJson = {
130 support: video.support,
131 descriptionPath: video.getDescriptionAPIPath(),
132 channel: video.VideoChannel.toFormattedJSON(),
133 account: video.VideoChannel.Account.toFormattedJSON(),
135 commentsEnabled: video.commentsEnabled,
136 downloadEnabled: video.downloadEnabled,
137 waitTranscoding: video.waitTranscoding,
140 label: VideoModel.getStateLabel(video.state)
143 trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs),
149 // Format and sort video files
150 detailsJson.files = videoFilesModelToFormattedJSON(video, baseUrlHttp, baseUrlWs, video.VideoFiles)
152 return Object.assign(formattedJson, detailsJson)
155 function streamingPlaylistsModelToFormattedJSON (video: MVideo, playlists: MStreamingPlaylistRedundanciesOpt[]): VideoStreamingPlaylist[] {
156 if (isArray(playlists) === false) return []
158 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
162 const playlistWithVideo = Object.assign(playlist, { Video: video })
164 const redundancies = isArray(playlist.RedundancyVideos)
165 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
168 const files = videoFilesModelToFormattedJSON(playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles)
173 playlistUrl: playlist.playlistUrl,
174 segmentsSha256Url: playlist.segmentsSha256Url,
181 function sortByResolutionDesc (fileA: MVideoFile, fileB: MVideoFile) {
182 if (fileA.resolution < fileB.resolution) return 1
183 if (fileA.resolution === fileB.resolution) return 0
187 function videoFilesModelToFormattedJSON (
188 model: MVideo | MStreamingPlaylistVideo,
191 videoFiles: MVideoFileRedundanciesOpt[]
193 const video = extractVideo(model)
195 return [ ...videoFiles ]
196 .sort(sortByResolutionDesc)
200 id: videoFile.resolution,
201 label: videoFile.resolution + 'p'
203 magnetUri: generateMagnetUri(model, videoFile, baseUrlHttp, baseUrlWs),
204 size: videoFile.size,
206 torrentUrl: model.getTorrentUrl(videoFile, baseUrlHttp),
207 torrentDownloadUrl: model.getTorrentDownloadUrl(videoFile, baseUrlHttp),
208 fileUrl: model.getVideoFileUrl(videoFile, baseUrlHttp),
209 fileDownloadUrl: model.getVideoFileDownloadUrl(videoFile, baseUrlHttp),
210 metadataUrl: video.getVideoFileMetadataUrl(videoFile, baseUrlHttp)
215 function addVideoFilesInAPAcc (
216 acc: ActivityUrlObject[] | ActivityTagObject[],
217 model: MVideoAP | MStreamingPlaylistVideo,
222 const sortedFiles = [ ...files ].sort(sortByResolutionDesc)
224 for (const file of sortedFiles) {
227 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any,
228 href: model.getVideoFileUrl(file, baseUrlHttp),
229 height: file.resolution,
236 rel: [ 'metadata', MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] ],
237 mediaType: 'application/json' as 'application/json',
238 href: extractVideo(model).getVideoFileMetadataUrl(file, baseUrlHttp),
239 height: file.resolution,
245 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
246 href: model.getTorrentUrl(file, baseUrlHttp),
247 height: file.resolution
252 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
253 href: generateMagnetUri(model, file, baseUrlHttp, baseUrlWs),
254 height: file.resolution
259 function videoModelToActivityPubObject (video: MVideoAP): VideoTorrentObject {
260 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
261 if (!video.Tags) video.Tags = []
263 const tag = video.Tags.map(t => ({
264 type: 'Hashtag' as 'Hashtag',
269 if (video.language) {
271 identifier: video.language,
272 name: VideoModel.getLanguageLabel(video.language)
277 if (video.category) {
279 identifier: video.category + '',
280 name: VideoModel.getCategoryLabel(video.category)
287 identifier: video.licence + '',
288 name: VideoModel.getLicenceLabel(video.licence)
292 const url: ActivityUrlObject[] = [
293 // HTML url should be the first element in the array so Mastodon correctly displays the embed
296 mediaType: 'text/html',
297 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
301 addVideoFilesInAPAcc(url, video, baseUrlHttp, baseUrlWs, video.VideoFiles || [])
303 for (const playlist of (video.VideoStreamingPlaylists || [])) {
304 const tag = playlist.p2pMediaLoaderInfohashes
305 .map(i => ({ type: 'Infohash' as 'Infohash', name: i })) as ActivityTagObject[]
309 mediaType: 'application/json' as 'application/json',
310 href: playlist.segmentsSha256Url
313 const playlistWithVideo = Object.assign(playlist, { Video: video })
314 addVideoFilesInAPAcc(tag, playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles || [])
318 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
319 href: playlist.playlistUrl,
324 const subtitleLanguage = []
325 for (const caption of video.VideoCaptions) {
326 subtitleLanguage.push({
327 identifier: caption.language,
328 name: VideoCaptionModel.getLanguageLabel(caption.language),
329 url: caption.getFileUrl(video)
333 const icons = [ video.getMiniature(), video.getPreview() ]
336 type: 'Video' as 'Video',
339 duration: getActivityStreamDuration(video.duration),
346 sensitive: video.nsfw,
347 waitTranscoding: video.waitTranscoding,
349 commentsEnabled: video.commentsEnabled,
350 downloadEnabled: video.downloadEnabled,
351 published: video.publishedAt.toISOString(),
352 originallyPublishedAt: video.originallyPublishedAt ? video.originallyPublishedAt.toISOString() : null,
353 updated: video.updatedAt.toISOString(),
354 mediaType: 'text/markdown',
355 content: video.description,
356 support: video.support,
358 icon: icons.map(i => ({
360 url: i.getFileUrl(video),
361 mediaType: 'image/jpeg',
366 likes: getVideoLikesActivityPubUrl(video),
367 dislikes: getVideoDislikesActivityPubUrl(video),
368 shares: getVideoSharesActivityPubUrl(video),
369 comments: getVideoCommentsActivityPubUrl(video),
373 id: video.VideoChannel.Account.Actor.url
377 id: video.VideoChannel.Actor.url
383 function getActivityStreamDuration (duration: number) {
384 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
385 return 'PT' + duration + 'S'
389 videoModelToFormattedJSON,
390 videoModelToFormattedDetailsJSON,
391 videoFilesModelToFormattedJSON,
392 videoModelToActivityPubObject,
393 getActivityStreamDuration