]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/models/video/video-format-utils.ts
7a9513cbe5d776c21f2b49983d3854832888cfbb
[github/Chocobozzz/PeerTube.git] / server / models / video / video-format-utils.ts
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'
7 import {
8 getVideoCommentsActivityPubUrl,
9 getVideoDislikesActivityPubUrl,
10 getVideoLikesActivityPubUrl,
11 getVideoSharesActivityPubUrl
12 } from '../../lib/activitypub'
13 import { isArray } from '../../helpers/custom-validators/misc'
14
15 export type VideoFormattingJSONOptions = {
16 completeDescription?: boolean
17 additionalAttributes: {
18 state?: boolean,
19 waitTranscoding?: boolean,
20 scheduledUpdate?: boolean,
21 blacklistInfo?: boolean
22 }
23 }
24 function videoModelToFormattedJSON (video: VideoModel, options?: VideoFormattingJSONOptions): Video {
25 const formattedAccount = video.VideoChannel.Account.toFormattedJSON()
26 const formattedVideoChannel = video.VideoChannel.toFormattedJSON()
27
28 const userHistory = isArray(video.UserVideoHistories) ? video.UserVideoHistories[0] : undefined
29
30 const videoObject: Video = {
31 id: video.id,
32 uuid: video.uuid,
33 name: video.name,
34 category: {
35 id: video.category,
36 label: VideoModel.getCategoryLabel(video.category)
37 },
38 licence: {
39 id: video.licence,
40 label: VideoModel.getLicenceLabel(video.licence)
41 },
42 language: {
43 id: video.language,
44 label: VideoModel.getLanguageLabel(video.language)
45 },
46 privacy: {
47 id: video.privacy,
48 label: VideoModel.getPrivacyLabel(video.privacy)
49 },
50 nsfw: video.nsfw,
51 description: options && options.completeDescription === true ? video.description : video.getTruncatedDescription(),
52 isLocal: video.isOwned(),
53 duration: video.duration,
54 views: video.views,
55 likes: video.likes,
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,
64 account: {
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
72 },
73 channel: {
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
81 },
82
83 userHistory: userHistory ? {
84 currentTime: userHistory.currentTime
85 } : undefined
86 }
87
88 if (options) {
89 if (options.additionalAttributes.state === true) {
90 videoObject.state = {
91 id: video.state,
92 label: VideoModel.getStateLabel(video.state)
93 }
94 }
95
96 if (options.additionalAttributes.waitTranscoding === true) {
97 videoObject.waitTranscoding = video.waitTranscoding
98 }
99
100 if (options.additionalAttributes.scheduledUpdate === true && video.ScheduleVideoUpdate) {
101 videoObject.scheduledUpdate = {
102 updateAt: video.ScheduleVideoUpdate.updateAt,
103 privacy: video.ScheduleVideoUpdate.privacy || undefined
104 }
105 }
106
107 if (options.additionalAttributes.blacklistInfo === true) {
108 videoObject.blacklisted = !!video.VideoBlacklist
109 videoObject.blacklistedReason = video.VideoBlacklist ? video.VideoBlacklist.reason : null
110 }
111 }
112
113 return videoObject
114 }
115
116 function videoModelToFormattedDetailsJSON (video: VideoModel): VideoDetails {
117 const formattedJson = video.toFormattedJSON({
118 additionalAttributes: {
119 scheduledUpdate: true,
120 blacklistInfo: true
121 }
122 })
123
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(),
130 tags,
131 commentsEnabled: video.commentsEnabled,
132 waitTranscoding: video.waitTranscoding,
133 state: {
134 id: video.state,
135 label: VideoModel.getStateLabel(video.state)
136 },
137 files: []
138 }
139
140 // Format and sort video files
141 detailsJson.files = videoFilesModelToFormattedJSON(video, video.VideoFiles)
142
143 return Object.assign(formattedJson, detailsJson)
144 }
145
146 function videoFilesModelToFormattedJSON (video: VideoModel, videoFiles: VideoFileModel[]): VideoFile[] {
147 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
148
149 return videoFiles
150 .map(videoFile => {
151 let resolutionLabel = videoFile.resolution + 'p'
152
153 return {
154 resolution: {
155 id: videoFile.resolution,
156 label: resolutionLabel
157 },
158 magnetUri: video.generateMagnetUri(videoFile, baseUrlHttp, baseUrlWs),
159 size: videoFile.size,
160 fps: videoFile.fps,
161 torrentUrl: video.getTorrentUrl(videoFile, baseUrlHttp),
162 torrentDownloadUrl: video.getTorrentDownloadUrl(videoFile, baseUrlHttp),
163 fileUrl: video.getVideoFileUrl(videoFile, baseUrlHttp),
164 fileDownloadUrl: video.getVideoFileDownloadUrl(videoFile, baseUrlHttp)
165 } as VideoFile
166 })
167 .sort((a, b) => {
168 if (a.resolution.id < b.resolution.id) return 1
169 if (a.resolution.id === b.resolution.id) return 0
170 return -1
171 })
172 }
173
174 function videoModelToActivityPubObject (video: VideoModel): VideoTorrentObject {
175 const { baseUrlHttp, baseUrlWs } = video.getBaseUrls()
176 if (!video.Tags) video.Tags = []
177
178 const tag = video.Tags.map(t => ({
179 type: 'Hashtag' as 'Hashtag',
180 name: t.name
181 }))
182
183 let language
184 if (video.language) {
185 language = {
186 identifier: video.language,
187 name: VideoModel.getLanguageLabel(video.language)
188 }
189 }
190
191 let category
192 if (video.category) {
193 category = {
194 identifier: video.category + '',
195 name: VideoModel.getCategoryLabel(video.category)
196 }
197 }
198
199 let licence
200 if (video.licence) {
201 licence = {
202 identifier: video.licence + '',
203 name: VideoModel.getLicenceLabel(video.licence)
204 }
205 }
206
207 const url: ActivityUrlObject[] = []
208 for (const file of video.VideoFiles) {
209 url.push({
210 type: 'Link',
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,
215 size: file.size,
216 fps: file.fps
217 })
218
219 url.push({
220 type: 'Link',
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
225 })
226
227 url.push({
228 type: 'Link',
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
233 })
234 }
235
236 // Add video url too
237 url.push({
238 type: 'Link',
239 mimeType: 'text/html',
240 mediaType: 'text/html',
241 href: CONFIG.WEBSERVER.URL + '/videos/watch/' + video.uuid
242 })
243
244 const subtitleLanguage = []
245 for (const caption of video.VideoCaptions) {
246 subtitleLanguage.push({
247 identifier: caption.language,
248 name: VideoCaptionModel.getLanguageLabel(caption.language)
249 })
250 }
251
252 return {
253 type: 'Video' as 'Video',
254 id: video.url,
255 name: video.name,
256 duration: getActivityStreamDuration(video.duration),
257 uuid: video.uuid,
258 tag,
259 category,
260 licence,
261 language,
262 views: video.views,
263 sensitive: video.nsfw,
264 waitTranscoding: video.waitTranscoding,
265 state: video.state,
266 commentsEnabled: video.commentsEnabled,
267 published: video.publishedAt.toISOString(),
268 originallyPublishedAt: video.originallyPublishedAt ?
269 video.originallyPublishedAt.toISOString() :
270 null,
271 updated: video.updatedAt.toISOString(),
272 mediaType: 'text/markdown',
273 content: video.getTruncatedDescription(),
274 support: video.support,
275 subtitleLanguage,
276 icon: {
277 type: 'Image',
278 url: video.getThumbnailUrl(baseUrlHttp),
279 mediaType: 'image/jpeg',
280 width: THUMBNAILS_SIZE.width,
281 height: THUMBNAILS_SIZE.height
282 },
283 url,
284 likes: getVideoLikesActivityPubUrl(video),
285 dislikes: getVideoDislikesActivityPubUrl(video),
286 shares: getVideoSharesActivityPubUrl(video),
287 comments: getVideoCommentsActivityPubUrl(video),
288 attributedTo: [
289 {
290 type: 'Person',
291 id: video.VideoChannel.Account.Actor.url
292 },
293 {
294 type: 'Group',
295 id: video.VideoChannel.Actor.url
296 }
297 ]
298 }
299 }
300
301 function getActivityStreamDuration (duration: number) {
302 // https://www.w3.org/TR/activitystreams-vocabulary/#dfn-duration
303 return 'PT' + duration + 'S'
304 }
305
306 export {
307 videoModelToFormattedJSON,
308 videoModelToFormattedDetailsJSON,
309 videoFilesModelToFormattedJSON,
310 videoModelToActivityPubObject,
311 getActivityStreamDuration
312 }