1 import { Video, VideoDetails } from '../../../shared/models/videos'
2 import { VideoModel } from './video'
3 import { ActivityTagObject, ActivityUrlObject, VideoObject } from '../../../shared/models/activitypub/objects'
4 import { MIMETYPES, WEBSERVER } from '../../initializers/constants'
5 import { VideoCaptionModel } from './video-caption'
7 getLocalVideoCommentsActivityPubUrl,
8 getLocalVideoDislikesActivityPubUrl,
9 getLocalVideoLikesActivityPubUrl,
10 getLocalVideoSharesActivityPubUrl
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,
82 account: video.VideoChannel.Account.toFormattedSummaryJSON(),
83 channel: video.VideoChannel.toFormattedSummaryJSON(),
85 userHistory: userHistory ? {
86 currentTime: userHistory.currentTime
89 // Can be added by external plugins
90 pluginData: (video as any).pluginData
94 if (options.additionalAttributes.state === true) {
97 label: VideoModel.getStateLabel(video.state)
101 if (options.additionalAttributes.waitTranscoding === true) {
102 videoObject.waitTranscoding = video.waitTranscoding
105 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
106 videoObject.scheduledUpdate = {
107 updateAt: video.ScheduleVideoUpdate.updateAt,
108 privacy: video.ScheduleVideoUpdate.privacy || undefined
112 if (options.additionalAttributes.blacklistInfo === true) {
113 videoObject.blacklisted = !!video.VideoBlacklist
114 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
121 function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails {
122 const formattedJson = video.toFormattedJSON({
123 additionalAttributes: {
124 scheduledUpdate: true,
129 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
131 const tags = video.Tags ? video.Tags.map(t => t.name) : []
133 const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
135 const detailsJson = {
136 support: video.support,
137 descriptionPath: video.getDescriptionAPIPath(),
138 channel: video.VideoChannel.toFormattedJSON(),
139 account: video.VideoChannel.Account.toFormattedJSON(),
141 commentsEnabled: video.commentsEnabled,
142 downloadEnabled: video.downloadEnabled,
143 waitTranscoding: video.waitTranscoding,
146 label: VideoModel.getStateLabel(video.state)
149 trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs),
155 // Format and sort video files
156 detailsJson.files = videoFilesModelToFormattedJSON(video, baseUrlHttp, baseUrlWs, video.VideoFiles)
158 return Object.assign(formattedJson, detailsJson)
161 function streamingPlaylistsModelToFormattedJSON (video: MVideo, playlists: MStreamingPlaylistRedundanciesOpt[]): VideoStreamingPlaylist[] {
162 if (isArray(playlists) === false) return []
164 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
168 const playlistWithVideo = Object.assign(playlist, { Video: video })
170 const redundancies = isArray(playlist.RedundancyVideos)
171 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
174 const files = videoFilesModelToFormattedJSON(playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles)
179 playlistUrl: playlist.playlistUrl,
180 segmentsSha256Url: playlist.segmentsSha256Url,
187 function sortByResolutionDesc (fileA: MVideoFile, fileB: MVideoFile) {
188 if (fileA.resolution < fileB.resolution) return 1
189 if (fileA.resolution === fileB.resolution) return 0
193 function videoFilesModelToFormattedJSON (
194 model: MVideo | MStreamingPlaylistVideo,
197 videoFiles: MVideoFileRedundanciesOpt[]
199 const video = extractVideo(model)
201 return [ ...videoFiles ]
202 .filter(f => !f.isLive())
203 .sort(sortByResolutionDesc)
207 id: videoFile.resolution,
208 label: videoFile.resolution + 'p'
210 magnetUri: generateMagnetUri(model, videoFile, baseUrlHttp, baseUrlWs),
211 size: videoFile.size,
213 torrentUrl: model.getTorrentUrl(videoFile, baseUrlHttp),
214 torrentDownloadUrl: model.getTorrentDownloadUrl(videoFile, baseUrlHttp),
215 fileUrl: model.getVideoFileUrl(videoFile, baseUrlHttp),
216 fileDownloadUrl: model.getVideoFileDownloadUrl(videoFile, baseUrlHttp),
217 metadataUrl: video.getVideoFileMetadataUrl(videoFile, baseUrlHttp)
222 function addVideoFilesInAPAcc (
223 acc: ActivityUrlObject[] | ActivityTagObject[],
224 model: MVideoAP | MStreamingPlaylistVideo,
229 const sortedFiles = [ ...files ]
230 .filter(f => !f.isLive())
231 .sort(sortByResolutionDesc)
233 for (const file of sortedFiles) {
236 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any,
237 href: model.getVideoFileUrl(file, baseUrlHttp),
238 height: file.resolution,
245 rel: [ 'metadata', MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] ],
246 mediaType: 'application/json' as 'application/json',
247 href: extractVideo(model).getVideoFileMetadataUrl(file, baseUrlHttp),
248 height: file.resolution,
254 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
255 href: model.getTorrentUrl(file, baseUrlHttp),
256 height: file.resolution
261 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
262 href: generateMagnetUri(model, file, baseUrlHttp, baseUrlWs),
263 height: file.resolution
268 function videoModelToActivityPubObject (video: MVideoAP): VideoObject {
269 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
270 if (!video.Tags) video.Tags = []
272 const tag = video.Tags.map(t => ({
273 type: 'Hashtag' as 'Hashtag',
278 if (video.language) {
280 identifier: video.language,
281 name: VideoModel.getLanguageLabel(video.language)
286 if (video.category) {
288 identifier: video.category + '',
289 name: VideoModel.getCategoryLabel(video.category)
296 identifier: video.licence + '',
297 name: VideoModel.getLicenceLabel(video.licence)
301 const url: ActivityUrlObject[] = [
302 // HTML url should be the first element in the array so Mastodon correctly displays the embed
305 mediaType: 'text/html',
306 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
310 addVideoFilesInAPAcc(url, video, baseUrlHttp, baseUrlWs, video.VideoFiles || [])
312 for (const playlist of (video.VideoStreamingPlaylists || [])) {
313 const tag = playlist.p2pMediaLoaderInfohashes
314 .map(i => ({ type: 'Infohash' as 'Infohash', name: i })) as ActivityTagObject[]
318 mediaType: 'application/json' as 'application/json',
319 href: playlist.segmentsSha256Url
322 const playlistWithVideo = Object.assign(playlist, { Video: video })
323 addVideoFilesInAPAcc(tag, playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles || [])
327 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
328 href: playlist.playlistUrl,
333 const subtitleLanguage = []
334 for (const caption of video.VideoCaptions) {
335 subtitleLanguage.push({
336 identifier: caption.language,
337 name: VideoCaptionModel.getLanguageLabel(caption.language),
338 url: caption.getFileUrl(video)
342 const icons = [ video.getMiniature(), video.getPreview() ]
345 type: 'Video' as 'Video',
348 duration: getActivityStreamDuration(video.duration),
355 sensitive: video.nsfw,
356 waitTranscoding: video.waitTranscoding,
357 isLiveBroadcast: video.isLive,
359 liveSaveReplay: video.isLive
360 ? video.VideoLive.saveReplay
364 commentsEnabled: video.commentsEnabled,
365 downloadEnabled: video.downloadEnabled,
366 published: video.publishedAt.toISOString(),
368 originallyPublishedAt: video.originallyPublishedAt
369 ? video.originallyPublishedAt.toISOString()
372 updated: video.updatedAt.toISOString(),
373 mediaType: 'text/markdown',
374 content: video.description,
375 support: video.support,
377 icon: icons.map(i => ({
379 url: i.getFileUrl(video),
380 mediaType: 'image/jpeg',
385 likes: getLocalVideoLikesActivityPubUrl(video),
386 dislikes: getLocalVideoDislikesActivityPubUrl(video),
387 shares: getLocalVideoSharesActivityPubUrl(video),
388 comments: getLocalVideoCommentsActivityPubUrl(video),
392 id: video.VideoChannel.Account.Actor.url
396 id: video.VideoChannel.Actor.url
402 function getActivityStreamDuration (duration: number) {
403 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
404 return 'PT' + duration + 'S'
408 videoModelToFormattedJSON,
409 videoModelToFormattedDetailsJSON,
410 videoFilesModelToFormattedJSON,
411 videoModelToActivityPubObject,
412 getActivityStreamDuration