]>
Commit | Line | Data |
---|---|---|
7acee6f1 | 1 | import * as Bluebird from 'bluebird' |
30bc55c8 | 2 | import { maxBy, minBy } from 'lodash' |
2ccaeeb3 | 3 | import * as magnetUtil from 'magnet-uri' |
90a8bd30 | 4 | import { basename, join } from 'path' |
892211e8 | 5 | import * as request from 'request' |
30bc55c8 | 6 | import * as sequelize from 'sequelize' |
053aed43 | 7 | import { VideoLiveModel } from '@server/models/video/video-live' |
a8b1b404 | 8 | import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes' |
09209296 | 9 | import { |
d7a25329 C |
10 | ActivityHashTagObject, |
11 | ActivityMagnetUrlObject, | |
09209296 | 12 | ActivityPlaylistSegmentHashesObject, |
30bc55c8 C |
13 | ActivityPlaylistUrlObject, |
14 | ActivitypubHttpFetcherPayload, | |
ca6d3622 | 15 | ActivityTagObject, |
09209296 | 16 | ActivityUrlObject, |
053aed43 | 17 | ActivityVideoUrlObject |
09209296 | 18 | } from '../../../shared/index' |
a8b1b404 | 19 | import { ActivityIconObject, VideoObject } from '../../../shared/models/activitypub/objects' |
1297eb5d | 20 | import { VideoPrivacy } from '../../../shared/models/videos' |
30bc55c8 C |
21 | import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' |
22 | import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' | |
23 | import { buildRemoteVideoBaseUrl, checkUrlsSameHost, getAPId } from '../../helpers/activitypub' | |
ac940348 | 24 | import { isAPVideoFileMetadataObject, sanitizeAndCheckVideoTorrentObject } from '../../helpers/custom-validators/activitypub/videos' |
30bc55c8 | 25 | import { isArray } from '../../helpers/custom-validators/misc' |
2ccaeeb3 | 26 | import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos' |
d7a25329 | 27 | import { deleteNonExistingModels, resetSequelizeInstance, retryTransactionWrapper } from '../../helpers/database-utils' |
2ccaeeb3 | 28 | import { logger } from '../../helpers/logger' |
ca6d3622 | 29 | import { doRequest } from '../../helpers/requests' |
30bc55c8 | 30 | import { fetchVideoByUrl, getExtFromMimetype, VideoFetchByUrlType } from '../../helpers/video' |
e8bafea3 C |
31 | import { |
32 | ACTIVITY_PUB, | |
90a8bd30 | 33 | LAZY_STATIC_PATHS, |
e8bafea3 C |
34 | MIMETYPES, |
35 | P2P_MEDIA_LOADER_PEER_VERSION, | |
36 | PREVIEWS_SIZE, | |
37 | REMOTE_SCHEME, | |
7b81edc8 | 38 | THUMBNAILS_SIZE |
e8bafea3 | 39 | } from '../../initializers/constants' |
30bc55c8 C |
40 | import { sequelizeTypescript } from '../../initializers/database' |
41 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' | |
3fd3ab2d | 42 | import { VideoModel } from '../../models/video/video' |
40e87e9e | 43 | import { VideoCaptionModel } from '../../models/video/video-caption' |
2ba92871 | 44 | import { VideoCommentModel } from '../../models/video/video-comment' |
30bc55c8 C |
45 | import { VideoFileModel } from '../../models/video/video-file' |
46 | import { VideoShareModel } from '../../models/video/video-share' | |
47 | import { VideoStreamingPlaylistModel } from '../../models/video/video-streaming-playlist' | |
453e83ea | 48 | import { |
f92e7f76 | 49 | MAccountIdActor, |
453e83ea C |
50 | MChannelAccountLight, |
51 | MChannelDefault, | |
52 | MChannelId, | |
d7a25329 | 53 | MStreamingPlaylist, |
90a8bd30 C |
54 | MStreamingPlaylistFilesVideo, |
55 | MStreamingPlaylistVideo, | |
453e83ea | 56 | MVideo, |
453e83ea | 57 | MVideoAccountLight, |
f92e7f76 | 58 | MVideoAccountLightBlacklistAllFiles, |
453e83ea C |
59 | MVideoAP, |
60 | MVideoAPWithoutCaption, | |
6302d599 | 61 | MVideoCaption, |
453e83ea C |
62 | MVideoFile, |
63 | MVideoFullLight, | |
7b81edc8 C |
64 | MVideoId, |
65 | MVideoImmutable, | |
90a8bd30 C |
66 | MVideoThumbnail, |
67 | MVideoWithHost | |
26d6bf65 C |
68 | } from '../../types/models' |
69 | import { MThumbnail } from '../../types/models/video/thumbnail' | |
30bc55c8 C |
70 | import { FilteredModelAttributes } from '../../types/sequelize' |
71 | import { ActorFollowScoreCache } from '../files-cache' | |
72 | import { JobQueue } from '../job-queue' | |
73 | import { Notifier } from '../notifier' | |
a5cf76af | 74 | import { PeerTubeSocket } from '../peertube-socket' |
30bc55c8 | 75 | import { createPlaceholderThumbnail, createVideoMiniatureFromUrl } from '../thumbnail' |
1ef65f4c | 76 | import { setVideoTags } from '../video' |
30bc55c8 | 77 | import { autoBlacklistVideoIfNeeded } from '../video-blacklist' |
90a8bd30 | 78 | import { generateTorrentFileName } from '../video-paths' |
30bc55c8 C |
79 | import { getOrCreateActorAndServerAndModel } from './actor' |
80 | import { crawlCollectionPage } from './crawl' | |
81 | import { sendCreateVideo, sendUpdateVideo } from './send' | |
82 | import { addVideoShares, shareVideoByServerAndChannel } from './share' | |
83 | import { addVideoComments } from './video-comments' | |
84 | import { createRates } from './video-rates' | |
453e83ea C |
85 | |
86 | async function federateVideoIfNeeded (videoArg: MVideoAPWithoutCaption, isNewVideo: boolean, transaction?: sequelize.Transaction) { | |
87 | const video = videoArg as MVideoAP | |
2186386c | 88 | |
5b77537c C |
89 | if ( |
90 | // Check this is not a blacklisted video, or unfederated blacklisted video | |
91 | (video.isBlacklisted() === false || (isNewVideo === false && video.VideoBlacklist.unfederated === false)) && | |
92 | // Check the video is public/unlisted and published | |
68e70a74 | 93 | video.hasPrivacyForFederation() && video.hasStateForFederation() |
5b77537c | 94 | ) { |
40e87e9e C |
95 | // Fetch more attributes that we will need to serialize in AP object |
96 | if (isArray(video.VideoCaptions) === false) { | |
97 | video.VideoCaptions = await video.$get('VideoCaptions', { | |
6302d599 | 98 | attributes: [ 'filename', 'language' ], |
40e87e9e | 99 | transaction |
e6122097 | 100 | }) |
40e87e9e C |
101 | } |
102 | ||
2cebd797 | 103 | if (isNewVideo) { |
2186386c C |
104 | // Now we'll add the video's meta data to our followers |
105 | await sendCreateVideo(video, transaction) | |
106 | await shareVideoByServerAndChannel(video, transaction) | |
107 | } else { | |
108 | await sendUpdateVideo(video, transaction) | |
109 | } | |
110 | } | |
111 | } | |
892211e8 | 112 | |
de6310b2 | 113 | async function fetchRemoteVideo (videoUrl: string): Promise<{ response: request.RequestResponse, videoObject: VideoObject }> { |
4157cdb1 C |
114 | const options = { |
115 | uri: videoUrl, | |
116 | method: 'GET', | |
117 | json: true, | |
118 | activityPub: true | |
119 | } | |
892211e8 | 120 | |
4157cdb1 C |
121 | logger.info('Fetching remote video %s.', videoUrl) |
122 | ||
bdd428a6 | 123 | const { response, body } = await doRequest<any>(options) |
4157cdb1 | 124 | |
5c6d985f | 125 | if (sanitizeAndCheckVideoTorrentObject(body) === false || checkUrlsSameHost(body.id, videoUrl) !== true) { |
4157cdb1 C |
126 | logger.debug('Remote video JSON is not valid.', { body }) |
127 | return { response, videoObject: undefined } | |
128 | } | |
129 | ||
130 | return { response, videoObject: body } | |
892211e8 C |
131 | } |
132 | ||
453e83ea | 133 | async function fetchRemoteVideoDescription (video: MVideoAccountLight) { |
50d6de9c | 134 | const host = video.VideoChannel.Account.Actor.Server.host |
96f29c0f | 135 | const path = video.getDescriptionAPIPath() |
892211e8 C |
136 | const options = { |
137 | uri: REMOTE_SCHEME.HTTP + '://' + host + path, | |
138 | json: true | |
139 | } | |
140 | ||
bdd428a6 | 141 | const { body } = await doRequest<any>(options) |
892211e8 C |
142 | return body.description ? body.description : '' |
143 | } | |
144 | ||
de6310b2 | 145 | function getOrCreateVideoChannelFromVideoObject (videoObject: VideoObject) { |
0f320037 C |
146 | const channel = videoObject.attributedTo.find(a => a.type === 'Group') |
147 | if (!channel) throw new Error('Cannot find associated video channel to video ' + videoObject.url) | |
148 | ||
5c6d985f C |
149 | if (checkUrlsSameHost(channel.id, videoObject.id) !== true) { |
150 | throw new Error(`Video channel url ${channel.id} does not have the same host than video object id ${videoObject.id}`) | |
151 | } | |
152 | ||
e587e0ec | 153 | return getOrCreateActorAndServerAndModel(channel.id, 'all') |
0f320037 C |
154 | } |
155 | ||
f6eebcb3 | 156 | type SyncParam = { |
1297eb5d C |
157 | likes: boolean |
158 | dislikes: boolean | |
159 | shares: boolean | |
160 | comments: boolean | |
f6eebcb3 | 161 | thumbnail: boolean |
04b8c3fb | 162 | refreshVideo?: boolean |
f6eebcb3 | 163 | } |
de6310b2 | 164 | async function syncVideoExternalAttributes (video: MVideo, fetchedVideo: VideoObject, syncParam: SyncParam) { |
f6eebcb3 | 165 | logger.info('Adding likes/dislikes/shares/comments of video %s.', video.uuid) |
2ccaeeb3 | 166 | |
f6eebcb3 | 167 | const jobPayloads: ActivitypubHttpFetcherPayload[] = [] |
2ccaeeb3 | 168 | |
f6eebcb3 | 169 | if (syncParam.likes === true) { |
2ba92871 C |
170 | const handler = items => createRates(items, video, 'like') |
171 | const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'like' as 'like', crawlStartDate) | |
172 | ||
173 | await crawlCollectionPage<string>(fetchedVideo.likes, handler, cleaner) | |
ca6d3622 | 174 | .catch(err => logger.error('Cannot add likes of video %s.', video.uuid, { err, rootUrl: fetchedVideo.likes })) |
f6eebcb3 C |
175 | } else { |
176 | jobPayloads.push({ uri: fetchedVideo.likes, videoId: video.id, type: 'video-likes' as 'video-likes' }) | |
177 | } | |
7acee6f1 | 178 | |
f6eebcb3 | 179 | if (syncParam.dislikes === true) { |
2ba92871 C |
180 | const handler = items => createRates(items, video, 'dislike') |
181 | const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'dislike' as 'dislike', crawlStartDate) | |
182 | ||
183 | await crawlCollectionPage<string>(fetchedVideo.dislikes, handler, cleaner) | |
ca6d3622 | 184 | .catch(err => logger.error('Cannot add dislikes of video %s.', video.uuid, { err, rootUrl: fetchedVideo.dislikes })) |
f6eebcb3 C |
185 | } else { |
186 | jobPayloads.push({ uri: fetchedVideo.dislikes, videoId: video.id, type: 'video-dislikes' as 'video-dislikes' }) | |
187 | } | |
188 | ||
189 | if (syncParam.shares === true) { | |
2ba92871 C |
190 | const handler = items => addVideoShares(items, video) |
191 | const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate) | |
192 | ||
193 | await crawlCollectionPage<string>(fetchedVideo.shares, handler, cleaner) | |
ca6d3622 | 194 | .catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err, rootUrl: fetchedVideo.shares })) |
f6eebcb3 C |
195 | } else { |
196 | jobPayloads.push({ uri: fetchedVideo.shares, videoId: video.id, type: 'video-shares' as 'video-shares' }) | |
197 | } | |
7acee6f1 | 198 | |
f6eebcb3 | 199 | if (syncParam.comments === true) { |
6b9c966f | 200 | const handler = items => addVideoComments(items) |
2ba92871 C |
201 | const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate) |
202 | ||
203 | await crawlCollectionPage<string>(fetchedVideo.comments, handler, cleaner) | |
ca6d3622 | 204 | .catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err, rootUrl: fetchedVideo.comments })) |
f6eebcb3 | 205 | } else { |
2ba92871 | 206 | jobPayloads.push({ uri: fetchedVideo.comments, videoId: video.id, type: 'video-comments' as 'video-comments' }) |
f6eebcb3 | 207 | } |
7acee6f1 | 208 | |
a1587156 | 209 | await Bluebird.map(jobPayloads, payload => JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload })) |
2ccaeeb3 C |
210 | } |
211 | ||
943e5193 C |
212 | type GetVideoResult <T> = Promise<{ |
213 | video: T | |
214 | created: boolean | |
215 | autoBlacklisted?: boolean | |
216 | }> | |
217 | ||
218 | type GetVideoParamAll = { | |
a1587156 C |
219 | videoObject: { id: string } | string |
220 | syncParam?: SyncParam | |
221 | fetchType?: 'all' | |
453e83ea | 222 | allowRefresh?: boolean |
943e5193 C |
223 | } |
224 | ||
225 | type GetVideoParamImmutable = { | |
a1587156 C |
226 | videoObject: { id: string } | string |
227 | syncParam?: SyncParam | |
943e5193 C |
228 | fetchType: 'only-immutable-attributes' |
229 | allowRefresh: false | |
230 | } | |
231 | ||
232 | type GetVideoParamOther = { | |
a1587156 C |
233 | videoObject: { id: string } | string |
234 | syncParam?: SyncParam | |
943e5193 C |
235 | fetchType?: 'all' | 'only-video' |
236 | allowRefresh?: boolean | |
237 | } | |
238 | ||
239 | function getOrCreateVideoAndAccountAndChannel (options: GetVideoParamAll): GetVideoResult<MVideoAccountLightBlacklistAllFiles> | |
240 | function getOrCreateVideoAndAccountAndChannel (options: GetVideoParamImmutable): GetVideoResult<MVideoImmutable> | |
241 | function getOrCreateVideoAndAccountAndChannel ( | |
242 | options: GetVideoParamOther | |
243 | ): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail> | |
244 | async function getOrCreateVideoAndAccountAndChannel ( | |
245 | options: GetVideoParamAll | GetVideoParamImmutable | GetVideoParamOther | |
246 | ): GetVideoResult<MVideoAccountLightBlacklistAllFiles | MVideoThumbnail | MVideoImmutable> { | |
4157cdb1 C |
247 | // Default params |
248 | const syncParam = options.syncParam || { likes: true, dislikes: true, shares: true, comments: true, thumbnail: true, refreshVideo: false } | |
249 | const fetchType = options.fetchType || 'all' | |
74577825 | 250 | const allowRefresh = options.allowRefresh !== false |
1297eb5d | 251 | |
4157cdb1 | 252 | // Get video url |
848f499d | 253 | const videoUrl = getAPId(options.videoObject) |
4157cdb1 | 254 | let videoFromDatabase = await fetchVideoByUrl(videoUrl, fetchType) |
943e5193 | 255 | |
4157cdb1 | 256 | if (videoFromDatabase) { |
943e5193 C |
257 | // If allowRefresh is true, we could not call this function using 'only-immutable-attributes' fetch type |
258 | if (allowRefresh === true && (videoFromDatabase as MVideoThumbnail).isOutdated()) { | |
74577825 | 259 | const refreshOptions = { |
943e5193 | 260 | video: videoFromDatabase as MVideoThumbnail, |
74577825 C |
261 | fetchedType: fetchType, |
262 | syncParam | |
263 | } | |
264 | ||
a1587156 C |
265 | if (syncParam.refreshVideo === true) { |
266 | videoFromDatabase = await refreshVideoIfNeeded(refreshOptions) | |
267 | } else { | |
268 | await JobQueue.Instance.createJobWithPromise({ | |
269 | type: 'activitypub-refresher', | |
270 | payload: { type: 'video', url: videoFromDatabase.url } | |
271 | }) | |
272 | } | |
d4defe07 | 273 | } |
1297eb5d | 274 | |
cef534ed | 275 | return { video: videoFromDatabase, created: false } |
1297eb5d C |
276 | } |
277 | ||
4157cdb1 C |
278 | const { videoObject: fetchedVideo } = await fetchRemoteVideo(videoUrl) |
279 | if (!fetchedVideo) throw new Error('Cannot fetch remote video with url: ' + videoUrl) | |
7acee6f1 | 280 | |
453e83ea C |
281 | const actor = await getOrCreateVideoChannelFromVideoObject(fetchedVideo) |
282 | const videoChannel = actor.VideoChannel | |
7acee6f1 | 283 | |
5fb2e288 C |
284 | try { |
285 | const { autoBlacklisted, videoCreated } = await retryTransactionWrapper(createVideo, fetchedVideo, videoChannel, syncParam.thumbnail) | |
286 | ||
287 | await syncVideoExternalAttributes(videoCreated, fetchedVideo, syncParam) | |
7acee6f1 | 288 | |
5fb2e288 C |
289 | return { video: videoCreated, created: true, autoBlacklisted } |
290 | } catch (err) { | |
291 | // Maybe a concurrent getOrCreateVideoAndAccountAndChannel call created this video | |
292 | if (err.name === 'SequelizeUniqueConstraintError') { | |
293 | const fallbackVideo = await fetchVideoByUrl(videoUrl, fetchType) | |
294 | if (fallbackVideo) return { video: fallbackVideo, created: false } | |
295 | } | |
296 | ||
297 | throw err | |
298 | } | |
7acee6f1 C |
299 | } |
300 | ||
d4defe07 | 301 | async function updateVideoFromAP (options: { |
a1587156 | 302 | video: MVideoAccountLightBlacklistAllFiles |
de6310b2 | 303 | videoObject: VideoObject |
a1587156 C |
304 | account: MAccountIdActor |
305 | channel: MChannelDefault | |
1297eb5d | 306 | overrideTo?: string[] |
d4defe07 | 307 | }) { |
b4055e1c C |
308 | const { video, videoObject, account, channel, overrideTo } = options |
309 | ||
68e70a74 | 310 | logger.debug('Updating remote video "%s".', options.videoObject.uuid, { videoObject: options.videoObject, account, channel }) |
e8d246d5 | 311 | |
1297eb5d | 312 | let videoFieldsSave: any |
b4055e1c C |
313 | const wasPrivateVideo = video.privacy === VideoPrivacy.PRIVATE |
314 | const wasUnlistedVideo = video.privacy === VideoPrivacy.UNLISTED | |
1297eb5d C |
315 | |
316 | try { | |
453e83ea | 317 | let thumbnailModel: MThumbnail |
e8bafea3 C |
318 | |
319 | try { | |
a35a2279 C |
320 | thumbnailModel = await createVideoMiniatureFromUrl({ |
321 | downloadUrl: getThumbnailFromIcons(videoObject).url, | |
322 | video, | |
323 | type: ThumbnailType.MINIATURE | |
324 | }) | |
e8bafea3 | 325 | } catch (err) { |
b4055e1c | 326 | logger.warn('Cannot generate thumbnail of %s.', videoObject.id, { err }) |
e8bafea3 C |
327 | } |
328 | ||
453e83ea | 329 | const videoUpdated = await sequelizeTypescript.transaction(async t => { |
e8d246d5 | 330 | const sequelizeOptions = { transaction: t } |
2ccaeeb3 | 331 | |
b4055e1c | 332 | videoFieldsSave = video.toJSON() |
2ccaeeb3 | 333 | |
1297eb5d | 334 | // Check actor has the right to update the video |
b4055e1c C |
335 | const videoChannel = video.VideoChannel |
336 | if (videoChannel.Account.id !== account.id) { | |
337 | throw new Error('Account ' + account.Actor.url + ' does not own video channel ' + videoChannel.Actor.url) | |
f6eebcb3 C |
338 | } |
339 | ||
a1587156 | 340 | const to = overrideTo || videoObject.to |
b4055e1c C |
341 | const videoData = await videoActivityObjectToDBAttributes(channel, videoObject, to) |
342 | video.name = videoData.name | |
343 | video.uuid = videoData.uuid | |
344 | video.url = videoData.url | |
345 | video.category = videoData.category | |
346 | video.licence = videoData.licence | |
347 | video.language = videoData.language | |
348 | video.description = videoData.description | |
349 | video.support = videoData.support | |
350 | video.nsfw = videoData.nsfw | |
351 | video.commentsEnabled = videoData.commentsEnabled | |
352 | video.downloadEnabled = videoData.downloadEnabled | |
353 | video.waitTranscoding = videoData.waitTranscoding | |
354 | video.state = videoData.state | |
355 | video.duration = videoData.duration | |
356 | video.createdAt = videoData.createdAt | |
357 | video.publishedAt = videoData.publishedAt | |
358 | video.originallyPublishedAt = videoData.originallyPublishedAt | |
359 | video.privacy = videoData.privacy | |
360 | video.channelId = videoData.channelId | |
361 | video.views = videoData.views | |
a5cf76af | 362 | video.isLive = videoData.isLive |
b4055e1c | 363 | |
b49f22d8 C |
364 | // Ensures we update the updated video attribute |
365 | video.changed('updatedAt', true) | |
366 | ||
453e83ea | 367 | const videoUpdated = await video.save(sequelizeOptions) as MVideoFullLight |
b4055e1c | 368 | |
453e83ea | 369 | if (thumbnailModel) await videoUpdated.addAndSaveThumbnail(thumbnailModel, t) |
e8bafea3 | 370 | |
6872996d | 371 | if (videoUpdated.getPreview()) { |
a8b1b404 | 372 | const previewUrl = getPreviewUrl(getPreviewFromIcons(videoObject), video) |
a35a2279 C |
373 | const previewModel = createPlaceholderThumbnail({ |
374 | fileUrl: previewUrl, | |
375 | video, | |
376 | type: ThumbnailType.PREVIEW, | |
377 | size: PREVIEWS_SIZE | |
378 | }) | |
6872996d C |
379 | await videoUpdated.addAndSaveThumbnail(previewModel, t) |
380 | } | |
e8bafea3 | 381 | |
e5565833 | 382 | { |
d7a25329 | 383 | const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoUpdated, videoObject.url) |
e5565833 | 384 | const newVideoFiles = videoFileAttributes.map(a => new VideoFileModel(a)) |
0032ebe9 | 385 | |
e5565833 | 386 | // Remove video files that do not exist anymore |
d7a25329 | 387 | const destroyTasks = deleteNonExistingModels(videoUpdated.VideoFiles, newVideoFiles, t) |
e5565833 | 388 | await Promise.all(destroyTasks) |
2ccaeeb3 | 389 | |
e5565833 | 390 | // Update or add other one |
d7a25329 | 391 | const upsertTasks = newVideoFiles.map(f => VideoFileModel.customUpsert(f, 'video', t)) |
453e83ea | 392 | videoUpdated.VideoFiles = await Promise.all(upsertTasks) |
e5565833 | 393 | } |
2ccaeeb3 | 394 | |
09209296 | 395 | { |
453e83ea | 396 | const streamingPlaylistAttributes = streamingPlaylistActivityUrlToDBAttributes(videoUpdated, videoObject, videoUpdated.VideoFiles) |
09209296 C |
397 | const newStreamingPlaylists = streamingPlaylistAttributes.map(a => new VideoStreamingPlaylistModel(a)) |
398 | ||
d7a25329 C |
399 | // Remove video playlists that do not exist anymore |
400 | const destroyTasks = deleteNonExistingModels(videoUpdated.VideoStreamingPlaylists, newStreamingPlaylists, t) | |
09209296 C |
401 | await Promise.all(destroyTasks) |
402 | ||
d7a25329 C |
403 | let oldStreamingPlaylistFiles: MVideoFile[] = [] |
404 | for (const videoStreamingPlaylist of videoUpdated.VideoStreamingPlaylists) { | |
405 | oldStreamingPlaylistFiles = oldStreamingPlaylistFiles.concat(videoStreamingPlaylist.VideoFiles) | |
406 | } | |
407 | ||
408 | videoUpdated.VideoStreamingPlaylists = [] | |
409 | ||
410 | for (const playlistAttributes of streamingPlaylistAttributes) { | |
411 | const streamingPlaylistModel = await VideoStreamingPlaylistModel.upsert(playlistAttributes, { returning: true, transaction: t }) | |
90a8bd30 C |
412 | .then(([ streamingPlaylist ]) => streamingPlaylist as MStreamingPlaylistFilesVideo) |
413 | streamingPlaylistModel.Video = videoUpdated | |
09209296 | 414 | |
d7a25329 C |
415 | const newVideoFiles: MVideoFile[] = videoFileActivityUrlToDBAttributes(streamingPlaylistModel, playlistAttributes.tagAPObject) |
416 | .map(a => new VideoFileModel(a)) | |
417 | const destroyTasks = deleteNonExistingModels(oldStreamingPlaylistFiles, newVideoFiles, t) | |
418 | await Promise.all(destroyTasks) | |
419 | ||
420 | // Update or add other one | |
421 | const upsertTasks = newVideoFiles.map(f => VideoFileModel.customUpsert(f, 'streaming-playlist', t)) | |
422 | streamingPlaylistModel.VideoFiles = await Promise.all(upsertTasks) | |
423 | ||
424 | videoUpdated.VideoStreamingPlaylists.push(streamingPlaylistModel) | |
425 | } | |
09209296 C |
426 | } |
427 | ||
e5565833 C |
428 | { |
429 | // Update Tags | |
d7a25329 C |
430 | const tags = videoObject.tag |
431 | .filter(isAPHashTagObject) | |
432 | .map(tag => tag.name) | |
1ef65f4c | 433 | await setVideoTags({ video: videoUpdated, tags, transaction: t, defaultValue: videoUpdated.Tags }) |
e5565833 | 434 | } |
2ccaeeb3 | 435 | |
e5565833 C |
436 | { |
437 | // Update captions | |
453e83ea | 438 | await VideoCaptionModel.deleteAllCaptionsOfRemoteVideo(videoUpdated.id, t) |
e5565833 | 439 | |
b4055e1c | 440 | const videoCaptionsPromises = videoObject.subtitleLanguage.map(c => { |
6302d599 C |
441 | const caption = new VideoCaptionModel({ |
442 | videoId: videoUpdated.id, | |
443 | filename: VideoCaptionModel.generateCaptionName(c.identifier), | |
444 | language: c.identifier, | |
445 | fileUrl: c.url | |
446 | }) as MVideoCaption | |
447 | ||
448 | return VideoCaptionModel.insertOrReplaceLanguage(caption, t) | |
e5565833 | 449 | }) |
453e83ea | 450 | await Promise.all(videoCaptionsPromises) |
e5565833 | 451 | } |
453e83ea | 452 | |
af4ae64f C |
453 | { |
454 | // Create or update existing live | |
455 | if (video.isLive) { | |
456 | const [ videoLive ] = await VideoLiveModel.upsert({ | |
457 | saveReplay: videoObject.liveSaveReplay, | |
bb4ba6d9 | 458 | permanentLive: videoObject.permanentLive, |
af4ae64f C |
459 | videoId: video.id |
460 | }, { transaction: t, returning: true }) | |
461 | ||
462 | videoUpdated.VideoLive = videoLive | |
463 | } else { // Delete existing live if it exists | |
464 | await VideoLiveModel.destroy({ | |
465 | where: { | |
466 | videoId: video.id | |
467 | }, | |
468 | transaction: t | |
469 | }) | |
470 | ||
471 | videoUpdated.VideoLive = null | |
472 | } | |
473 | } | |
474 | ||
453e83ea | 475 | return videoUpdated |
1297eb5d C |
476 | }) |
477 | ||
5b77537c | 478 | await autoBlacklistVideoIfNeeded({ |
453e83ea | 479 | video: videoUpdated, |
6691c522 C |
480 | user: undefined, |
481 | isRemote: true, | |
482 | isNew: false, | |
483 | transaction: undefined | |
484 | }) | |
b4055e1c | 485 | |
a800dbf3 C |
486 | // Notify our users? |
487 | if (wasPrivateVideo || wasUnlistedVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoUpdated) | |
488 | ||
489 | if (videoUpdated.isLive) { | |
490 | PeerTubeSocket.Instance.sendVideoLiveNewState(videoUpdated) | |
491 | PeerTubeSocket.Instance.sendVideoViewsUpdate(videoUpdated) | |
492 | } | |
e8d246d5 | 493 | |
b4055e1c | 494 | logger.info('Remote video with uuid %s updated', videoObject.uuid) |
453e83ea C |
495 | |
496 | return videoUpdated | |
1297eb5d | 497 | } catch (err) { |
b4055e1c C |
498 | if (video !== undefined && videoFieldsSave !== undefined) { |
499 | resetSequelizeInstance(video, videoFieldsSave) | |
1297eb5d C |
500 | } |
501 | ||
502 | // This is just a debug because we will retry the insert | |
503 | logger.debug('Cannot update the remote video.', { err }) | |
504 | throw err | |
505 | } | |
892211e8 | 506 | } |
2186386c | 507 | |
04b8c3fb | 508 | async function refreshVideoIfNeeded (options: { |
a1587156 C |
509 | video: MVideoThumbnail |
510 | fetchedType: VideoFetchByUrlType | |
04b8c3fb | 511 | syncParam: SyncParam |
453e83ea | 512 | }): Promise<MVideoThumbnail> { |
04b8c3fb C |
513 | if (!options.video.isOutdated()) return options.video |
514 | ||
515 | // We need more attributes if the argument video was fetched with not enough joints | |
b4055e1c | 516 | const video = options.fetchedType === 'all' |
0283eaac | 517 | ? options.video as MVideoAccountLightBlacklistAllFiles |
b4055e1c | 518 | : await VideoModel.loadByUrlAndPopulateAccount(options.video.url) |
04b8c3fb C |
519 | |
520 | try { | |
521 | const { response, videoObject } = await fetchRemoteVideo(video.url) | |
f2eb23cd | 522 | if (response.statusCode === HttpStatusCode.NOT_FOUND_404) { |
04b8c3fb C |
523 | logger.info('Cannot refresh remote video %s: video does not exist anymore. Deleting it.', video.url) |
524 | ||
525 | // Video does not exist anymore | |
526 | await video.destroy() | |
527 | return undefined | |
528 | } | |
529 | ||
530 | if (videoObject === undefined) { | |
531 | logger.warn('Cannot refresh remote video %s: invalid body.', video.url) | |
532 | ||
533 | await video.setAsRefreshed() | |
534 | return video | |
535 | } | |
536 | ||
537 | const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject) | |
04b8c3fb C |
538 | |
539 | const updateOptions = { | |
540 | video, | |
541 | videoObject, | |
453e83ea | 542 | account: channelActor.VideoChannel.Account, |
04b8c3fb C |
543 | channel: channelActor.VideoChannel |
544 | } | |
545 | await retryTransactionWrapper(updateVideoFromAP, updateOptions) | |
546 | await syncVideoExternalAttributes(video, videoObject, options.syncParam) | |
547 | ||
6b9c966f C |
548 | ActorFollowScoreCache.Instance.addGoodServerId(video.VideoChannel.Actor.serverId) |
549 | ||
04b8c3fb C |
550 | return video |
551 | } catch (err) { | |
552 | logger.warn('Cannot refresh video %s.', options.video.url, { err }) | |
553 | ||
6b9c966f C |
554 | ActorFollowScoreCache.Instance.addBadServerId(video.VideoChannel.Actor.serverId) |
555 | ||
04b8c3fb C |
556 | // Don't refresh in loop |
557 | await video.setAsRefreshed() | |
558 | return video | |
559 | } | |
892211e8 | 560 | } |
2186386c C |
561 | |
562 | export { | |
1297eb5d | 563 | updateVideoFromAP, |
04b8c3fb | 564 | refreshVideoIfNeeded, |
2186386c C |
565 | federateVideoIfNeeded, |
566 | fetchRemoteVideo, | |
1297eb5d | 567 | getOrCreateVideoAndAccountAndChannel, |
2186386c | 568 | fetchRemoteVideoDescription, |
4157cdb1 | 569 | getOrCreateVideoChannelFromVideoObject |
2186386c | 570 | } |
c48e82b5 C |
571 | |
572 | // --------------------------------------------------------------------------- | |
573 | ||
d7a25329 | 574 | function isAPVideoUrlObject (url: any): url is ActivityVideoUrlObject { |
d7a25329 | 575 | const urlMediaType = url.mediaType |
30bc55c8 C |
576 | |
577 | return MIMETYPES.VIDEO.MIMETYPE_EXT[urlMediaType] && urlMediaType.startsWith('video/') | |
c48e82b5 | 578 | } |
4157cdb1 | 579 | |
09209296 | 580 | function isAPStreamingPlaylistUrlObject (url: ActivityUrlObject): url is ActivityPlaylistUrlObject { |
d7a25329 | 581 | return url && url.mediaType === 'application/x-mpegURL' |
09209296 C |
582 | } |
583 | ||
584 | function isAPPlaylistSegmentHashesUrlObject (tag: any): tag is ActivityPlaylistSegmentHashesObject { | |
d7a25329 C |
585 | return tag && tag.name === 'sha256' && tag.type === 'Link' && tag.mediaType === 'application/json' |
586 | } | |
587 | ||
588 | function isAPMagnetUrlObject (url: any): url is ActivityMagnetUrlObject { | |
589 | return url && url.mediaType === 'application/x-bittorrent;x-scheme-handler/magnet' | |
590 | } | |
09209296 | 591 | |
d7a25329 C |
592 | function isAPHashTagObject (url: any): url is ActivityHashTagObject { |
593 | return url && url.type === 'Hashtag' | |
c48e82b5 | 594 | } |
4157cdb1 | 595 | |
de6310b2 | 596 | async function createVideo (videoObject: VideoObject, channel: MChannelAccountLight, waitThumbnail = false) { |
4157cdb1 C |
597 | logger.debug('Adding remote video %s.', videoObject.id) |
598 | ||
453e83ea C |
599 | const videoData = await videoActivityObjectToDBAttributes(channel, videoObject, videoObject.to) |
600 | const video = VideoModel.build(videoData) as MVideoThumbnail | |
e8bafea3 | 601 | |
a35a2279 C |
602 | const promiseThumbnail = createVideoMiniatureFromUrl({ |
603 | downloadUrl: getThumbnailFromIcons(videoObject).url, | |
604 | video, | |
605 | type: ThumbnailType.MINIATURE | |
606 | }).catch(err => { | |
607 | logger.error('Cannot create miniature from url.', { err }) | |
608 | return undefined | |
609 | }) | |
e8bafea3 | 610 | |
453e83ea | 611 | let thumbnailModel: MThumbnail |
e8bafea3 C |
612 | if (waitThumbnail === true) { |
613 | thumbnailModel = await promiseThumbnail | |
614 | } | |
615 | ||
6691c522 | 616 | const { autoBlacklisted, videoCreated } = await sequelizeTypescript.transaction(async t => { |
a35a2279 C |
617 | try { |
618 | const sequelizeOptions = { transaction: t } | |
4157cdb1 | 619 | |
a35a2279 C |
620 | const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight |
621 | videoCreated.VideoChannel = channel | |
e8bafea3 | 622 | |
a35a2279 | 623 | if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t) |
e8bafea3 | 624 | |
a35a2279 C |
625 | const previewUrl = getPreviewUrl(getPreviewFromIcons(videoObject), videoCreated) |
626 | const previewModel = createPlaceholderThumbnail({ | |
627 | fileUrl: previewUrl, | |
628 | video: videoCreated, | |
629 | type: ThumbnailType.PREVIEW, | |
630 | size: PREVIEWS_SIZE | |
631 | }) | |
ca6d3622 | 632 | |
a35a2279 | 633 | if (thumbnailModel) await videoCreated.addAndSaveThumbnail(previewModel, t) |
4157cdb1 | 634 | |
a35a2279 C |
635 | // Process files |
636 | const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoObject.url) | |
4157cdb1 | 637 | |
a35a2279 C |
638 | const videoFilePromises = videoFileAttributes.map(f => VideoFileModel.create(f, { transaction: t })) |
639 | const videoFiles = await Promise.all(videoFilePromises) | |
4157cdb1 | 640 | |
a35a2279 C |
641 | const streamingPlaylistsAttributes = streamingPlaylistActivityUrlToDBAttributes(videoCreated, videoObject, videoFiles) |
642 | videoCreated.VideoStreamingPlaylists = [] | |
d7a25329 | 643 | |
a35a2279 | 644 | for (const playlistAttributes of streamingPlaylistsAttributes) { |
90a8bd30 C |
645 | const playlist = await VideoStreamingPlaylistModel.create(playlistAttributes, { transaction: t }) as MStreamingPlaylistFilesVideo |
646 | playlist.Video = videoCreated | |
d7a25329 | 647 | |
90a8bd30 | 648 | const playlistFiles = videoFileActivityUrlToDBAttributes(playlist, playlistAttributes.tagAPObject) |
a35a2279 | 649 | const videoFilePromises = playlistFiles.map(f => VideoFileModel.create(f, { transaction: t })) |
90a8bd30 | 650 | playlist.VideoFiles = await Promise.all(videoFilePromises) |
d7a25329 | 651 | |
90a8bd30 | 652 | videoCreated.VideoStreamingPlaylists.push(playlist) |
a35a2279 | 653 | } |
09209296 | 654 | |
a35a2279 C |
655 | // Process tags |
656 | const tags = videoObject.tag | |
657 | .filter(isAPHashTagObject) | |
658 | .map(t => t.name) | |
659 | await setVideoTags({ video: videoCreated, tags, transaction: t }) | |
660 | ||
661 | // Process captions | |
662 | const videoCaptionsPromises = videoObject.subtitleLanguage.map(c => { | |
663 | const caption = new VideoCaptionModel({ | |
664 | videoId: videoCreated.id, | |
665 | filename: VideoCaptionModel.generateCaptionName(c.identifier), | |
666 | language: c.identifier, | |
667 | fileUrl: c.url | |
668 | }) as MVideoCaption | |
669 | ||
670 | return VideoCaptionModel.insertOrReplaceLanguage(caption, t) | |
671 | }) | |
672 | await Promise.all(videoCaptionsPromises) | |
6b9c966f | 673 | |
a35a2279 | 674 | videoCreated.VideoFiles = videoFiles |
4157cdb1 | 675 | |
a35a2279 C |
676 | if (videoCreated.isLive) { |
677 | const videoLive = new VideoLiveModel({ | |
678 | streamKey: null, | |
679 | saveReplay: videoObject.liveSaveReplay, | |
680 | permanentLive: videoObject.permanentLive, | |
681 | videoId: videoCreated.id | |
682 | }) | |
af4ae64f | 683 | |
a35a2279 C |
684 | videoCreated.VideoLive = await videoLive.save({ transaction: t }) |
685 | } | |
af4ae64f | 686 | |
a35a2279 C |
687 | const autoBlacklisted = await autoBlacklistVideoIfNeeded({ |
688 | video: videoCreated, | |
689 | user: undefined, | |
690 | isRemote: true, | |
691 | isNew: true, | |
692 | transaction: t | |
693 | }) | |
6691c522 | 694 | |
a35a2279 | 695 | logger.info('Remote video with uuid %s inserted.', videoObject.uuid) |
4157cdb1 | 696 | |
a35a2279 C |
697 | return { autoBlacklisted, videoCreated } |
698 | } catch (err) { | |
699 | // FIXME: Use rollback hook when https://github.com/sequelize/sequelize/pull/13038 is released | |
700 | // Remove thumbnail | |
701 | if (thumbnailModel) await thumbnailModel.removeThumbnail() | |
702 | ||
703 | throw err | |
704 | } | |
4157cdb1 C |
705 | }) |
706 | ||
e8bafea3 | 707 | if (waitThumbnail === false) { |
6872996d C |
708 | // Error is already caught above |
709 | // eslint-disable-next-line @typescript-eslint/no-floating-promises | |
e8bafea3 | 710 | promiseThumbnail.then(thumbnailModel => { |
6872996d C |
711 | if (!thumbnailModel) return |
712 | ||
e8bafea3 | 713 | thumbnailModel = videoCreated.id |
4157cdb1 | 714 | |
e8bafea3 | 715 | return thumbnailModel.save() |
6872996d | 716 | }) |
e8bafea3 | 717 | } |
4157cdb1 | 718 | |
6691c522 | 719 | return { autoBlacklisted, videoCreated } |
4157cdb1 C |
720 | } |
721 | ||
de6310b2 | 722 | function videoActivityObjectToDBAttributes (videoChannel: MChannelId, videoObject: VideoObject, to: string[] = []) { |
bdd428a6 C |
723 | const privacy = to.includes(ACTIVITY_PUB.PUBLIC) |
724 | ? VideoPrivacy.PUBLIC | |
725 | : VideoPrivacy.UNLISTED | |
4157cdb1 | 726 | |
bdd428a6 | 727 | const duration = videoObject.duration.replace(/[^\d]+/, '') |
58b6fdca | 728 | const language = videoObject.language?.identifier |
4157cdb1 | 729 | |
58b6fdca C |
730 | const category = videoObject.category |
731 | ? parseInt(videoObject.category.identifier, 10) | |
732 | : undefined | |
4157cdb1 | 733 | |
58b6fdca C |
734 | const licence = videoObject.licence |
735 | ? parseInt(videoObject.licence.identifier, 10) | |
736 | : undefined | |
4157cdb1 C |
737 | |
738 | const description = videoObject.content || null | |
739 | const support = videoObject.support || null | |
740 | ||
741 | return { | |
742 | name: videoObject.name, | |
743 | uuid: videoObject.uuid, | |
744 | url: videoObject.id, | |
745 | category, | |
746 | licence, | |
747 | language, | |
748 | description, | |
749 | support, | |
750 | nsfw: videoObject.sensitive, | |
751 | commentsEnabled: videoObject.commentsEnabled, | |
7f2cfe3a | 752 | downloadEnabled: videoObject.downloadEnabled, |
4157cdb1 | 753 | waitTranscoding: videoObject.waitTranscoding, |
de6310b2 | 754 | isLive: videoObject.isLiveBroadcast, |
4157cdb1 C |
755 | state: videoObject.state, |
756 | channelId: videoChannel.id, | |
757 | duration: parseInt(duration, 10), | |
758 | createdAt: new Date(videoObject.published), | |
759 | publishedAt: new Date(videoObject.published), | |
58b6fdca C |
760 | |
761 | originallyPublishedAt: videoObject.originallyPublishedAt | |
762 | ? new Date(videoObject.originallyPublishedAt) | |
763 | : null, | |
764 | ||
4157cdb1 C |
765 | updatedAt: new Date(videoObject.updated), |
766 | views: videoObject.views, | |
767 | likes: 0, | |
768 | dislikes: 0, | |
769 | remote: true, | |
770 | privacy | |
771 | } | |
772 | } | |
773 | ||
d7a25329 | 774 | function videoFileActivityUrlToDBAttributes ( |
90a8bd30 | 775 | videoOrPlaylist: MVideo | MStreamingPlaylistVideo, |
d7a25329 C |
776 | urls: (ActivityTagObject | ActivityUrlObject)[] |
777 | ) { | |
778 | const fileUrls = urls.filter(u => isAPVideoUrlObject(u)) as ActivityVideoUrlObject[] | |
4157cdb1 | 779 | |
d7a25329 | 780 | if (fileUrls.length === 0) return [] |
4157cdb1 | 781 | |
3acc5084 | 782 | const attributes: FilteredModelAttributes<VideoFileModel>[] = [] |
4157cdb1 C |
783 | for (const fileUrl of fileUrls) { |
784 | // Fetch associated magnet uri | |
d7a25329 C |
785 | const magnet = urls.filter(isAPMagnetUrlObject) |
786 | .find(u => u.height === fileUrl.height) | |
4157cdb1 C |
787 | |
788 | if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.href) | |
789 | ||
790 | const parsed = magnetUtil.decode(magnet.href) | |
791 | if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) { | |
792 | throw new Error('Cannot parse magnet URI ' + magnet.href) | |
793 | } | |
794 | ||
90a8bd30 C |
795 | const torrentUrl = Array.isArray(parsed.xs) |
796 | ? parsed.xs[0] | |
797 | : parsed.xs | |
798 | ||
8319d6ae RK |
799 | // Fetch associated metadata url, if any |
800 | const metadata = urls.filter(isAPVideoFileMetadataObject) | |
7b81edc8 C |
801 | .find(u => { |
802 | return u.height === fileUrl.height && | |
803 | u.fps === fileUrl.fps && | |
804 | u.rel.includes(fileUrl.mediaType) | |
805 | }) | |
8319d6ae | 806 | |
90a8bd30 C |
807 | const extname = getExtFromMimetype(MIMETYPES.VIDEO.MIMETYPE_EXT, fileUrl.mediaType) |
808 | const resolution = fileUrl.height | |
809 | const videoId = (videoOrPlaylist as MStreamingPlaylist).playlistUrl ? null : videoOrPlaylist.id | |
810 | const videoStreamingPlaylistId = (videoOrPlaylist as MStreamingPlaylist).playlistUrl ? videoOrPlaylist.id : null | |
811 | ||
4157cdb1 | 812 | const attribute = { |
90a8bd30 | 813 | extname, |
4157cdb1 | 814 | infoHash: parsed.infoHash, |
90a8bd30 | 815 | resolution, |
4157cdb1 | 816 | size: fileUrl.size, |
d7a25329 | 817 | fps: fileUrl.fps || -1, |
8319d6ae | 818 | metadataUrl: metadata?.href, |
d7a25329 | 819 | |
90a8bd30 C |
820 | // Use the name of the remote file because we don't proxify video file requests |
821 | filename: basename(fileUrl.href), | |
822 | fileUrl: fileUrl.href, | |
823 | ||
824 | torrentUrl, | |
825 | // Use our own torrent name since we proxify torrent requests | |
826 | torrentFilename: generateTorrentFileName(videoOrPlaylist, resolution), | |
827 | ||
d7a25329 | 828 | // This is a video file owned by a video or by a streaming playlist |
90a8bd30 C |
829 | videoId, |
830 | videoStreamingPlaylistId | |
09209296 C |
831 | } |
832 | ||
833 | attributes.push(attribute) | |
834 | } | |
835 | ||
836 | return attributes | |
837 | } | |
838 | ||
de6310b2 | 839 | function streamingPlaylistActivityUrlToDBAttributes (video: MVideoId, videoObject: VideoObject, videoFiles: MVideoFile[]) { |
09209296 C |
840 | const playlistUrls = videoObject.url.filter(u => isAPStreamingPlaylistUrlObject(u)) as ActivityPlaylistUrlObject[] |
841 | if (playlistUrls.length === 0) return [] | |
842 | ||
d7a25329 | 843 | const attributes: (FilteredModelAttributes<VideoStreamingPlaylistModel> & { tagAPObject?: ActivityTagObject[] })[] = [] |
09209296 | 844 | for (const playlistUrlObject of playlistUrls) { |
d7a25329 C |
845 | const segmentsSha256UrlObject = playlistUrlObject.tag.find(isAPPlaylistSegmentHashesUrlObject) |
846 | ||
847 | let files: unknown[] = playlistUrlObject.tag.filter(u => isAPVideoUrlObject(u)) as ActivityVideoUrlObject[] | |
848 | ||
849 | // FIXME: backward compatibility introduced in v2.1.0 | |
850 | if (files.length === 0) files = videoFiles | |
851 | ||
09209296 C |
852 | if (!segmentsSha256UrlObject) { |
853 | logger.warn('No segment sha256 URL found in AP playlist object.', { playlistUrl: playlistUrlObject }) | |
854 | continue | |
855 | } | |
856 | ||
857 | const attribute = { | |
858 | type: VideoStreamingPlaylistType.HLS, | |
859 | playlistUrl: playlistUrlObject.href, | |
860 | segmentsSha256Url: segmentsSha256UrlObject.href, | |
d7a25329 | 861 | p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrlObject.href, files), |
594d0c6a | 862 | p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION, |
d7a25329 C |
863 | videoId: video.id, |
864 | tagAPObject: playlistUrlObject.tag | |
09209296 C |
865 | } |
866 | ||
4157cdb1 C |
867 | attributes.push(attribute) |
868 | } | |
869 | ||
870 | return attributes | |
871 | } | |
ca6d3622 | 872 | |
de6310b2 | 873 | function getThumbnailFromIcons (videoObject: VideoObject) { |
ca6d3622 C |
874 | let validIcons = videoObject.icon.filter(i => i.width > THUMBNAILS_SIZE.minWidth) |
875 | // Fallback if there are not valid icons | |
876 | if (validIcons.length === 0) validIcons = videoObject.icon | |
877 | ||
878 | return minBy(validIcons, 'width') | |
879 | } | |
880 | ||
de6310b2 | 881 | function getPreviewFromIcons (videoObject: VideoObject) { |
ca6d3622 C |
882 | const validIcons = videoObject.icon.filter(i => i.width > PREVIEWS_SIZE.minWidth) |
883 | ||
ca6d3622 C |
884 | return maxBy(validIcons, 'width') |
885 | } | |
a8b1b404 | 886 | |
90a8bd30 | 887 | function getPreviewUrl (previewIcon: ActivityIconObject, video: MVideoWithHost) { |
a8b1b404 C |
888 | return previewIcon |
889 | ? previewIcon.url | |
90a8bd30 | 890 | : buildRemoteVideoBaseUrl(video, join(LAZY_STATIC_PATHS.PREVIEWS, video.generatePreviewName())) |
a8b1b404 | 891 | } |