1 import { generateMagnetUri } from '@server/helpers/webtorrent'
2 import { getLocalVideoFileMetadataUrl } from '@server/lib/video-paths'
3 import { VideoFile } from '@shared/models/videos/video-file.model'
4 import { ActivityTagObject, ActivityUrlObject, VideoObject } from '../../../shared/models/activitypub/objects'
5 import { Video, VideoDetails } from '../../../shared/models/videos'
6 import { VideoStreamingPlaylist } from '../../../shared/models/videos/video-streaming-playlist.model'
7 import { isArray } from '../../helpers/custom-validators/misc'
8 import { MIMETYPES, WEBSERVER } from '../../initializers/constants'
10 getLocalVideoCommentsActivityPubUrl,
11 getLocalVideoDislikesActivityPubUrl,
12 getLocalVideoLikesActivityPubUrl,
13 getLocalVideoSharesActivityPubUrl
14 } from '../../lib/activitypub/url'
16 MStreamingPlaylistRedundanciesOpt,
17 MStreamingPlaylistVideo,
22 MVideoFormattableDetails,
24 } from '../../types/models'
25 import { MVideoFileRedundanciesOpt } from '../../types/models/video/video-file'
26 import { VideoModel } from './video'
27 import { VideoCaptionModel } from './video-caption'
29 export type VideoFormattingJSONOptions = {
30 completeDescription?: boolean
31 additionalAttributes: {
33 waitTranscoding?: boolean
34 scheduledUpdate?: boolean
35 blacklistInfo?: boolean
39 function videoModelToFormattedJSON (video: MVideoFormattable, options?: VideoFormattingJSONOptions): Video {
40 const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined
42 const videoObject: Video = {
48 label: VideoModel.getCategoryLabel(video.category)
52 label: VideoModel.getLicenceLabel(video.licence)
56 label: VideoModel.getLanguageLabel(video.language)
60 label: VideoModel.getPrivacyLabel(video.privacy)
64 description: options && options.completeDescription === true
66 : video.getTruncatedDescription(),
68 isLocal: video.isOwned(),
69 duration: video.duration,
72 dislikes: video.dislikes,
73 thumbnailPath: video.getMiniatureStaticPath(),
74 previewPath: video.getPreviewStaticPath(),
75 embedPath: video.getEmbedStaticPath(),
76 createdAt: video.createdAt,
77 updatedAt: video.updatedAt,
78 publishedAt: video.publishedAt,
79 originallyPublishedAt: video.originallyPublishedAt,
83 account: video.VideoChannel.Account.toFormattedSummaryJSON(),
84 channel: video.VideoChannel.toFormattedSummaryJSON(),
86 userHistory: userHistory
87 ? { currentTime: userHistory.currentTime }
90 // Can be added by external plugins
91 pluginData: (video as any).pluginData
95 if (options.additionalAttributes.state === true) {
98 label: VideoModel.getStateLabel(video.state)
102 if (options.additionalAttributes.waitTranscoding === true) {
103 videoObject.waitTranscoding = video.waitTranscoding
106 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
107 videoObject.scheduledUpdate = {
108 updateAt: video.ScheduleVideoUpdate.updateAt,
109 privacy: video.ScheduleVideoUpdate.privacy || undefined
113 if (options.additionalAttributes.blacklistInfo === true) {
114 videoObject.blacklisted = !!video.VideoBlacklist
115 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
122 function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails {
123 const formattedJson = video.toFormattedJSON({
124 additionalAttributes: {
125 scheduledUpdate: true,
130 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
132 const tags = video.Tags ? video.Tags.map(t => t.name) : []
134 const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
136 const detailsJson = {
137 support: video.support,
138 descriptionPath: video.getDescriptionAPIPath(),
139 channel: video.VideoChannel.toFormattedJSON(),
140 account: video.VideoChannel.Account.toFormattedJSON(),
142 commentsEnabled: video.commentsEnabled,
143 downloadEnabled: video.downloadEnabled,
144 waitTranscoding: video.waitTranscoding,
147 label: VideoModel.getStateLabel(video.state)
150 trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs),
156 // Format and sort video files
157 detailsJson.files = videoFilesModelToFormattedJSON(video, video, baseUrlHttp, baseUrlWs, video.VideoFiles)
159 return Object.assign(formattedJson, detailsJson)
162 function streamingPlaylistsModelToFormattedJSON (
163 video: MVideoFormattableDetails,
164 playlists: MStreamingPlaylistRedundanciesOpt[]
165 ): VideoStreamingPlaylist[] {
166 if (isArray(playlists) === false) return []
168 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
172 const playlistWithVideo = Object.assign(playlist, { Video: video })
174 const redundancies = isArray(playlist.RedundancyVideos)
175 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
178 const files = videoFilesModelToFormattedJSON(playlistWithVideo, video, baseUrlHttp, baseUrlWs, playlist.VideoFiles)
183 playlistUrl: playlist.playlistUrl,
184 segmentsSha256Url: playlist.segmentsSha256Url,
191 function sortByResolutionDesc (fileA: MVideoFile, fileB: MVideoFile) {
192 if (fileA.resolution < fileB.resolution) return 1
193 if (fileA.resolution === fileB.resolution) return 0
197 // FIXME: refactor/merge model and video arguments
198 function videoFilesModelToFormattedJSON (
199 model: MVideo | MStreamingPlaylistVideo,
200 video: MVideoFormattableDetails,
203 videoFiles: MVideoFileRedundanciesOpt[]
205 return [ ...videoFiles ]
206 .filter(f => !f.isLive())
207 .sort(sortByResolutionDesc)
211 id: videoFile.resolution,
212 label: videoFile.resolution + 'p'
215 // FIXME: deprecated in 3.2
216 magnetUri: generateMagnetUri(model, video, videoFile, baseUrlHttp, baseUrlWs),
218 size: videoFile.size,
221 torrentUrl: videoFile.getTorrentUrl(),
222 torrentDownloadUrl: videoFile.getTorrentDownloadUrl(),
224 fileUrl: videoFile.getFileUrl(video),
225 fileDownloadUrl: videoFile.getFileDownloadUrl(video),
227 metadataUrl: videoFile.metadataUrl ?? getLocalVideoFileMetadataUrl(video, videoFile)
232 // FIXME: refactor/merge model and video arguments
233 function addVideoFilesInAPAcc (
234 acc: ActivityUrlObject[] | ActivityTagObject[],
235 model: MVideoAP | MStreamingPlaylistVideo,
236 video: MVideoWithHost,
241 const sortedFiles = [ ...files ]
242 .filter(f => !f.isLive())
243 .sort(sortByResolutionDesc)
245 for (const file of sortedFiles) {
248 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any,
249 href: file.getFileUrl(video),
250 height: file.resolution,
257 rel: [ 'metadata', MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] ],
258 mediaType: 'application/json' as 'application/json',
259 href: getLocalVideoFileMetadataUrl(video, file),
260 height: file.resolution,
266 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
267 href: file.getTorrentUrl(),
268 height: file.resolution
273 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
274 href: generateMagnetUri(model, video, file, baseUrlHttp, baseUrlWs),
275 height: file.resolution
280 function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
281 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
282 if (!video.Tags) video.Tags = []
284 const tag = video.Tags.map(t => ({
285 type: 'Hashtag' as 'Hashtag',
290 if (video.language) {
292 identifier: video.language,
293 name: VideoModel.getLanguageLabel(video.language)
298 if (video.category) {
300 identifier: video.category + '',
301 name: VideoModel.getCategoryLabel(video.category)
308 identifier: video.licence + '',
309 name: VideoModel.getLicenceLabel(video.licence)
313 const url: ActivityUrlObject[] = [
314 // HTML url should be the first element in the array so Mastodon correctly displays the embed
317 mediaType: 'text/html',
318 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
322 addVideoFilesInAPAcc(url, video, video, baseUrlHttp, baseUrlWs, video.VideoFiles || [])
324 for (const playlist of (video.VideoStreamingPlaylists || [])) {
325 const tag = playlist.p2pMediaLoaderInfohashes
326 .map(i => ({ type: 'Infohash' as 'Infohash', name: i })) as ActivityTagObject[]
330 mediaType: 'application/json' as 'application/json',
331 href: playlist.segmentsSha256Url
334 const playlistWithVideo = Object.assign(playlist, { Video: video })
335 addVideoFilesInAPAcc(tag, playlistWithVideo, video, baseUrlHttp, baseUrlWs, playlist.VideoFiles || [])
339 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
340 href: playlist.playlistUrl,
345 const subtitleLanguage = []
346 for (const caption of video.VideoCaptions) {
347 subtitleLanguage.push({
348 identifier: caption.language,
349 name: VideoCaptionModel.getLanguageLabel(caption.language),
350 url: caption.getFileUrl(video)
354 const icons = [ video.getMiniature(), video.getPreview() ]
357 type: 'Video' as 'Video',
360 duration: getActivityStreamDuration(video.duration),
367 sensitive: video.nsfw,
368 waitTranscoding: video.waitTranscoding,
369 isLiveBroadcast: video.isLive,
371 liveSaveReplay: video.isLive
372 ? video.VideoLive.saveReplay
375 permanentLive: video.isLive
376 ? video.VideoLive.permanentLive
380 commentsEnabled: video.commentsEnabled,
381 downloadEnabled: video.downloadEnabled,
382 published: video.publishedAt.toISOString(),
384 originallyPublishedAt: video.originallyPublishedAt
385 ? video.originallyPublishedAt.toISOString()
388 updated: video.updatedAt.toISOString(),
389 mediaType: 'text/markdown',
390 content: video.description,
391 support: video.support,
393 icon: icons.map(i => ({
395 url: i.getFileUrl(video),
396 mediaType: 'image/jpeg',
401 likes: getLocalVideoLikesActivityPubUrl(video),
402 dislikes: getLocalVideoDislikesActivityPubUrl(video),
403 shares: getLocalVideoSharesActivityPubUrl(video),
404 comments: getLocalVideoCommentsActivityPubUrl(video),
408 id: video.VideoChannel.Account.Actor.url
412 id: video.VideoChannel.Actor.url
418 function getActivityStreamDuration (duration: number) {
419 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
420 return 'PT' + duration + 'S'
424 videoModelToFormattedJSON,
425 videoModelToFormattedDetailsJSON,
426 videoFilesModelToFormattedJSON,
427 videoModelToActivityPubObject,
428 getActivityStreamDuration