1 import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos'
2 import { VideoModel } from './video'
3 import { VideoFileModel } from './video-file'
4 import { ActivityUrlObject, VideoTorrentObject } from '../../../shared/models/activitypub/objects'
5 import { CONFIG, THUMBNAILS_SIZE, VIDEO_EXT_MIMETYPE } from '../../initializers'
6 import { VideoCaptionModel } from './video-caption'
8 getVideoCommentsActivityPubUrl,
9 getVideoDislikesActivityPubUrl,
10 getVideoLikesActivityPubUrl,
11 getVideoSharesActivityPubUrl
12 } from '../../lib/activitypub'
14 export type VideoFormattingJSONOptions = {
15 additionalAttributes: {
17 waitTranscoding?: boolean,
18 scheduledUpdate?: boolean,
19 blacklistInfo?: boolean
22 function videoModelToFormattedJSON (video: VideoModel, options?: VideoFormattingJSONOptions): Video {
23 const formattedAccount = video.VideoChannel.Account.toFormattedJSON()
24 const formattedVideoChannel = video.VideoChannel.toFormattedJSON()
26 const videoObject: Video = {
32 label: VideoModel.getCategoryLabel(video.category)
36 label: VideoModel.getLicenceLabel(video.licence)
40 label: VideoModel.getLanguageLabel(video.language)
44 label: VideoModel.getPrivacyLabel(video.privacy)
47 description: video.getTruncatedDescription(),
48 isLocal: video.isOwned(),
49 duration: video.duration,
52 dislikes: video.dislikes,
53 thumbnailPath: video.getThumbnailStaticPath(),
54 previewPath: video.getPreviewStaticPath(),
55 embedPath: video.getEmbedStaticPath(),
56 createdAt: video.createdAt,
57 updatedAt: video.updatedAt,
58 publishedAt: video.publishedAt,
60 id: formattedAccount.id,
61 uuid: formattedAccount.uuid,
62 name: formattedAccount.name,
63 displayName: formattedAccount.displayName,
64 url: formattedAccount.url,
65 host: formattedAccount.host,
66 avatar: formattedAccount.avatar
69 id: formattedVideoChannel.id,
70 uuid: formattedVideoChannel.uuid,
71 name: formattedVideoChannel.name,
72 displayName: formattedVideoChannel.displayName,
73 url: formattedVideoChannel.url,
74 host: formattedVideoChannel.host,
75 avatar: formattedVideoChannel.avatar
80 if (options.additionalAttributes.state === true) {
83 label: VideoModel.getStateLabel(video.state)
87 if (options.additionalAttributes.waitTranscoding === true) {
88 videoObject.waitTranscoding = video.waitTranscoding
91 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
92 videoObject.scheduledUpdate = {
93 updateAt: video.ScheduleVideoUpdate.updateAt,
94 privacy: video.ScheduleVideoUpdate.privacy || undefined
98 if (options.additionalAttributes.blacklistInfo === true) {
99 videoObject.blacklisted = !!video.VideoBlacklist
100 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
107 function videoModelToFormattedDetailsJSON (video: VideoModel): VideoDetails {
108 const formattedJson = video.toFormattedJSON({
109 additionalAttributes: {
110 scheduledUpdate: true,
115 const tags = video.Tags ? video.Tags.map(t => t.name) : []
116 const detailsJson = {
117 support: video.support,
118 descriptionPath: video.getDescriptionAPIPath(),
119 channel: video.VideoChannel.toFormattedJSON(),
120 account: video.VideoChannel.Account.toFormattedJSON(),
122 commentsEnabled: video.commentsEnabled,
123 waitTranscoding: video.waitTranscoding,
126 label: VideoModel.getStateLabel(video.state)
131 // Format and sort video files
132 detailsJson.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
134 return Object.assign(formattedJson, detailsJson)
137 function videoFilesModelToFormattedJSON (video: VideoModel, videoFiles: VideoFileModel[]): VideoFile[] {
138 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
142 let resolutionLabel = videoFile.resolution + 'p'
146 id: videoFile.resolution,
147 label: resolutionLabel
149 magnetUri: video.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
150 size: videoFile.size,
152 torrentUrl: video.getTorrentUrl(videoFile, baseUrlHttp),
153 torrentDownloadUrl: video.getTorrentDownloadUrl(videoFile, baseUrlHttp),
154 fileUrl: video.getVideoFileUrl(videoFile, baseUrlHttp),
155 fileDownloadUrl: video.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
159 if (a.resolution.id < b.resolution.id) return 1
160 if (a.resolution.id === b.resolution.id) return 0
165 function videoModelToActivityPubObject (video: VideoModel): VideoTorrentObject {
166 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
167 if (!video.Tags) video.Tags = []
169 const tag = video.Tags.map(t => ({
170 type: 'Hashtag' as 'Hashtag',
175 if (video.language) {
177 identifier: video.language,
178 name: VideoModel.getLanguageLabel(video.language)
183 if (video.category) {
185 identifier: video.category + '',
186 name: VideoModel.getCategoryLabel(video.category)
193 identifier: video.licence + '',
194 name: VideoModel.getLicenceLabel(video.licence)
198 const url: ActivityUrlObject[] = []
199 for (const file of video.VideoFiles) {
202 mimeType: VIDEO_EXT_MIMETYPE[ file.extname ] as any,
203 href: video.getVideoFileUrl(file, baseUrlHttp),
204 height: file.resolution,
211 mimeType: 'application/x-bittorrent' as 'application/x-bittorrent',
212 href: video.getTorrentUrl(file, baseUrlHttp),
213 height: file.resolution
218 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
219 href: video.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
220 height: file.resolution
227 mimeType: 'text/html',
228 href: CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
231 const subtitleLanguage = []
232 for (const caption of video.VideoCaptions) {
233 subtitleLanguage.push({
234 identifier: caption.language,
235 name: VideoCaptionModel.getLanguageLabel(caption.language)
240 type: 'Video' as 'Video',
243 duration: getActivityStreamDuration(video.duration),
250 sensitive: video.nsfw,
251 waitTranscoding: video.waitTranscoding,
253 commentsEnabled: video.commentsEnabled,
254 published: video.publishedAt.toISOString(),
255 updated: video.updatedAt.toISOString(),
256 mediaType: 'text/markdown',
257 content: video.getTruncatedDescription(),
258 support: video.support,
262 url: video.getThumbnailUrl(baseUrlHttp),
263 mediaType: 'image/jpeg',
264 width: THUMBNAILS_SIZE.width,
265 height: THUMBNAILS_SIZE.height
268 likes: getVideoLikesActivityPubUrl(video),
269 dislikes: getVideoDislikesActivityPubUrl(video),
270 shares: getVideoSharesActivityPubUrl(video),
271 comments: getVideoCommentsActivityPubUrl(video),
275 id: video.VideoChannel.Account.Actor.url
279 id: video.VideoChannel.Actor.url
285 function getActivityStreamDuration (duration: number) {
286 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
287 return 'PT' + duration + 'S'
291 videoModelToFormattedJSON,
292 videoModelToFormattedDetailsJSON,
293 videoFilesModelToFormattedJSON,
294 videoModelToActivityPubObject,
295 getActivityStreamDuration