]>
Commit | Line | Data |
---|---|---|
1 | import { Video, VideoDetails, VideoFile } from '../../../shared/models/videos' | |
2 | import { VideoModel } from './video' | |
3 | import { VideoFileModel } from './video-file' | |
4 | import { | |
5 | ActivityPlaylistInfohashesObject, | |
6 | ActivityPlaylistSegmentHashesObject, | |
7 | ActivityUrlObject, | |
8 | VideoTorrentObject | |
9 | } from '../../../shared/models/activitypub/objects' | |
10 | import { MIMETYPES, WEBSERVER } from '../../initializers/constants' | |
11 | import { VideoCaptionModel } from './video-caption' | |
12 | import { | |
13 | getVideoCommentsActivityPubUrl, | |
14 | getVideoDislikesActivityPubUrl, | |
15 | getVideoLikesActivityPubUrl, | |
16 | getVideoSharesActivityPubUrl | |
17 | } from '../../lib/activitypub' | |
18 | import { isArray } from '../../helpers/custom-validators/misc' | |
19 | import { VideoStreamingPlaylist } from '../../../shared/models/videos/video-streaming-playlist.model' | |
20 | import { VideoStreamingPlaylistModel } from './video-streaming-playlist' | |
21 | ||
22 | export type VideoFormattingJSONOptions = { | |
23 | completeDescription?: boolean | |
24 | additionalAttributes: { | |
25 | state?: boolean, | |
26 | waitTranscoding?: boolean, | |
27 | scheduledUpdate?: boolean, | |
28 | blacklistInfo?: boolean | |
29 | } | |
30 | } | |
31 | function videoModelToFormattedJSON (video: VideoModel, options?: VideoFormattingJSONOptions): Video { | |
32 | const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined | |
33 | ||
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, | |
55 | description: options && options.completeDescription === true ? video.description : video.getTruncatedDescription(), | |
56 | isLocal: video.isOwned(), | |
57 | duration: video.duration, | |
58 | views: video.views, | |
59 | likes: video.likes, | |
60 | dislikes: video.dislikes, | |
61 | thumbnailPath: video.getMiniatureStaticPath(), | |
62 | previewPath: video.getPreviewStaticPath(), | |
63 | embedPath: video.getEmbedStaticPath(), | |
64 | createdAt: video.createdAt, | |
65 | updatedAt: video.updatedAt, | |
66 | publishedAt: video.publishedAt, | |
67 | originallyPublishedAt: video.originallyPublishedAt, | |
68 | ||
69 | account: video.VideoChannel.Account.toFormattedSummaryJSON(), | |
70 | channel: video.VideoChannel.toFormattedSummaryJSON(), | |
71 | ||
72 | userHistory: userHistory ? { | |
73 | currentTime: userHistory.currentTime | |
74 | } : undefined | |
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 | ||
113 | const { baseUrlHttp, baseUrlWs } = video.getBaseUrls() | |
114 | ||
115 | const tags = video.Tags ? video.Tags.map(t => t.name) : [] | |
116 | ||
117 | const streamingPlaylists = streamingPlaylistsModelToFormattedJSON(video, video.VideoStreamingPlaylists) | |
118 | ||
119 | const detailsJson = { | |
120 | support: video.support, | |
121 | descriptionPath: video.getDescriptionAPIPath(), | |
122 | channel: video.VideoChannel.toFormattedJSON(), | |
123 | account: video.VideoChannel.Account.toFormattedJSON(), | |
124 | tags, | |
125 | commentsEnabled: video.commentsEnabled, | |
126 | downloadEnabled: video.downloadEnabled, | |
127 | waitTranscoding: video.waitTranscoding, | |
128 | state: { | |
129 | id: video.state, | |
130 | label: VideoModel.getStateLabel(video.state) | |
131 | }, | |
132 | ||
133 | trackerUrls: video.getTrackerUrls(baseUrlHttp, baseUrlWs), | |
134 | ||
135 | files: [], | |
136 | streamingPlaylists | |
137 | } | |
138 | ||
139 | // Format and sort video files | |
140 | detailsJson.files = videoFilesModelToFormattedJSON(video, video.VideoFiles) | |
141 | ||
142 | return Object.assign(formattedJson, detailsJson) | |
143 | } | |
144 | ||
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 | ||
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', | |
229 | mimeType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any, | |
230 | mediaType: MIMETYPES.VIDEO.EXT_MIMETYPE[ file.extname ] as any, | |
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', | |
240 | mediaType: 'application/x-bittorrent' as 'application/x-bittorrent', | |
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', | |
248 | mediaType: 'application/x-bittorrent;x-scheme-handler/magnet' as 'application/x-bittorrent;x-scheme-handler/magnet', | |
249 | href: video.generateMagnetUri(file, baseUrlHttp, baseUrlWs), | |
250 | height: file.resolution | |
251 | }) | |
252 | } | |
253 | ||
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 | ||
276 | // Add video url too | |
277 | url.push({ | |
278 | type: 'Link', | |
279 | mimeType: 'text/html', | |
280 | mediaType: 'text/html', | |
281 | href: WEBSERVER.URL + '/videos/watch/' + video.uuid | |
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 | ||
292 | const miniature = video.getMiniature() | |
293 | ||
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, | |
309 | downloadEnabled: video.downloadEnabled, | |
310 | published: video.publishedAt.toISOString(), | |
311 | originallyPublishedAt: video.originallyPublishedAt ? video.originallyPublishedAt.toISOString() : null, | |
312 | updated: video.updatedAt.toISOString(), | |
313 | mediaType: 'text/markdown', | |
314 | content: video.getTruncatedDescription(), | |
315 | support: video.support, | |
316 | subtitleLanguage, | |
317 | icon: { | |
318 | type: 'Image', | |
319 | url: miniature.getFileUrl(), | |
320 | mediaType: 'image/jpeg', | |
321 | width: miniature.width, | |
322 | height: miniature.height | |
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 | } |