]>
Commit | Line | Data |
---|---|---|
d7a25329 | 1 | import { Video, VideoDetails } from '../../../shared/models/videos' |
098eb377 | 2 | import { VideoModel } from './video' |
d7a25329 | 3 | import { ActivityTagObject, ActivityUrlObject, VideoTorrentObject } from '../../../shared/models/activitypub/objects' |
e8bafea3 | 4 | import { MIMETYPES, WEBSERVER } from '../../initializers/constants' |
098eb377 C |
5 | import { VideoCaptionModel } from './video-caption' |
6 | import { | |
7 | getVideoCommentsActivityPubUrl, | |
8 | getVideoDislikesActivityPubUrl, | |
9 | getVideoLikesActivityPubUrl, | |
10 | getVideoSharesActivityPubUrl | |
8dc8a34e | 11 | } from '../../lib/activitypub/url' |
79bd2632 | 12 | import { isArray } from '../../helpers/custom-validators/misc' |
09209296 | 13 | import { VideoStreamingPlaylist } from '../../../shared/models/videos/video-streaming-playlist.model' |
d7a25329 C |
14 | import { |
15 | MStreamingPlaylistRedundanciesOpt, | |
16 | MStreamingPlaylistVideo, | |
17 | MVideo, | |
18 | MVideoAP, | |
19 | MVideoFile, | |
20 | MVideoFormattable, | |
21 | MVideoFormattableDetails | |
26d6bf65 C |
22 | } from '../../types/models' |
23 | import { MVideoFileRedundanciesOpt } from '../../types/models/video/video-file' | |
d7a25329 C |
24 | import { VideoFile } from '@shared/models/videos/video-file.model' |
25 | import { generateMagnetUri } from '@server/helpers/webtorrent' | |
8dc8a34e | 26 | import { extractVideo } from '@server/helpers/video' |
098eb377 C |
27 | |
28 | export type VideoFormattingJSONOptions = { | |
c39e86b8 | 29 | completeDescription?: boolean |
098eb377 | 30 | additionalAttributes: { |
a1587156 C |
31 | state?: boolean |
32 | waitTranscoding?: boolean | |
33 | scheduledUpdate?: boolean | |
098eb377 C |
34 | blacklistInfo?: boolean |
35 | } | |
36 | } | |
a1587156 | 37 | |
1ca9f7c3 | 38 | function videoModelToFormattedJSON (video: MVideoFormattable, options?: VideoFormattingJSONOptions): Video { |
6e46de09 C |
39 | const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined |
40 | ||
098eb377 C |
41 | const videoObject: Video = { |
42 | id: video.id, | |
43 | uuid: video.uuid, | |
44 | name: video.name, | |
45 | category: { | |
46 | id: video.category, | |
47 | label: VideoModel.getCategoryLabel(video.category) | |
48 | }, | |
49 | licence: { | |
50 | id: video.licence, | |
51 | label: VideoModel.getLicenceLabel(video.licence) | |
52 | }, | |
53 | language: { | |
54 | id: video.language, | |
55 | label: VideoModel.getLanguageLabel(video.language) | |
56 | }, | |
57 | privacy: { | |
58 | id: video.privacy, | |
59 | label: VideoModel.getPrivacyLabel(video.privacy) | |
60 | }, | |
61 | nsfw: video.nsfw, | |
07524e22 | 62 | description: options && options.completeDescription === true ? video.description : video.getTruncatedDescription(), |
098eb377 C |
63 | isLocal: video.isOwned(), |
64 | duration: video.duration, | |
65 | views: video.views, | |
66 | likes: video.likes, | |
67 | dislikes: video.dislikes, | |
3acc5084 | 68 | thumbnailPath: video.getMiniatureStaticPath(), |
098eb377 C |
69 | previewPath: video.getPreviewStaticPath(), |
70 | embedPath: video.getEmbedStaticPath(), | |
71 | createdAt: video.createdAt, | |
72 | updatedAt: video.updatedAt, | |
73 | publishedAt: video.publishedAt, | |
c8034165 | 74 | originallyPublishedAt: video.originallyPublishedAt, |
418d092a C |
75 | |
76 | account: video.VideoChannel.Account.toFormattedSummaryJSON(), | |
77 | channel: video.VideoChannel.toFormattedSummaryJSON(), | |
6e46de09 C |
78 | |
79 | userHistory: userHistory ? { | |
80 | currentTime: userHistory.currentTime | |
81 | } : undefined | |
098eb377 C |
82 | } |
83 | ||
84 | if (options) { | |
85 | if (options.additionalAttributes.state === true) { | |
86 | videoObject.state = { | |
87 | id: video.state, | |
88 | label: VideoModel.getStateLabel(video.state) | |
89 | } | |
90 | } | |
91 | ||
92 | if (options.additionalAttributes.waitTranscoding === true) { | |
93 | videoObject.waitTranscoding = video.waitTranscoding | |
94 | } | |
95 | ||
96 | if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) { | |
97 | videoObject.scheduledUpdate = { | |
98 | updateAt: video.ScheduleVideoUpdate.updateAt, | |
99 | privacy: video.ScheduleVideoUpdate.privacy || undefined | |
100 | } | |
101 | } | |
102 | ||
103 | if (options.additionalAttributes.blacklistInfo === true) { | |
104 | videoObject.blacklisted = !!video.VideoBlacklist | |
105 | videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null | |
106 | } | |
107 | } | |
108 | ||
109 | return videoObject | |
110 | } | |
111 | ||
1ca9f7c3 | 112 | function videoModelToFormattedDetailsJSON (video: MVideoFormattableDetails): VideoDetails { |
098eb377 C |
113 | const formattedJson = video.toFormattedJSON({ |
114 | additionalAttributes: { | |
115 | scheduledUpdate: true, | |
116 | blacklistInfo: true | |
117 | } | |
118 | }) | |
119 | ||
09209296 C |
120 | const { baseUrlHttp, baseUrlWs } = video.getBaseUrls() |
121 | ||
96f29c0f | 122 | const tags = video.Tags ? video.Tags.map(t => t.name) : [] |
09209296 | 123 | |
d7a25329 | 124 | const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists) |
09209296 | 125 | |
098eb377 C |
126 | const detailsJson = { |
127 | support: video.support, | |
96f29c0f | 128 | descriptionPath: video.getDescriptionAPIPath(), |
098eb377 C |
129 | channel: video.VideoChannel.toFormattedJSON(), |
130 | account: video.VideoChannel.Account.toFormattedJSON(), | |
96f29c0f | 131 | tags, |
098eb377 | 132 | commentsEnabled: video.commentsEnabled, |
7f2cfe3a | 133 | downloadEnabled: video.downloadEnabled, |
098eb377 C |
134 | waitTranscoding: video.waitTranscoding, |
135 | state: { | |
136 | id: video.state, | |
137 | label: VideoModel.getStateLabel(video.state) | |
138 | }, | |
09209296 C |
139 | |
140 | trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs), | |
141 | ||
142 | files: [], | |
143 | streamingPlaylists | |
098eb377 C |
144 | } |
145 | ||
146 | // Format and sort video files | |
d7a25329 | 147 | detailsJson.files = videoFilesModelToFormattedJSON(video, baseUrlHttp, baseUrlWs, video.VideoFiles) |
098eb377 C |
148 | |
149 | return Object.assign(formattedJson, detailsJson) | |
150 | } | |
151 | ||
d7a25329 | 152 | function streamingPlaylistsModelToFormattedJSON (video: MVideo, playlists: MStreamingPlaylistRedundanciesOpt[]): VideoStreamingPlaylist[] { |
09209296 C |
153 | if (isArray(playlists) === false) return [] |
154 | ||
d7a25329 C |
155 | const { baseUrlHttp, baseUrlWs } = video.getBaseUrls() |
156 | ||
09209296 C |
157 | return playlists |
158 | .map(playlist => { | |
d7a25329 C |
159 | const playlistWithVideo = Object.assign(playlist, { Video: video }) |
160 | ||
09209296 C |
161 | const redundancies = isArray(playlist.RedundancyVideos) |
162 | ? playlist.RedundancyVideos.map(r => ({ baseUrl: r.fileUrl })) | |
163 | : [] | |
164 | ||
d7a25329 C |
165 | const files = videoFilesModelToFormattedJSON(playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles) |
166 | ||
09209296 C |
167 | return { |
168 | id: playlist.id, | |
169 | type: playlist.type, | |
170 | playlistUrl: playlist.playlistUrl, | |
171 | segmentsSha256Url: playlist.segmentsSha256Url, | |
d7a25329 C |
172 | redundancies, |
173 | files | |
174 | } | |
09209296 C |
175 | }) |
176 | } | |
177 | ||
5072b909 C |
178 | function sortByResolutionDesc (fileA: MVideoFile, fileB: MVideoFile) { |
179 | if (fileA.resolution < fileB.resolution) return 1 | |
180 | if (fileA.resolution === fileB.resolution) return 0 | |
181 | return -1 | |
182 | } | |
183 | ||
d7a25329 C |
184 | function videoFilesModelToFormattedJSON ( |
185 | model: MVideo | MStreamingPlaylistVideo, | |
186 | baseUrlHttp: string, | |
187 | baseUrlWs: string, | |
188 | videoFiles: MVideoFileRedundanciesOpt[] | |
189 | ): VideoFile[] { | |
7b81edc8 C |
190 | const video = extractVideo(model) |
191 | ||
5072b909 C |
192 | return [ ...videoFiles ] |
193 | .sort(sortByResolutionDesc) | |
098eb377 | 194 | .map(videoFile => { |
098eb377 C |
195 | return { |
196 | resolution: { | |
197 | id: videoFile.resolution, | |
a1587156 | 198 | label: videoFile.resolution + 'p' |
098eb377 | 199 | }, |
d7a25329 | 200 | magnetUri: generateMagnetUri(model, videoFile, baseUrlHttp, baseUrlWs), |
098eb377 C |
201 | size: videoFile.size, |
202 | fps: videoFile.fps, | |
d7a25329 C |
203 | torrentUrl: model.getTorrentUrl(videoFile, baseUrlHttp), |
204 | torrentDownloadUrl: model.getTorrentDownloadUrl(videoFile, baseUrlHttp), | |
205 | fileUrl: model.getVideoFileUrl(videoFile, baseUrlHttp), | |
8319d6ae | 206 | fileDownloadUrl: model.getVideoFileDownloadUrl(videoFile, baseUrlHttp), |
7b81edc8 | 207 | metadataUrl: video.getVideoFileMetadataUrl(videoFile, baseUrlHttp) |
098eb377 C |
208 | } as VideoFile |
209 | }) | |
098eb377 C |
210 | } |
211 | ||
d7a25329 C |
212 | function addVideoFilesInAPAcc ( |
213 | acc: ActivityUrlObject[] | ActivityTagObject[], | |
214 | model: MVideoAP | MStreamingPlaylistVideo, | |
215 | baseUrlHttp: string, | |
216 | baseUrlWs: string, | |
217 | files: MVideoFile[] | |
218 | ) { | |
5072b909 C |
219 | const sortedFiles = [ ...files ].sort(sortByResolutionDesc) |
220 | ||
221 | for (const file of sortedFiles) { | |
d7a25329 C |
222 | acc.push({ |
223 | type: 'Link', | |
a1587156 | 224 | mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] as any, |
d7a25329 C |
225 | href: model.getVideoFileUrl(file, baseUrlHttp), |
226 | height: file.resolution, | |
227 | size: file.size, | |
228 | fps: file.fps | |
229 | }) | |
230 | ||
8319d6ae RK |
231 | acc.push({ |
232 | type: 'Link', | |
233 | rel: [ 'metadata', MIMETYPES.VIDEO.EXT_MIMETYPE[file.extname] ], | |
234 | mediaType: 'application/json' as 'application/json', | |
235 | href: extractVideo(model).getVideoFileMetadataUrl(file, baseUrlHttp), | |
236 | height: file.resolution, | |
237 | fps: file.fps | |
238 | }) | |
239 | ||
d7a25329 C |
240 | acc.push({ |
241 | type: 'Link', | |
242 | mediaType: 'application/x-bittorrent' as 'application/x-bittorrent', | |
243 | href: model.getTorrentUrl(file, baseUrlHttp), | |
244 | height: file.resolution | |
245 | }) | |
246 | ||
247 | acc.push({ | |
248 | type: 'Link', | |
249 | mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet', | |
250 | href: generateMagnetUri(model, file, baseUrlHttp, baseUrlWs), | |
251 | height: file.resolution | |
252 | }) | |
253 | } | |
254 | } | |
255 | ||
453e83ea | 256 | function videoModelToActivityPubObject (video: MVideoAP): VideoTorrentObject { |
098eb377 C |
257 | const { baseUrlHttp, baseUrlWs } = video.getBaseUrls() |
258 | if (!video.Tags) video.Tags = [] | |
259 | ||
260 | const tag = video.Tags.map(t => ({ | |
261 | type: 'Hashtag' as 'Hashtag', | |
262 | name: t.name | |
263 | })) | |
264 | ||
265 | let language | |
266 | if (video.language) { | |
267 | language = { | |
268 | identifier: video.language, | |
269 | name: VideoModel.getLanguageLabel(video.language) | |
270 | } | |
271 | } | |
272 | ||
273 | let category | |
274 | if (video.category) { | |
275 | category = { | |
276 | identifier: video.category + '', | |
277 | name: VideoModel.getCategoryLabel(video.category) | |
278 | } | |
279 | } | |
280 | ||
281 | let licence | |
282 | if (video.licence) { | |
283 | licence = { | |
284 | identifier: video.licence + '', | |
285 | name: VideoModel.getLicenceLabel(video.licence) | |
286 | } | |
287 | } | |
288 | ||
22f18a4a C |
289 | const url: ActivityUrlObject[] = [ |
290 | // HTML url should be the first element in the array so Mastodon correctly displays the embed | |
291 | { | |
292 | type: 'Link', | |
293 | mediaType: 'text/html', | |
294 | href: WEBSERVER.URL + '/videos/watch/' + video.uuid | |
295 | } | |
296 | ] | |
297 | ||
d7a25329 | 298 | addVideoFilesInAPAcc(url, video, baseUrlHttp, baseUrlWs, video.VideoFiles || []) |
098eb377 | 299 | |
09209296 | 300 | for (const playlist of (video.VideoStreamingPlaylists || [])) { |
a1587156 C |
301 | const tag = playlist.p2pMediaLoaderInfohashes |
302 | .map(i => ({ type: 'Infohash' as 'Infohash', name: i })) as ActivityTagObject[] | |
09209296 C |
303 | tag.push({ |
304 | type: 'Link', | |
305 | name: 'sha256', | |
09209296 C |
306 | mediaType: 'application/json' as 'application/json', |
307 | href: playlist.segmentsSha256Url | |
308 | }) | |
309 | ||
d7a25329 C |
310 | const playlistWithVideo = Object.assign(playlist, { Video: video }) |
311 | addVideoFilesInAPAcc(tag, playlistWithVideo, baseUrlHttp, baseUrlWs, playlist.VideoFiles || []) | |
312 | ||
09209296 C |
313 | url.push({ |
314 | type: 'Link', | |
09209296 C |
315 | mediaType: 'application/x-mpegURL' as 'application/x-mpegURL', |
316 | href: playlist.playlistUrl, | |
317 | tag | |
318 | }) | |
319 | } | |
320 | ||
098eb377 C |
321 | const subtitleLanguage = [] |
322 | for (const caption of video.VideoCaptions) { | |
323 | subtitleLanguage.push({ | |
324 | identifier: caption.language, | |
ca6d3622 C |
325 | name: VideoCaptionModel.getLanguageLabel(caption.language), |
326 | url: caption.getFileUrl(video) | |
098eb377 C |
327 | }) |
328 | } | |
329 | ||
4282dafc | 330 | const icons = [ video.getMiniature(), video.getPreview() ] |
3acc5084 | 331 | |
098eb377 C |
332 | return { |
333 | type: 'Video' as 'Video', | |
334 | id: video.url, | |
335 | name: video.name, | |
336 | duration: getActivityStreamDuration(video.duration), | |
337 | uuid: video.uuid, | |
338 | tag, | |
339 | category, | |
340 | licence, | |
341 | language, | |
342 | views: video.views, | |
343 | sensitive: video.nsfw, | |
344 | waitTranscoding: video.waitTranscoding, | |
345 | state: video.state, | |
346 | commentsEnabled: video.commentsEnabled, | |
7f2cfe3a | 347 | downloadEnabled: video.downloadEnabled, |
098eb377 | 348 | published: video.publishedAt.toISOString(), |
7519127b | 349 | originallyPublishedAt: video.originallyPublishedAt ? video.originallyPublishedAt.toISOString() : null, |
098eb377 C |
350 | updated: video.updatedAt.toISOString(), |
351 | mediaType: 'text/markdown', | |
5cb9f0f4 | 352 | content: video.description, |
098eb377 C |
353 | support: video.support, |
354 | subtitleLanguage, | |
4282dafc | 355 | icon: icons.map(i => ({ |
098eb377 | 356 | type: 'Image', |
4282dafc | 357 | url: i.getFileUrl(video), |
098eb377 | 358 | mediaType: 'image/jpeg', |
4282dafc C |
359 | width: i.width, |
360 | height: i.height | |
361 | })), | |
098eb377 C |
362 | url, |
363 | likes: getVideoLikesActivityPubUrl(video), | |
364 | dislikes: getVideoDislikesActivityPubUrl(video), | |
365 | shares: getVideoSharesActivityPubUrl(video), | |
366 | comments: getVideoCommentsActivityPubUrl(video), | |
367 | attributedTo: [ | |
368 | { | |
369 | type: 'Person', | |
370 | id: video.VideoChannel.Account.Actor.url | |
371 | }, | |
372 | { | |
373 | type: 'Group', | |
374 | id: video.VideoChannel.Actor.url | |
375 | } | |
376 | ] | |
377 | } | |
378 | } | |
379 | ||
380 | function getActivityStreamDuration (duration: number) { | |
381 | // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration | |
382 | return 'PT' + duration + 'S' | |
383 | } | |
384 | ||
385 | export { | |
386 | videoModelToFormattedJSON, | |
387 | videoModelToFormattedDetailsJSON, | |
388 | videoFilesModelToFormattedJSON, | |
389 | videoModelToActivityPubObject, | |
390 | getActivityStreamDuration | |
391 | } |