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
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 sortByResolutionDesc (fileA: MVideoFile, fileB: MVideoFile) {
179 if (fileA.resolution < fileB.resolution) return 1
180 if (fileA.resolution === fileB.resolution) return 0
184 function videoFilesModelToFormattedJSON (
185 model: MVideo | MStreamingPlaylistVideo,
188 videoFiles: MVideoFileRedundanciesOpt[]
190 const video = extractVideo(model)
192 return [ ...videoFiles ]
193 .sort(sortByResolutionDesc)
197 id: videoFile.resolution,
198 label: videoFile.resolution + 'p'
200 magnetUri: generateMagnetUri(model, videoFile, baseUrlHttp, baseUrlWs),
201 size: videoFile.size,
203 torrentUrl: model.getTorrentUrl(videoFile, baseUrlHttp),
204 torrentDownloadUrl: model.getTorrentDownloadUrl(videoFile, baseUrlHttp),
205 fileUrl: model.getVideoFileUrl(videoFile, baseUrlHttp),
206 fileDownloadUrl: model.getVideoFileDownloadUrl(videoFile, baseUrlHttp),
207 metadataUrl: video.getVideoFileMetadataUrl(videoFile, baseUrlHttp)
212 function addVideoFilesInAPAcc (
213 acc: ActivityUrlObject[] | ActivityTagObject[],
214 model: MVideoAP | MStreamingPlaylistVideo,
219 const sortedFiles = [ ...files ].sort(sortByResolutionDesc)
221 for (const file of sortedFiles) {
224 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any,
225 href: model.getVideoFileUrl(file, baseUrlHttp),
226 height: file.resolution,
233 rel: [ 'metadata', MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] ],
234 mediaType: 'application/json' as 'application/json',
235 href: extractVideo(model).getVideoFileMetadataUrl(file, baseUrlHttp),
236 height: file.resolution,
242 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
243 href: model.getTorrentUrl(file, baseUrlHttp),
244 height: file.resolution
249 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
250 href: generateMagnetUri(model, file, baseUrlHttp, baseUrlWs),
251 height: file.resolution
256 function videoModelToActivityPubObject (video: MVideoAP): VideoTorrentObject {
257 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
258 if (!video.Tags) video.Tags = []
260 const tag = video.Tags.map(t => ({
261 type: 'Hashtag' as 'Hashtag',
266 if (video.language) {
268 identifier: video.language,
269 name: VideoModel.getLanguageLabel(video.language)
274 if (video.category) {
276 identifier: video.category + '',
277 name: VideoModel.getCategoryLabel(video.category)
284 identifier: video.licence + '',
285 name: VideoModel.getLicenceLabel(video.licence)
289 const url: ActivityUrlObject[] = [
290 // HTML url should be the first element in the array so Mastodon correctly displays the embed
293 mediaType: 'text/html',
294 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
298 addVideoFilesInAPAcc(url, video, baseUrlHttp, baseUrlWs, video.VideoFiles || [])
300 for (const playlist of (video.VideoStreamingPlaylists || [])) {
301 const tag = playlist.p2pMediaLoaderInfohashes
302 .map(i => ({ type: 'Infohash' as 'Infohash', name: i })) as ActivityTagObject[]
306 mediaType: 'application/json' as 'application/json',
307 href: playlist.segmentsSha256Url
310 const playlistWithVideo = Object.assign(playlist, { Video: video })
311 addVideoFilesInAPAcc(tag, playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles || [])
315 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
316 href: playlist.playlistUrl,
321 const subtitleLanguage = []
322 for (const caption of video.VideoCaptions) {
323 subtitleLanguage.push({
324 identifier: caption.language,
325 name: VideoCaptionModel.getLanguageLabel(caption.language),
326 url: caption.getFileUrl(video)
330 const icons = [ video.getMiniature(), video.getPreview() ]
333 type: 'Video' as 'Video',
336 duration: getActivityStreamDuration(video.duration),
343 sensitive: video.nsfw,
344 waitTranscoding: video.waitTranscoding,
346 commentsEnabled: video.commentsEnabled,
347 downloadEnabled: video.downloadEnabled,
348 published: video.publishedAt.toISOString(),
349 originallyPublishedAt: video.originallyPublishedAt ? video.originallyPublishedAt.toISOString() : null,
350 updated: video.updatedAt.toISOString(),
351 mediaType: 'text/markdown',
352 content: video.description,
353 support: video.support,
355 icon: icons.map(i => ({
357 url: i.getFileUrl(video),
358 mediaType: 'image/jpeg',
363 likes: getVideoLikesActivityPubUrl(video),
364 dislikes: getVideoDislikesActivityPubUrl(video),
365 shares: getVideoSharesActivityPubUrl(video),
366 comments: getVideoCommentsActivityPubUrl(video),
370 id: video.VideoChannel.Account.Actor.url
374 id: video.VideoChannel.Actor.url
380 function getActivityStreamDuration (duration: number) {
381 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
382 return 'PT' + duration + 'S'
386 videoModelToFormattedJSON,
387 videoModelToFormattedDetailsJSON,
388 videoFilesModelToFormattedJSON,
389 videoModelToActivityPubObject,
390 getActivityStreamDuration