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'
26 import { extractVideo } from '@server/lib/videos'
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
85 if (options.additionalAttributes.state === true) {
88 label: VideoModel.getStateLabel(video.state)
92 if (options.additionalAttributes.waitTranscoding === true) {
93 videoObject.waitTranscoding = video.waitTranscoding
96 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
97 videoObject.scheduledUpdate = {
98 updateAt: video.ScheduleVideoUpdate.updateAt,
99 privacy: video.ScheduleVideoUpdate.privacy || undefined
103 if (options.additionalAttributes.blacklistInfo === true) {
104 videoObject.blacklisted = !!video.VideoBlacklist
105 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
112 function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails {
113 const formattedJson = video.toFormattedJSON({
114 additionalAttributes: {
115 scheduledUpdate: true,
120 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
122 const tags = video.Tags ? video.Tags.map(t => t.name) : []
124 const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
126 const detailsJson = {
127 support: video.support,
128 descriptionPath: video.getDescriptionAPIPath(),
129 channel: video.VideoChannel.toFormattedJSON(),
130 account: video.VideoChannel.Account.toFormattedJSON(),
132 commentsEnabled: video.commentsEnabled,
133 downloadEnabled: video.downloadEnabled,
134 waitTranscoding: video.waitTranscoding,
137 label: VideoModel.getStateLabel(video.state)
140 trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs),
146 // Format and sort video files
147 detailsJson.files = videoFilesModelToFormattedJSON(video, baseUrlHttp, baseUrlWs, video.VideoFiles)
149 return Object.assign(formattedJson, detailsJson)
152 function streamingPlaylistsModelToFormattedJSON (video: MVideo, playlists: MStreamingPlaylistRedundanciesOpt[]): VideoStreamingPlaylist[] {
153 if (isArray(playlists) === false) return []
155 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
159 const playlistWithVideo = Object.assign(playlist, { Video: video })
161 const redundancies = isArray(playlist.RedundancyVideos)
162 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
165 const files = videoFilesModelToFormattedJSON(playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles)
170 playlistUrl: playlist.playlistUrl,
171 segmentsSha256Url: playlist.segmentsSha256Url,
178 function videoFilesModelToFormattedJSON (
179 model: MVideo | MStreamingPlaylistVideo,
182 videoFiles: MVideoFileRedundanciesOpt[]
184 const video = extractVideo(model)
190 id: videoFile.resolution,
191 label: videoFile.resolution + 'p'
193 magnetUri: generateMagnetUri(model, videoFile, baseUrlHttp, baseUrlWs),
194 size: videoFile.size,
196 torrentUrl: model.getTorrentUrl(videoFile, baseUrlHttp),
197 torrentDownloadUrl: model.getTorrentDownloadUrl(videoFile, baseUrlHttp),
198 fileUrl: model.getVideoFileUrl(videoFile, baseUrlHttp),
199 fileDownloadUrl: model.getVideoFileDownloadUrl(videoFile, baseUrlHttp),
200 metadataUrl: video.getVideoFileMetadataUrl(videoFile, baseUrlHttp)
204 if (a.resolution.id < b.resolution.id) return 1
205 if (a.resolution.id === b.resolution.id) return 0
210 function addVideoFilesInAPAcc (
211 acc: ActivityUrlObject[] | ActivityTagObject[],
212 model: MVideoAP | MStreamingPlaylistVideo,
217 for (const file of files) {
220 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any,
221 href: model.getVideoFileUrl(file, baseUrlHttp),
222 height: file.resolution,
229 rel: [ 'metadata', MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] ],
230 mediaType: 'application/json' as 'application/json',
231 href: extractVideo(model).getVideoFileMetadataUrl(file, baseUrlHttp),
232 height: file.resolution,
238 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
239 href: model.getTorrentUrl(file, baseUrlHttp),
240 height: file.resolution
245 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
246 href: generateMagnetUri(model, file, baseUrlHttp, baseUrlWs),
247 height: file.resolution
252 function videoModelToActivityPubObject (video: MVideoAP): VideoTorrentObject {
253 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
254 if (!video.Tags) video.Tags = []
256 const tag = video.Tags.map(t => ({
257 type: 'Hashtag' as 'Hashtag',
262 if (video.language) {
264 identifier: video.language,
265 name: VideoModel.getLanguageLabel(video.language)
270 if (video.category) {
272 identifier: video.category + '',
273 name: VideoModel.getCategoryLabel(video.category)
280 identifier: video.licence + '',
281 name: VideoModel.getLicenceLabel(video.licence)
285 const url: ActivityUrlObject[] = [
286 // HTML url should be the first element in the array so Mastodon correctly displays the embed
289 mediaType: 'text/html',
290 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
294 addVideoFilesInAPAcc(url, video, baseUrlHttp, baseUrlWs, video.VideoFiles || [])
296 for (const playlist of (video.VideoStreamingPlaylists || [])) {
297 const tag = playlist.p2pMediaLoaderInfohashes
298 .map(i => ({ type: 'Infohash' as 'Infohash', name: i })) as ActivityTagObject[]
302 mediaType: 'application/json' as 'application/json',
303 href: playlist.segmentsSha256Url
306 const playlistWithVideo = Object.assign(playlist, { Video: video })
307 addVideoFilesInAPAcc(tag, playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles || [])
311 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
312 href: playlist.playlistUrl,
317 const subtitleLanguage = []
318 for (const caption of video.VideoCaptions) {
319 subtitleLanguage.push({
320 identifier: caption.language,
321 name: VideoCaptionModel.getLanguageLabel(caption.language),
322 url: caption.getFileUrl(video)
326 const icons = [ video.getMiniature(), video.getPreview() ]
329 type: 'Video' as 'Video',
332 duration: getActivityStreamDuration(video.duration),
339 sensitive: video.nsfw,
340 waitTranscoding: video.waitTranscoding,
342 commentsEnabled: video.commentsEnabled,
343 downloadEnabled: video.downloadEnabled,
344 published: video.publishedAt.toISOString(),
345 originallyPublishedAt: video.originallyPublishedAt ? video.originallyPublishedAt.toISOString() : null,
346 updated: video.updatedAt.toISOString(),
347 mediaType: 'text/markdown',
348 content: video.getTruncatedDescription(),
349 support: video.support,
351 icon: icons.map(i => ({
353 url: i.getFileUrl(video),
354 mediaType: 'image/jpeg',
359 likes: getVideoLikesActivityPubUrl(video),
360 dislikes: getVideoDislikesActivityPubUrl(video),
361 shares: getVideoSharesActivityPubUrl(video),
362 comments: getVideoCommentsActivityPubUrl(video),
366 id: video.VideoChannel.Account.Actor.url
370 id: video.VideoChannel.Actor.url
376 function getActivityStreamDuration (duration: number) {
377 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
378 return 'PT' + duration + 'S'
382 videoModelToFormattedJSON,
383 videoModelToFormattedDetailsJSON,
384 videoFilesModelToFormattedJSON,
385 videoModelToActivityPubObject,
386 getActivityStreamDuration