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[]
188 id: videoFile.resolution,
189 label: videoFile.resolution + 'p'
191 magnetUri: generateMagnetUri(model, videoFile, baseUrlHttp, baseUrlWs),
192 size: videoFile.size,
194 torrentUrl: model.getTorrentUrl(videoFile, baseUrlHttp),
195 torrentDownloadUrl: model.getTorrentDownloadUrl(videoFile, baseUrlHttp),
196 fileUrl: model.getVideoFileUrl(videoFile, baseUrlHttp),
197 fileDownloadUrl: model.getVideoFileDownloadUrl(videoFile, baseUrlHttp),
198 metadataUrl: videoFile.metadataUrl // only send the metadataUrl and not the metadata over the wire
202 if (a.resolution.id < b.resolution.id) return 1
203 if (a.resolution.id === b.resolution.id) return 0
208 function addVideoFilesInAPAcc (
209 acc: ActivityUrlObject[] | ActivityTagObject[],
210 model: MVideoAP | MStreamingPlaylistVideo,
215 for (const file of files) {
218 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any,
219 href: model.getVideoFileUrl(file, baseUrlHttp),
220 height: file.resolution,
227 rel: [ 'metadata', MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] ],
228 mediaType: 'application/json' as 'application/json',
229 href: extractVideo(model).getVideoFileMetadataUrl(file, baseUrlHttp),
230 height: file.resolution,
236 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
237 href: model.getTorrentUrl(file, baseUrlHttp),
238 height: file.resolution
243 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
244 href: generateMagnetUri(model, file, baseUrlHttp, baseUrlWs),
245 height: file.resolution
250 function videoModelToActivityPubObject (video: MVideoAP): VideoTorrentObject {
251 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
252 if (!video.Tags) video.Tags = []
254 const tag = video.Tags.map(t => ({
255 type: 'Hashtag' as 'Hashtag',
260 if (video.language) {
262 identifier: video.language,
263 name: VideoModel.getLanguageLabel(video.language)
268 if (video.category) {
270 identifier: video.category + '',
271 name: VideoModel.getCategoryLabel(video.category)
278 identifier: video.licence + '',
279 name: VideoModel.getLicenceLabel(video.licence)
283 const url: ActivityUrlObject[] = [
284 // HTML url should be the first element in the array so Mastodon correctly displays the embed
287 mediaType: 'text/html',
288 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
292 addVideoFilesInAPAcc(url, video, baseUrlHttp, baseUrlWs, video.VideoFiles || [])
294 for (const playlist of (video.VideoStreamingPlaylists || [])) {
295 const tag = playlist.p2pMediaLoaderInfohashes
296 .map(i => ({ type: 'Infohash' as 'Infohash', name: i })) as ActivityTagObject[]
300 mediaType: 'application/json' as 'application/json',
301 href: playlist.segmentsSha256Url
304 const playlistWithVideo = Object.assign(playlist, { Video: video })
305 addVideoFilesInAPAcc(tag, playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles || [])
309 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
310 href: playlist.playlistUrl,
315 const subtitleLanguage = []
316 for (const caption of video.VideoCaptions) {
317 subtitleLanguage.push({
318 identifier: caption.language,
319 name: VideoCaptionModel.getLanguageLabel(caption.language),
320 url: caption.getFileUrl(video)
324 const icons = [ video.getMiniature(), video.getPreview() ]
327 type: 'Video' as 'Video',
330 duration: getActivityStreamDuration(video.duration),
337 sensitive: video.nsfw,
338 waitTranscoding: video.waitTranscoding,
340 commentsEnabled: video.commentsEnabled,
341 downloadEnabled: video.downloadEnabled,
342 published: video.publishedAt.toISOString(),
343 originallyPublishedAt: video.originallyPublishedAt ? video.originallyPublishedAt.toISOString() : null,
344 updated: video.updatedAt.toISOString(),
345 mediaType: 'text/markdown',
346 content: video.getTruncatedDescription(),
347 support: video.support,
349 icon: icons.map(i => ({
351 url: i.getFileUrl(video),
352 mediaType: 'image/jpeg',
357 likes: getVideoLikesActivityPubUrl(video),
358 dislikes: getVideoDislikesActivityPubUrl(video),
359 shares: getVideoSharesActivityPubUrl(video),
360 comments: getVideoCommentsActivityPubUrl(video),
364 id: video.VideoChannel.Account.Actor.url
368 id: video.VideoChannel.Actor.url
374 function getActivityStreamDuration (duration: number) {
375 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
376 return 'PT' + duration + 'S'
380 videoModelToFormattedJSON,
381 videoModelToFormattedDetailsJSON,
382 videoFilesModelToFormattedJSON,
383 videoModelToActivityPubObject,
384 getActivityStreamDuration