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 completeDescription?: boolean
16 additionalAttributes: {
18 waitTranscoding?: boolean,
19 scheduledUpdate?: boolean,
20 blacklistInfo?: boolean
23 function videoModelToFormattedJSON (video: VideoModel, options?: VideoFormattingJSONOptions): Video {
24 const formattedAccount = video.VideoChannel.Account.toFormattedJSON()
25 const formattedVideoChannel = video.VideoChannel.toFormattedJSON()
27 const videoObject: Video = {
33 label: VideoModel.getCategoryLabel(video.category)
37 label: VideoModel.getLicenceLabel(video.licence)
41 label: VideoModel.getLanguageLabel(video.language)
45 label: VideoModel.getPrivacyLabel(video.privacy)
48 description: options && options.completeDescription === true ? video.description : video.getTruncatedDescription(),
49 isLocal: video.isOwned(),
50 duration: video.duration,
53 dislikes: video.dislikes,
54 thumbnailPath: video.getThumbnailStaticPath(),
55 previewPath: video.getPreviewStaticPath(),
56 embedPath: video.getEmbedStaticPath(),
57 createdAt: video.createdAt,
58 updatedAt: video.updatedAt,
59 publishedAt: video.publishedAt,
61 id: formattedAccount.id,
62 uuid: formattedAccount.uuid,
63 name: formattedAccount.name,
64 displayName: formattedAccount.displayName,
65 url: formattedAccount.url,
66 host: formattedAccount.host,
67 avatar: formattedAccount.avatar
70 id: formattedVideoChannel.id,
71 uuid: formattedVideoChannel.uuid,
72 name: formattedVideoChannel.name,
73 displayName: formattedVideoChannel.displayName,
74 url: formattedVideoChannel.url,
75 host: formattedVideoChannel.host,
76 avatar: formattedVideoChannel.avatar
81 if (options.additionalAttributes.state === true) {
84 label: VideoModel.getStateLabel(video.state)
88 if (options.additionalAttributes.waitTranscoding === true) {
89 videoObject.waitTranscoding = video.waitTranscoding
92 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
93 videoObject.scheduledUpdate = {
94 updateAt: video.ScheduleVideoUpdate.updateAt,
95 privacy: video.ScheduleVideoUpdate.privacy || undefined
99 if (options.additionalAttributes.blacklistInfo === true) {
100 videoObject.blacklisted = !!video.VideoBlacklist
101 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
108 function videoModelToFormattedDetailsJSON (video: VideoModel): VideoDetails {
109 const formattedJson = video.toFormattedJSON({
110 additionalAttributes: {
111 scheduledUpdate: true,
116 const tags = video.Tags ? video.Tags.map(t => t.name) : []
117 const detailsJson = {
118 support: video.support,
119 descriptionPath: video.getDescriptionAPIPath(),
120 channel: video.VideoChannel.toFormattedJSON(),
121 account: video.VideoChannel.Account.toFormattedJSON(),
123 commentsEnabled: video.commentsEnabled,
124 waitTranscoding: video.waitTranscoding,
127 label: VideoModel.getStateLabel(video.state)
132 // Format and sort video files
133 detailsJson.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
135 return Object.assign(formattedJson, detailsJson)
138 function videoFilesModelToFormattedJSON (video: VideoModel, videoFiles: VideoFileModel[]): VideoFile[] {
139 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
143 let resolutionLabel = videoFile.resolution + 'p'
147 id: videoFile.resolution,
148 label: resolutionLabel
150 magnetUri: video.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
151 size: videoFile.size,
153 torrentUrl: video.getTorrentUrl(videoFile, baseUrlHttp),
154 torrentDownloadUrl: video.getTorrentDownloadUrl(videoFile, baseUrlHttp),
155 fileUrl: video.getVideoFileUrl(videoFile, baseUrlHttp),
156 fileDownloadUrl: video.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
160 if (a.resolution.id < b.resolution.id) return 1
161 if (a.resolution.id === b.resolution.id) return 0
166 function videoModelToActivityPubObject (video: VideoModel): VideoTorrentObject {
167 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
168 if (!video.Tags) video.Tags = []
170 const tag = video.Tags.map(t => ({
171 type: 'Hashtag' as 'Hashtag',
176 if (video.language) {
178 identifier: video.language,
179 name: VideoModel.getLanguageLabel(video.language)
184 if (video.category) {
186 identifier: video.category + '',
187 name: VideoModel.getCategoryLabel(video.category)
194 identifier: video.licence + '',
195 name: VideoModel.getLicenceLabel(video.licence)
199 const url: ActivityUrlObject[] = []
200 for (const file of video.VideoFiles) {
203 mimeType: VIDEO_EXT_MIMETYPE[ file.extname ] as any,
204 href: video.getVideoFileUrl(file, baseUrlHttp),
205 height: file.resolution,
212 mimeType: 'application/x-bittorrent' as 'application/x-bittorrent',
213 href: video.getTorrentUrl(file, baseUrlHttp),
214 height: file.resolution
219 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
220 href: video.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
221 height: file.resolution
228 mimeType: 'text/html',
229 href: CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
232 const subtitleLanguage = []
233 for (const caption of video.VideoCaptions) {
234 subtitleLanguage.push({
235 identifier: caption.language,
236 name: VideoCaptionModel.getLanguageLabel(caption.language)
241 type: 'Video' as 'Video',
244 duration: getActivityStreamDuration(video.duration),
251 sensitive: video.nsfw,
252 waitTranscoding: video.waitTranscoding,
254 commentsEnabled: video.commentsEnabled,
255 published: video.publishedAt.toISOString(),
256 updated: video.updatedAt.toISOString(),
257 mediaType: 'text/markdown',
258 content: video.getTruncatedDescription(),
259 support: video.support,
263 url: video.getThumbnailUrl(baseUrlHttp),
264 mediaType: 'image/jpeg',
265 width: THUMBNAILS_SIZE.width,
266 height: THUMBNAILS_SIZE.height
269 likes: getVideoLikesActivityPubUrl(video),
270 dislikes: getVideoDislikesActivityPubUrl(video),
271 shares: getVideoSharesActivityPubUrl(video),
272 comments: getVideoCommentsActivityPubUrl(video),
276 id: video.VideoChannel.Account.Actor.url
280 id: video.VideoChannel.Actor.url
286 function getActivityStreamDuration (duration: number) {
287 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
288 return 'PT' + duration + 'S'
292 videoModelToFormattedJSON,
293 videoModelToFormattedDetailsJSON,
294 videoFilesModelToFormattedJSON,
295 videoModelToActivityPubObject,
296 getActivityStreamDuration