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, MIMETYPES, THUMBNAILS_SIZE } from '../../initializers'
6 import { VideoCaptionModel } from './video-caption'
8 getVideoCommentsActivityPubUrl,
9 getVideoDislikesActivityPubUrl,
10 getVideoLikesActivityPubUrl,
11 getVideoSharesActivityPubUrl
12 } from '../../lib/activitypub'
13 import { isArray } from '../../helpers/custom-validators/misc'
15 export type VideoFormattingJSONOptions = {
16 completeDescription?: boolean
17 additionalAttributes: {
19 waitTranscoding?: boolean,
20 scheduledUpdate?: boolean,
21 blacklistInfo?: boolean
24 function videoModelToFormattedJSON (video: VideoModel, options?: VideoFormattingJSONOptions): Video {
25 const formattedAccount = video.VideoChannel.Account.toFormattedJSON()
26 const formattedVideoChannel = video.VideoChannel.toFormattedJSON()
28 const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined
30 const videoObject: Video = {
36 label: VideoModel.getCategoryLabel(video.category)
40 label: VideoModel.getLicenceLabel(video.licence)
44 label: VideoModel.getLanguageLabel(video.language)
48 label: VideoModel.getPrivacyLabel(video.privacy)
51 description: options && options.completeDescription === true ? video.description : video.getTruncatedDescription(),
52 isLocal: video.isOwned(),
53 duration: video.duration,
56 dislikes: video.dislikes,
57 thumbnailPath: video.getThumbnailStaticPath(),
58 previewPath: video.getPreviewStaticPath(),
59 embedPath: video.getEmbedStaticPath(),
60 createdAt: video.createdAt,
61 updatedAt: video.updatedAt,
62 publishedAt: video.publishedAt,
63 originallyPublishedAt: video.originallyPublishedAt,
65 id: formattedAccount.id,
66 uuid: formattedAccount.uuid,
67 name: formattedAccount.name,
68 displayName: formattedAccount.displayName,
69 url: formattedAccount.url,
70 host: formattedAccount.host,
71 avatar: formattedAccount.avatar
74 id: formattedVideoChannel.id,
75 uuid: formattedVideoChannel.uuid,
76 name: formattedVideoChannel.name,
77 displayName: formattedVideoChannel.displayName,
78 url: formattedVideoChannel.url,
79 host: formattedVideoChannel.host,
80 avatar: formattedVideoChannel.avatar
83 userHistory: userHistory ? {
84 currentTime: userHistory.currentTime
89 if (options.additionalAttributes.state === true) {
92 label: VideoModel.getStateLabel(video.state)
96 if (options.additionalAttributes.waitTranscoding === true) {
97 videoObject.waitTranscoding = video.waitTranscoding
100 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
101 videoObject.scheduledUpdate = {
102 updateAt: video.ScheduleVideoUpdate.updateAt,
103 privacy: video.ScheduleVideoUpdate.privacy || undefined
107 if (options.additionalAttributes.blacklistInfo === true) {
108 videoObject.blacklisted = !!video.VideoBlacklist
109 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
116 function videoModelToFormattedDetailsJSON (video: VideoModel): VideoDetails {
117 const formattedJson = video.toFormattedJSON({
118 additionalAttributes: {
119 scheduledUpdate: true,
124 const tags = video.Tags ? video.Tags.map(t => t.name) : []
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 waitTranscoding: video.waitTranscoding,
135 label: VideoModel.getStateLabel(video.state)
140 // Format and sort video files
141 detailsJson.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
143 return Object.assign(formattedJson, detailsJson)
146 function videoFilesModelToFormattedJSON (video: VideoModel, videoFiles: VideoFileModel[]): VideoFile[] {
147 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
151 let resolutionLabel = videoFile.resolution + 'p'
155 id: videoFile.resolution,
156 label: resolutionLabel
158 magnetUri: video.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
159 size: videoFile.size,
161 torrentUrl: video.getTorrentUrl(videoFile, baseUrlHttp),
162 torrentDownloadUrl: video.getTorrentDownloadUrl(videoFile, baseUrlHttp),
163 fileUrl: video.getVideoFileUrl(videoFile, baseUrlHttp),
164 fileDownloadUrl: video.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
168 if (a.resolution.id < b.resolution.id) return 1
169 if (a.resolution.id === b.resolution.id) return 0
174 function videoModelToActivityPubObject (video: VideoModel): VideoTorrentObject {
175 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
176 if (!video.Tags) video.Tags = []
178 const tag = video.Tags.map(t => ({
179 type: 'Hashtag' as 'Hashtag',
184 if (video.language) {
186 identifier: video.language,
187 name: VideoModel.getLanguageLabel(video.language)
192 if (video.category) {
194 identifier: video.category + '',
195 name: VideoModel.getCategoryLabel(video.category)
202 identifier: video.licence + '',
203 name: VideoModel.getLicenceLabel(video.licence)
207 const url: ActivityUrlObject[] = []
208 for (const file of video.VideoFiles) {
211 mimeType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any,
212 mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any,
213 href: video.getVideoFileUrl(file, baseUrlHttp),
214 height: file.resolution,
221 mimeType: 'application/x-bittorrent' as 'application/x-bittorrent',
222 mediaType: 'application/x-bittorrent' as 'application/x-bittorrent',
223 href: video.getTorrentUrl(file, baseUrlHttp),
224 height: file.resolution
229 mimeType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
230 mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet',
231 href: video.generateMagnetUri(file, baseUrlHttp, baseUrlWs),
232 height: file.resolution
239 mimeType: 'text/html',
240 mediaType: 'text/html',
241 href: CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
244 const subtitleLanguage = []
245 for (const caption of video.VideoCaptions) {
246 subtitleLanguage.push({
247 identifier: caption.language,
248 name: VideoCaptionModel.getLanguageLabel(caption.language)
253 type: 'Video' as 'Video',
256 duration: getActivityStreamDuration(video.duration),
263 sensitive: video.nsfw,
264 waitTranscoding: video.waitTranscoding,
266 commentsEnabled: video.commentsEnabled,
267 published: video.publishedAt.toISOString(),
268 originallyPublishedAt: video.originallyPublishedAt ?
269 video.originallyPublishedAt.toISOString() :
271 updated: video.updatedAt.toISOString(),
272 mediaType: 'text/markdown',
273 content: video.getTruncatedDescription(),
274 support: video.support,
278 url: video.getThumbnailUrl(baseUrlHttp),
279 mediaType: 'image/jpeg',
280 width: THUMBNAILS_SIZE.width,
281 height: THUMBNAILS_SIZE.height
284 likes: getVideoLikesActivityPubUrl(video),
285 dislikes: getVideoDislikesActivityPubUrl(video),
286 shares: getVideoSharesActivityPubUrl(video),
287 comments: getVideoCommentsActivityPubUrl(video),
291 id: video.VideoChannel.Account.Actor.url
295 id: video.VideoChannel.Actor.url
301 function getActivityStreamDuration (duration: number) {
302 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
303 return 'PT' + duration + 'S'
307 videoModelToFormattedJSON,
308 videoModelToFormattedDetailsJSON,
309 videoFilesModelToFormattedJSON,
310 videoModelToActivityPubObject,
311 getActivityStreamDuration