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'
27 export type VideoFormattingJSONOptions = {
28 completeDescription?: boolean
29 additionalAttributes: {
31 waitTranscoding?: boolean
32 scheduledUpdate?: boolean
33 blacklistInfo?: boolean
37 function videoModelToFormattedJSON (video: MVideoFormattable, options?: VideoFormattingJSONOptions): Video {
38 const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined
40 const videoObject: Video = {
46 label: VideoModel.getCategoryLabel(video.category)
50 label: VideoModel.getLicenceLabel(video.licence)
54 label: VideoModel.getLanguageLabel(video.language)
58 label: VideoModel.getPrivacyLabel(video.privacy)
61 description: options && options.completeDescription === true ? video.description : video.getTruncatedDescription(),
62 isLocal: video.isOwned(),
63 duration: video.duration,
66 dislikes: video.dislikes,
67 thumbnailPath: video.getMiniatureStaticPath(),
68 previewPath: video.getPreviewStaticPath(),
69 embedPath: video.getEmbedStaticPath(),
70 createdAt: video.createdAt,
71 updatedAt: video.updatedAt,
72 publishedAt: video.publishedAt,
73 originallyPublishedAt: video.originallyPublishedAt,
75 account: video.VideoChannel.Account.toFormattedSummaryJSON(),
76 channel: video.VideoChannel.toFormattedSummaryJSON(),
78 userHistory: userHistory ? {
79 currentTime: userHistory.currentTime
84 if (options.additionalAttributes.state === true) {
87 label: VideoModel.getStateLabel(video.state)
91 if (options.additionalAttributes.waitTranscoding === true) {
92 videoObject.waitTranscoding = video.waitTranscoding
95 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
96 videoObject.scheduledUpdate = {
97 updateAt: video.ScheduleVideoUpdate.updateAt,
98 privacy: video.ScheduleVideoUpdate.privacy || undefined
102 if (options.additionalAttributes.blacklistInfo === true) {
103 videoObject.blacklisted = !!video.VideoBlacklist
104 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
111 function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails {
112 const formattedJson = video.toFormattedJSON({
113 additionalAttributes: {
114 scheduledUpdate: true,
119 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
121 const tags = video.Tags ? video.Tags.map(t => t.name) : []
123 const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists)
125 const detailsJson = {
126 support: video.support,
127 descriptionPath: video.getDescriptionAPIPath(),
128 channel: video.VideoChannel.toFormattedJSON(),
129 account: video.VideoChannel.Account.toFormattedJSON(),
131 commentsEnabled: video.commentsEnabled,
132 downloadEnabled: video.downloadEnabled,
133 waitTranscoding: video.waitTranscoding,
136 label: VideoModel.getStateLabel(video.state)
139 trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs),
145 // Format and sort video files
146 detailsJson.files = videoFilesModelToFormattedJSON(video, baseUrlHttp, baseUrlWs, video.VideoFiles)
148 return Object.assign(formattedJson, detailsJson)
151 function streamingPlaylistsModelToFormattedJSON (video: MVideo, playlists: MStreamingPlaylistRedundanciesOpt[]): VideoStreamingPlaylist[] {
152 if (isArray(playlists) === false) return []
154 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
158 const playlistWithVideo = Object.assign(playlist, { Video: video })
160 const redundancies = isArray(playlist.RedundancyVideos)
161 ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl }))
164 const files = videoFilesModelToFormattedJSON(playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles)
169 playlistUrl: playlist.playlistUrl,
170 segmentsSha256Url: playlist.segmentsSha256Url,
177 function videoFilesModelToFormattedJSON (
178 model: MVideo | MStreamingPlaylistVideo,
181 videoFiles: MVideoFileRedundanciesOpt[]
187 id: videoFile.resolution,
188 label: videoFile.resolution + 'p'
190 magnetUri: generateMagnetUri(model, videoFile, baseUrlHttp, baseUrlWs),
191 size: videoFile.size,
193 torrentUrl: model.getTorrentUrl(videoFile, baseUrlHttp),
194 torrentDownloadUrl: model.getTorrentDownloadUrl(videoFile, baseUrlHttp),
195 fileUrl: model.getVideoFileUrl(videoFile, baseUrlHttp),
196 fileDownloadUrl: model.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
200 if (a.resolution.id < b.resolution.id) return 1
201 if (a.resolution.id === b.resolution.id) return 0
206 function addVideoFilesInAPAcc (
207 acc: ActivityUrlObject[] | ActivityTagObject[],
208 model: MVideoAP | MStreamingPlaylistVideo,
213 for (const file of files) {
216 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any,
217 href: model.getVideoFileUrl(file, baseUrlHttp),
218 height: file.resolution,
225 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
226 href: model.getTorrentUrl(file, baseUrlHttp),
227 height: file.resolution
232 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
233 href: generateMagnetUri(model, file, baseUrlHttp, baseUrlWs),
234 height: file.resolution
239 function videoModelToActivityPubObject (video: MVideoAP): VideoTorrentObject {
240 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
241 if (!video.Tags) video.Tags = []
243 const tag = video.Tags.map(t => ({
244 type: 'Hashtag' as 'Hashtag',
249 if (video.language) {
251 identifier: video.language,
252 name: VideoModel.getLanguageLabel(video.language)
257 if (video.category) {
259 identifier: video.category + '',
260 name: VideoModel.getCategoryLabel(video.category)
267 identifier: video.licence + '',
268 name: VideoModel.getLicenceLabel(video.licence)
272 const url: ActivityUrlObject[] = [
273 // HTML url should be the first element in the array so Mastodon correctly displays the embed
276 mediaType: 'text/html',
277 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
281 addVideoFilesInAPAcc(url, video, baseUrlHttp, baseUrlWs, video.VideoFiles || [])
283 for (const playlist of (video.VideoStreamingPlaylists || [])) {
284 const tag = playlist.p2pMediaLoaderInfohashes
285 .map(i => ({ type: 'Infohash' as 'Infohash', name: i })) as ActivityTagObject[]
289 mediaType: 'application/json' as 'application/json',
290 href: playlist.segmentsSha256Url
293 const playlistWithVideo = Object.assign(playlist, { Video: video })
294 addVideoFilesInAPAcc(tag, playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles || [])
298 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
299 href: playlist.playlistUrl,
304 const subtitleLanguage = []
305 for (const caption of video.VideoCaptions) {
306 subtitleLanguage.push({
307 identifier: caption.language,
308 name: VideoCaptionModel.getLanguageLabel(caption.language),
309 url: caption.getFileUrl(video)
313 const icons = [ video.getMiniature(), video.getPreview() ]
316 type: 'Video' as 'Video',
319 duration: getActivityStreamDuration(video.duration),
326 sensitive: video.nsfw,
327 waitTranscoding: video.waitTranscoding,
329 commentsEnabled: video.commentsEnabled,
330 downloadEnabled: video.downloadEnabled,
331 published: video.publishedAt.toISOString(),
332 originallyPublishedAt: video.originallyPublishedAt ? video.originallyPublishedAt.toISOString() : null,
333 updated: video.updatedAt.toISOString(),
334 mediaType: 'text/markdown',
335 content: video.getTruncatedDescription(),
336 support: video.support,
338 icon: icons.map(i => ({
340 url: i.getFileUrl(video),
341 mediaType: 'image/jpeg',
346 likes: getVideoLikesActivityPubUrl(video),
347 dislikes: getVideoDislikesActivityPubUrl(video),
348 shares: getVideoSharesActivityPubUrl(video),
349 comments: getVideoCommentsActivityPubUrl(video),
353 id: video.VideoChannel.Account.Actor.url
357 id: video.VideoChannel.Actor.url
363 function getActivityStreamDuration (duration: number) {
364 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
365 return 'PT' + duration + 'S'
369 videoModelToFormattedJSON,
370 videoModelToFormattedDetailsJSON,
371 videoFilesModelToFormattedJSON,
372 videoModelToActivityPubObject,
373 getActivityStreamDuration