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 addVideoFilesInAPAcc(url, video, baseUrlHttp, baseUrlWs, video.VideoFiles || [])
275 for (const playlist of (video.VideoStreamingPlaylists || [])) {
276 const tag = playlist.p2pMediaLoaderInfohashes
277 .map(i => ({ type: 'Infohash' as 'Infohash', name: i })) as ActivityTagObject[]
281 mediaType: 'application/json' as 'application/json',
282 href: playlist.segmentsSha256Url
285 const playlistWithVideo = Object.assign(playlist, { Video: video })
286 addVideoFilesInAPAcc(tag, playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles || [])
290 mediaType: 'application/x-mpegURL' as 'application/x-mpegURL',
291 href: playlist.playlistUrl,
299 mediaType: 'text/html',
300 href: WEBSERVER.URL + '/videos/watch/' + video.uuid
303 const subtitleLanguage = []
304 for (const caption of video.VideoCaptions) {
305 subtitleLanguage.push({
306 identifier: caption.language,
307 name: VideoCaptionModel.getLanguageLabel(caption.language),
308 url: caption.getFileUrl(video)
312 const icons = [ video.getMiniature(), video.getPreview() ]
315 type: 'Video' as 'Video',
318 duration: getActivityStreamDuration(video.duration),
325 sensitive: video.nsfw,
326 waitTranscoding: video.waitTranscoding,
328 commentsEnabled: video.commentsEnabled,
329 downloadEnabled: video.downloadEnabled,
330 published: video.publishedAt.toISOString(),
331 originallyPublishedAt: video.originallyPublishedAt ? video.originallyPublishedAt.toISOString() : null,
332 updated: video.updatedAt.toISOString(),
333 mediaType: 'text/markdown',
334 content: video.getTruncatedDescription(),
335 support: video.support,
337 icon: icons.map(i => ({
339 url: i.getFileUrl(video),
340 mediaType: 'image/jpeg',
345 likes: getVideoLikesActivityPubUrl(video),
346 dislikes: getVideoDislikesActivityPubUrl(video),
347 shares: getVideoSharesActivityPubUrl(video),
348 comments: getVideoCommentsActivityPubUrl(video),
352 id: video.VideoChannel.Account.Actor.url
356 id: video.VideoChannel.Actor.url
362 function getActivityStreamDuration (duration: number) {
363 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
364 return 'PT' + duration + 'S'
368 videoModelToFormattedJSON,
369 videoModelToFormattedDetailsJSON,
370 videoFilesModelToFormattedJSON,
371 videoModelToActivityPubObject,
372 getActivityStreamDuration