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 detailsJson = {
116 support: video.support,
117 descriptionPath: video.getDescriptionPath(),
118 channel: video.VideoChannel.toFormattedJSON(),
119 account: video.VideoChannel.Account.toFormattedJSON(),
120 tags: video.Tags.map(t => t.name),
121 commentsEnabled: video.commentsEnabled,
122 waitTranscoding: video.waitTranscoding,
125 label: VideoModel.getStateLabel(video.state)
130 // Format and sort video files
131 detailsJson.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
133 return Object.assign(formattedJson, detailsJson)
136 function videoFilesModelToFormattedJSON (video: VideoModel, videoFiles: VideoFileModel[]): VideoFile[] {
137 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
141 let resolutionLabel = videoFile.resolution + 'p'
145 id: videoFile.resolution,
146 label: resolutionLabel
148 magnetUri: video.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
149 size: videoFile.size,
151 torrentUrl: video.getTorrentUrl(videoFile, baseUrlHttp),
152 torrentDownloadUrl: video.getTorrentDownloadUrl(videoFile, baseUrlHttp),
153 fileUrl: video.getVideoFileUrl(videoFile, baseUrlHttp),
154 fileDownloadUrl: video.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
158 if (a.resolution.id < b.resolution.id) return 1
159 if (a.resolution.id === b.resolution.id) return 0
164 function videoModelToActivityPubObject (video: VideoModel): VideoTorrentObject {
165 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
166 if (!video.Tags) video.Tags = []
168 const tag = video.Tags.map(t => ({
169 type: 'Hashtag' as 'Hashtag',
174 if (video.language) {
176 identifier: video.language,
177 name: VideoModel.getLanguageLabel(video.language)
182 if (video.category) {
184 identifier: video.category + '',
185 name: VideoModel.getCategoryLabel(video.category)
192 identifier: video.licence + '',
193 name: VideoModel.getLicenceLabel(video.licence)
197 const url: ActivityUrlObject[] = []
198 for (const file of video.VideoFiles) {
201 mimeType: VIDEO_EXT_MIMETYPE[ file.extname ] as any,
202 href: video.getVideoFileUrl(file, baseUrlHttp),
203 height: file.resolution,
210 mimeType: 'application/x-bittorrent' as 'application/x-bittorrent',
211 href: video.getTorrentUrl(file, baseUrlHttp),
212 height: file.resolution
217 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
218 href: video.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
219 height: file.resolution
226 mimeType: 'text/html',
227 href: CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
230 const subtitleLanguage = []
231 for (const caption of video.VideoCaptions) {
232 subtitleLanguage.push({
233 identifier: caption.language,
234 name: VideoCaptionModel.getLanguageLabel(caption.language)
239 type: 'Video' as 'Video',
242 duration: getActivityStreamDuration(video.duration),
249 sensitive: video.nsfw,
250 waitTranscoding: video.waitTranscoding,
252 commentsEnabled: video.commentsEnabled,
253 published: video.publishedAt.toISOString(),
254 updated: video.updatedAt.toISOString(),
255 mediaType: 'text/markdown',
256 content: video.getTruncatedDescription(),
257 support: video.support,
261 url: video.getThumbnailUrl(baseUrlHttp),
262 mediaType: 'image/jpeg',
263 width: THUMBNAILS_SIZE.width,
264 height: THUMBNAILS_SIZE.height
267 likes: getVideoLikesActivityPubUrl(video),
268 dislikes: getVideoDislikesActivityPubUrl(video),
269 shares: getVideoSharesActivityPubUrl(video),
270 comments: getVideoCommentsActivityPubUrl(video),
274 id: video.VideoChannel.Account.Actor.url
278 id: video.VideoChannel.Actor.url
284 function getActivityStreamDuration (duration: number) {
285 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
286 return 'PT' + duration + 'S'
290 videoModelToFormattedJSON,
291 videoModelToFormattedDetailsJSON,
292 videoFilesModelToFormattedJSON,
293 videoModelToActivityPubObject,
294 getActivityStreamDuration