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