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