]>
Commit | Line | Data |
---|---|---|
7acee6f1 | 1 | import * as Bluebird from 'bluebird' |
2186386c | 2 | import * as sequelize from 'sequelize' |
2ccaeeb3 | 3 | import * as magnetUtil from 'magnet-uri' |
892211e8 | 4 | import * as request from 'request' |
09209296 | 5 | import { |
09209296 C |
6 | ActivityPlaylistSegmentHashesObject, |
7 | ActivityPlaylistUrlObject, | |
8 | ActivityUrlObject, | |
1735c825 | 9 | ActivityVideoUrlObject, |
09209296 C |
10 | VideoState |
11 | } from '../../../shared/index' | |
2ccaeeb3 | 12 | import { VideoTorrentObject } from '../../../shared/models/activitypub/objects' |
1297eb5d | 13 | import { VideoPrivacy } from '../../../shared/models/videos' |
1d6e5dfc | 14 | import { sanitizeAndCheckVideoTorrentObject } from '../../helpers/custom-validators/activitypub/videos' |
2ccaeeb3 | 15 | import { isVideoFileInfoHashValid } from '../../helpers/custom-validators/videos' |
c48e82b5 | 16 | import { resetSequelizeInstance, retryTransactionWrapper } from '../../helpers/database-utils' |
2ccaeeb3 | 17 | import { logger } from '../../helpers/logger' |
dc852737 | 18 | import { doRequest, doRequestAndSaveToFile } from '../../helpers/requests' |
e8bafea3 C |
19 | import { |
20 | ACTIVITY_PUB, | |
21 | MIMETYPES, | |
22 | P2P_MEDIA_LOADER_PEER_VERSION, | |
23 | PREVIEWS_SIZE, | |
24 | REMOTE_SCHEME, | |
25 | STATIC_PATHS | |
26 | } from '../../initializers/constants' | |
2ccaeeb3 | 27 | import { TagModel } from '../../models/video/tag' |
3fd3ab2d | 28 | import { VideoModel } from '../../models/video/video' |
2ccaeeb3 | 29 | import { VideoFileModel } from '../../models/video/video-file' |
c48e82b5 | 30 | import { getOrCreateActorAndServerAndModel } from './actor' |
7acee6f1 | 31 | import { addVideoComments } from './video-comments' |
8fffe21a | 32 | import { crawlCollectionPage } from './crawl' |
2186386c | 33 | import { sendCreateVideo, sendUpdateVideo } from './send' |
40e87e9e C |
34 | import { isArray } from '../../helpers/custom-validators/misc' |
35 | import { VideoCaptionModel } from '../../models/video/video-caption' | |
f6eebcb3 C |
36 | import { JobQueue } from '../job-queue' |
37 | import { ActivitypubHttpFetcherPayload } from '../job-queue/handlers/activitypub-http-fetcher' | |
1297eb5d C |
38 | import { createRates } from './video-rates' |
39 | import { addVideoShares, shareVideoByServerAndChannel } from './share' | |
4157cdb1 | 40 | import { fetchVideoByUrl, VideoFetchByUrlType } from '../../helpers/video' |
848f499d | 41 | import { checkUrlsSameHost, getAPId } from '../../helpers/activitypub' |
cef534ed | 42 | import { Notifier } from '../notifier' |
09209296 C |
43 | import { VideoStreamingPlaylistModel } from '../../models/video/video-streaming-playlist' |
44 | import { VideoStreamingPlaylistType } from '../../../shared/models/videos/video-streaming-playlist.type' | |
2ba92871 C |
45 | import { AccountVideoRateModel } from '../../models/account/account-video-rate' |
46 | import { VideoShareModel } from '../../models/video/video-share' | |
47 | import { VideoCommentModel } from '../../models/video/video-comment' | |
74dc3bca | 48 | import { sequelizeTypescript } from '../../initializers/database' |
3acc5084 | 49 | import { createPlaceholderThumbnail, createVideoMiniatureFromUrl } from '../thumbnail' |
e8bafea3 C |
50 | import { ThumbnailType } from '../../../shared/models/videos/thumbnail.type' |
51 | import { join } from 'path' | |
3acc5084 | 52 | import { FilteredModelAttributes } from '../../typings/sequelize' |
b4055e1c | 53 | import { autoBlacklistVideoIfNeeded } from '../video-blacklist' |
6b9c966f | 54 | import { ActorFollowScoreCache } from '../files-cache' |
453e83ea C |
55 | import { |
56 | MAccountActor, | |
57 | MChannelAccountLight, | |
58 | MChannelDefault, | |
59 | MChannelId, | |
60 | MVideo, | |
0283eaac | 61 | MVideoAccountLightBlacklistAllFiles, |
453e83ea C |
62 | MVideoAccountLight, |
63 | MVideoAP, | |
64 | MVideoAPWithoutCaption, | |
65 | MVideoFile, | |
66 | MVideoFullLight, | |
67 | MVideoId, | |
96ca24f0 | 68 | MVideoThumbnail |
453e83ea C |
69 | } from '../../typings/models' |
70 | import { MThumbnail } from '../../typings/models/video/thumbnail' | |
71 | ||
72 | async function federateVideoIfNeeded (videoArg: MVideoAPWithoutCaption, isNewVideo: boolean, transaction?: sequelize.Transaction) { | |
73 | const video = videoArg as MVideoAP | |
2186386c | 74 | |
5b77537c C |
75 | if ( |
76 | // Check this is not a blacklisted video, or unfederated blacklisted video | |
77 | (video.isBlacklisted() === false || (isNewVideo === false && video.VideoBlacklist.unfederated === false)) && | |
78 | // Check the video is public/unlisted and published | |
79 | video.privacy !== VideoPrivacy.PRIVATE && video.state === VideoState.PUBLISHED | |
80 | ) { | |
40e87e9e C |
81 | // Fetch more attributes that we will need to serialize in AP object |
82 | if (isArray(video.VideoCaptions) === false) { | |
83 | video.VideoCaptions = await video.$get('VideoCaptions', { | |
84 | attributes: [ 'language' ], | |
85 | transaction | |
86 | }) as VideoCaptionModel[] | |
87 | } | |
88 | ||
2cebd797 | 89 | if (isNewVideo) { |
2186386c C |
90 | // Now we'll add the video's meta data to our followers |
91 | await sendCreateVideo(video, transaction) | |
92 | await shareVideoByServerAndChannel(video, transaction) | |
93 | } else { | |
94 | await sendUpdateVideo(video, transaction) | |
95 | } | |
96 | } | |
97 | } | |
892211e8 | 98 | |
4157cdb1 C |
99 | async function fetchRemoteVideo (videoUrl: string): Promise<{ response: request.RequestResponse, videoObject: VideoTorrentObject }> { |
100 | const options = { | |
101 | uri: videoUrl, | |
102 | method: 'GET', | |
103 | json: true, | |
104 | activityPub: true | |
105 | } | |
892211e8 | 106 | |
4157cdb1 C |
107 | logger.info('Fetching remote video %s.', videoUrl) |
108 | ||
109 | const { response, body } = await doRequest(options) | |
110 | ||
5c6d985f | 111 | if (sanitizeAndCheckVideoTorrentObject(body) === false || checkUrlsSameHost(body.id, videoUrl) !== true) { |
4157cdb1 C |
112 | logger.debug('Remote video JSON is not valid.', { body }) |
113 | return { response, videoObject: undefined } | |
114 | } | |
115 | ||
116 | return { response, videoObject: body } | |
892211e8 C |
117 | } |
118 | ||
453e83ea | 119 | async function fetchRemoteVideoDescription (video: MVideoAccountLight) { |
50d6de9c | 120 | const host = video.VideoChannel.Account.Actor.Server.host |
96f29c0f | 121 | const path = video.getDescriptionAPIPath() |
892211e8 C |
122 | const options = { |
123 | uri: REMOTE_SCHEME.HTTP + '://' + host + path, | |
124 | json: true | |
125 | } | |
126 | ||
127 | const { body } = await doRequest(options) | |
128 | return body.description ? body.description : '' | |
129 | } | |
130 | ||
453e83ea | 131 | function fetchRemoteVideoStaticFile (video: MVideoAccountLight, path: string, destPath: string) { |
e8bafea3 | 132 | const url = buildRemoteBaseUrl(video, path) |
4157cdb1 C |
133 | |
134 | // We need to provide a callback, if no we could have an uncaught exception | |
dc852737 | 135 | return doRequestAndSaveToFile({ uri: url }, destPath) |
4157cdb1 C |
136 | } |
137 | ||
453e83ea | 138 | function buildRemoteBaseUrl (video: MVideoAccountLight, path: string) { |
e8bafea3 | 139 | const host = video.VideoChannel.Account.Actor.Server.host |
892211e8 | 140 | |
e8bafea3 | 141 | return REMOTE_SCHEME.HTTP + '://' + host + path |
892211e8 C |
142 | } |
143 | ||
f37dc0dd | 144 | function getOrCreateVideoChannelFromVideoObject (videoObject: VideoTorrentObject) { |
0f320037 C |
145 | const channel = videoObject.attributedTo.find(a => a.type === 'Group') |
146 | if (!channel) throw new Error('Cannot find associated video channel to video ' + videoObject.url) | |
147 | ||
5c6d985f C |
148 | if (checkUrlsSameHost(channel.id, videoObject.id) !== true) { |
149 | throw new Error(`Video channel url ${channel.id} does not have the same host than video object id ${videoObject.id}`) | |
150 | } | |
151 | ||
e587e0ec | 152 | return getOrCreateActorAndServerAndModel(channel.id, 'all') |
0f320037 C |
153 | } |
154 | ||
f6eebcb3 | 155 | type SyncParam = { |
1297eb5d C |
156 | likes: boolean |
157 | dislikes: boolean | |
158 | shares: boolean | |
159 | comments: boolean | |
f6eebcb3 | 160 | thumbnail: boolean |
04b8c3fb | 161 | refreshVideo?: boolean |
f6eebcb3 | 162 | } |
453e83ea | 163 | async function syncVideoExternalAttributes (video: MVideo, fetchedVideo: VideoTorrentObject, syncParam: SyncParam) { |
f6eebcb3 | 164 | logger.info('Adding likes/dislikes/shares/comments of video %s.', video.uuid) |
2ccaeeb3 | 165 | |
f6eebcb3 | 166 | const jobPayloads: ActivitypubHttpFetcherPayload[] = [] |
2ccaeeb3 | 167 | |
f6eebcb3 | 168 | if (syncParam.likes === true) { |
2ba92871 C |
169 | const handler = items => createRates(items, video, 'like') |
170 | const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'like' as 'like', crawlStartDate) | |
171 | ||
172 | await crawlCollectionPage<string>(fetchedVideo.likes, handler, cleaner) | |
f6eebcb3 C |
173 | .catch(err => logger.error('Cannot add likes of video %s.', video.uuid, { err })) |
174 | } else { | |
175 | jobPayloads.push({ uri: fetchedVideo.likes, videoId: video.id, type: 'video-likes' as 'video-likes' }) | |
176 | } | |
7acee6f1 | 177 | |
f6eebcb3 | 178 | if (syncParam.dislikes === true) { |
2ba92871 C |
179 | const handler = items => createRates(items, video, 'dislike') |
180 | const cleaner = crawlStartDate => AccountVideoRateModel.cleanOldRatesOf(video.id, 'dislike' as 'dislike', crawlStartDate) | |
181 | ||
182 | await crawlCollectionPage<string>(fetchedVideo.dislikes, handler, cleaner) | |
f6eebcb3 C |
183 | .catch(err => logger.error('Cannot add dislikes of video %s.', video.uuid, { err })) |
184 | } else { | |
185 | jobPayloads.push({ uri: fetchedVideo.dislikes, videoId: video.id, type: 'video-dislikes' as 'video-dislikes' }) | |
186 | } | |
187 | ||
188 | if (syncParam.shares === true) { | |
2ba92871 C |
189 | const handler = items => addVideoShares(items, video) |
190 | const cleaner = crawlStartDate => VideoShareModel.cleanOldSharesOf(video.id, crawlStartDate) | |
191 | ||
192 | await crawlCollectionPage<string>(fetchedVideo.shares, handler, cleaner) | |
f6eebcb3 C |
193 | .catch(err => logger.error('Cannot add shares of video %s.', video.uuid, { err })) |
194 | } else { | |
195 | jobPayloads.push({ uri: fetchedVideo.shares, videoId: video.id, type: 'video-shares' as 'video-shares' }) | |
196 | } | |
7acee6f1 | 197 | |
f6eebcb3 | 198 | if (syncParam.comments === true) { |
6b9c966f | 199 | const handler = items => addVideoComments(items) |
2ba92871 C |
200 | const cleaner = crawlStartDate => VideoCommentModel.cleanOldCommentsOf(video.id, crawlStartDate) |
201 | ||
202 | await crawlCollectionPage<string>(fetchedVideo.comments, handler, cleaner) | |
f6eebcb3 C |
203 | .catch(err => logger.error('Cannot add comments of video %s.', video.uuid, { err })) |
204 | } else { | |
2ba92871 | 205 | jobPayloads.push({ uri: fetchedVideo.comments, videoId: video.id, type: 'video-comments' as 'video-comments' }) |
f6eebcb3 | 206 | } |
7acee6f1 | 207 | |
f6eebcb3 | 208 | await Bluebird.map(jobPayloads, payload => JobQueue.Instance.createJob({ type: 'activitypub-http-fetcher', payload })) |
2ccaeeb3 C |
209 | } |
210 | ||
453e83ea C |
211 | function getOrCreateVideoAndAccountAndChannel (options: { |
212 | videoObject: { id: string } | string, | |
213 | syncParam?: SyncParam, | |
214 | fetchType?: 'all', | |
215 | allowRefresh?: boolean | |
0283eaac | 216 | }): Promise<{ video: MVideoAccountLightBlacklistAllFiles, created: boolean, autoBlacklisted?: boolean }> |
453e83ea C |
217 | function getOrCreateVideoAndAccountAndChannel (options: { |
218 | videoObject: { id: string } | string, | |
219 | syncParam?: SyncParam, | |
220 | fetchType?: VideoFetchByUrlType, | |
221 | allowRefresh?: boolean | |
0283eaac | 222 | }): Promise<{ video: MVideoAccountLightBlacklistAllFiles | MVideoThumbnail, created: boolean, autoBlacklisted?: boolean }> |
4157cdb1 | 223 | async function getOrCreateVideoAndAccountAndChannel (options: { |
848f499d | 224 | videoObject: { id: string } | string, |
4157cdb1 | 225 | syncParam?: SyncParam, |
d4defe07 | 226 | fetchType?: VideoFetchByUrlType, |
74577825 | 227 | allowRefresh?: boolean // true by default |
0283eaac | 228 | }): Promise<{ video: MVideoAccountLightBlacklistAllFiles | MVideoThumbnail, created: boolean, autoBlacklisted?: boolean }> { |
4157cdb1 C |
229 | // Default params |
230 | const syncParam = options.syncParam || { likes: true, dislikes: true, shares: true, comments: true, thumbnail: true, refreshVideo: false } | |
231 | const fetchType = options.fetchType || 'all' | |
74577825 | 232 | const allowRefresh = options.allowRefresh !== false |
1297eb5d | 233 | |
4157cdb1 | 234 | // Get video url |
848f499d | 235 | const videoUrl = getAPId(options.videoObject) |
1297eb5d | 236 | |
4157cdb1 C |
237 | let videoFromDatabase = await fetchVideoByUrl(videoUrl, fetchType) |
238 | if (videoFromDatabase) { | |
f58094b2 | 239 | if (videoFromDatabase.isOutdated() && allowRefresh === true) { |
74577825 C |
240 | const refreshOptions = { |
241 | video: videoFromDatabase, | |
242 | fetchedType: fetchType, | |
243 | syncParam | |
244 | } | |
245 | ||
246 | if (syncParam.refreshVideo === true) videoFromDatabase = await refreshVideoIfNeeded(refreshOptions) | |
744d0eca | 247 | else await JobQueue.Instance.createJob({ type: 'activitypub-refresher', payload: { type: 'video', url: videoFromDatabase.url } }) |
d4defe07 | 248 | } |
1297eb5d | 249 | |
cef534ed | 250 | return { video: videoFromDatabase, created: false } |
1297eb5d C |
251 | } |
252 | ||
4157cdb1 C |
253 | const { videoObject: fetchedVideo } = await fetchRemoteVideo(videoUrl) |
254 | if (!fetchedVideo) throw new Error('Cannot fetch remote video with url: ' + videoUrl) | |
7acee6f1 | 255 | |
453e83ea C |
256 | const actor = await getOrCreateVideoChannelFromVideoObject(fetchedVideo) |
257 | const videoChannel = actor.VideoChannel | |
258 | const { autoBlacklisted, videoCreated } = await retryTransactionWrapper(createVideo, fetchedVideo, videoChannel, syncParam.thumbnail) | |
7acee6f1 | 259 | |
6691c522 | 260 | await syncVideoExternalAttributes(videoCreated, fetchedVideo, syncParam) |
7acee6f1 | 261 | |
6691c522 | 262 | return { video: videoCreated, created: true, autoBlacklisted } |
7acee6f1 C |
263 | } |
264 | ||
d4defe07 | 265 | async function updateVideoFromAP (options: { |
0283eaac | 266 | video: MVideoAccountLightBlacklistAllFiles, |
1297eb5d | 267 | videoObject: VideoTorrentObject, |
453e83ea C |
268 | account: MAccountActor, |
269 | channel: MChannelDefault, | |
1297eb5d | 270 | overrideTo?: string[] |
d4defe07 | 271 | }) { |
b4055e1c C |
272 | const { video, videoObject, account, channel, overrideTo } = options |
273 | ||
453e83ea | 274 | logger.debug('Updating remote video "%s".', options.videoObject.uuid, { account, channel }) |
e8d246d5 | 275 | |
1297eb5d | 276 | let videoFieldsSave: any |
b4055e1c C |
277 | const wasPrivateVideo = video.privacy === VideoPrivacy.PRIVATE |
278 | const wasUnlistedVideo = video.privacy === VideoPrivacy.UNLISTED | |
1297eb5d C |
279 | |
280 | try { | |
453e83ea | 281 | let thumbnailModel: MThumbnail |
e8bafea3 C |
282 | |
283 | try { | |
b4055e1c | 284 | thumbnailModel = await createVideoMiniatureFromUrl(videoObject.icon.url, video, ThumbnailType.MINIATURE) |
e8bafea3 | 285 | } catch (err) { |
b4055e1c | 286 | logger.warn('Cannot generate thumbnail of %s.', videoObject.id, { err }) |
e8bafea3 C |
287 | } |
288 | ||
453e83ea | 289 | const videoUpdated = await sequelizeTypescript.transaction(async t => { |
e8d246d5 | 290 | const sequelizeOptions = { transaction: t } |
2ccaeeb3 | 291 | |
b4055e1c | 292 | videoFieldsSave = video.toJSON() |
2ccaeeb3 | 293 | |
1297eb5d | 294 | // Check actor has the right to update the video |
b4055e1c C |
295 | const videoChannel = video.VideoChannel |
296 | if (videoChannel.Account.id !== account.id) { | |
297 | throw new Error('Account ' + account.Actor.url + ' does not own video channel ' + videoChannel.Actor.url) | |
f6eebcb3 C |
298 | } |
299 | ||
b4055e1c C |
300 | const to = overrideTo ? overrideTo : videoObject.to |
301 | const videoData = await videoActivityObjectToDBAttributes(channel, videoObject, to) | |
302 | video.name = videoData.name | |
303 | video.uuid = videoData.uuid | |
304 | video.url = videoData.url | |
305 | video.category = videoData.category | |
306 | video.licence = videoData.licence | |
307 | video.language = videoData.language | |
308 | video.description = videoData.description | |
309 | video.support = videoData.support | |
310 | video.nsfw = videoData.nsfw | |
311 | video.commentsEnabled = videoData.commentsEnabled | |
312 | video.downloadEnabled = videoData.downloadEnabled | |
313 | video.waitTranscoding = videoData.waitTranscoding | |
314 | video.state = videoData.state | |
315 | video.duration = videoData.duration | |
316 | video.createdAt = videoData.createdAt | |
317 | video.publishedAt = videoData.publishedAt | |
318 | video.originallyPublishedAt = videoData.originallyPublishedAt | |
319 | video.privacy = videoData.privacy | |
320 | video.channelId = videoData.channelId | |
321 | video.views = videoData.views | |
322 | ||
453e83ea | 323 | const videoUpdated = await video.save(sequelizeOptions) as MVideoFullLight |
b4055e1c | 324 | |
453e83ea | 325 | if (thumbnailModel) await videoUpdated.addAndSaveThumbnail(thumbnailModel, t) |
e8bafea3 C |
326 | |
327 | // FIXME: use icon URL instead | |
453e83ea | 328 | const previewUrl = buildRemoteBaseUrl(videoUpdated, join(STATIC_PATHS.PREVIEWS, videoUpdated.getPreview().filename)) |
b4055e1c | 329 | const previewModel = createPlaceholderThumbnail(previewUrl, video, ThumbnailType.PREVIEW, PREVIEWS_SIZE) |
453e83ea | 330 | await videoUpdated.addAndSaveThumbnail(previewModel, t) |
e8bafea3 | 331 | |
e5565833 | 332 | { |
453e83ea | 333 | const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoUpdated, videoObject) |
e5565833 | 334 | const newVideoFiles = videoFileAttributes.map(a => new VideoFileModel(a)) |
0032ebe9 | 335 | |
e5565833 | 336 | // Remove video files that do not exist anymore |
453e83ea | 337 | const destroyTasks = videoUpdated.VideoFiles |
b4055e1c C |
338 | .filter(f => !newVideoFiles.find(newFile => newFile.hasSameUniqueKeysThan(f))) |
339 | .map(f => f.destroy(sequelizeOptions)) | |
e5565833 | 340 | await Promise.all(destroyTasks) |
2ccaeeb3 | 341 | |
e5565833 | 342 | // Update or add other one |
d382f4e9 | 343 | const upsertTasks = videoFileAttributes.map(a => { |
3acc5084 | 344 | return VideoFileModel.upsert<VideoFileModel>(a, { returning: true, transaction: t }) |
d382f4e9 C |
345 | .then(([ file ]) => file) |
346 | }) | |
347 | ||
453e83ea | 348 | videoUpdated.VideoFiles = await Promise.all(upsertTasks) |
e5565833 | 349 | } |
2ccaeeb3 | 350 | |
09209296 | 351 | { |
453e83ea | 352 | const streamingPlaylistAttributes = streamingPlaylistActivityUrlToDBAttributes(videoUpdated, videoObject, videoUpdated.VideoFiles) |
09209296 C |
353 | const newStreamingPlaylists = streamingPlaylistAttributes.map(a => new VideoStreamingPlaylistModel(a)) |
354 | ||
355 | // Remove video files that do not exist anymore | |
453e83ea | 356 | const destroyTasks = videoUpdated.VideoStreamingPlaylists |
b4055e1c C |
357 | .filter(f => !newStreamingPlaylists.find(newPlaylist => newPlaylist.hasSameUniqueKeysThan(f))) |
358 | .map(f => f.destroy(sequelizeOptions)) | |
09209296 C |
359 | await Promise.all(destroyTasks) |
360 | ||
361 | // Update or add other one | |
362 | const upsertTasks = streamingPlaylistAttributes.map(a => { | |
3acc5084 | 363 | return VideoStreamingPlaylistModel.upsert<VideoStreamingPlaylistModel>(a, { returning: true, transaction: t }) |
09209296 C |
364 | .then(([ streamingPlaylist ]) => streamingPlaylist) |
365 | }) | |
366 | ||
453e83ea | 367 | videoUpdated.VideoStreamingPlaylists = await Promise.all(upsertTasks) |
09209296 C |
368 | } |
369 | ||
e5565833 C |
370 | { |
371 | // Update Tags | |
b4055e1c | 372 | const tags = videoObject.tag.map(tag => tag.name) |
e5565833 | 373 | const tagInstances = await TagModel.findOrCreateTags(tags, t) |
453e83ea | 374 | await videoUpdated.$set('Tags', tagInstances, sequelizeOptions) |
e5565833 | 375 | } |
2ccaeeb3 | 376 | |
e5565833 C |
377 | { |
378 | // Update captions | |
453e83ea | 379 | await VideoCaptionModel.deleteAllCaptionsOfRemoteVideo(videoUpdated.id, t) |
e5565833 | 380 | |
b4055e1c | 381 | const videoCaptionsPromises = videoObject.subtitleLanguage.map(c => { |
453e83ea | 382 | return VideoCaptionModel.insertOrReplaceLanguage(videoUpdated.id, c.identifier, t) |
e5565833 | 383 | }) |
453e83ea | 384 | await Promise.all(videoCaptionsPromises) |
e5565833 | 385 | } |
453e83ea C |
386 | |
387 | return videoUpdated | |
1297eb5d C |
388 | }) |
389 | ||
5b77537c | 390 | await autoBlacklistVideoIfNeeded({ |
453e83ea | 391 | video: videoUpdated, |
6691c522 C |
392 | user: undefined, |
393 | isRemote: true, | |
394 | isNew: false, | |
395 | transaction: undefined | |
396 | }) | |
b4055e1c | 397 | |
453e83ea | 398 | if (wasPrivateVideo || wasUnlistedVideo) Notifier.Instance.notifyOnNewVideoIfNeeded(videoUpdated) // Notify our users? |
e8d246d5 | 399 | |
b4055e1c | 400 | logger.info('Remote video with uuid %s updated', videoObject.uuid) |
453e83ea C |
401 | |
402 | return videoUpdated | |
1297eb5d | 403 | } catch (err) { |
b4055e1c C |
404 | if (video !== undefined && videoFieldsSave !== undefined) { |
405 | resetSequelizeInstance(video, videoFieldsSave) | |
1297eb5d C |
406 | } |
407 | ||
408 | // This is just a debug because we will retry the insert | |
409 | logger.debug('Cannot update the remote video.', { err }) | |
410 | throw err | |
411 | } | |
892211e8 | 412 | } |
2186386c | 413 | |
04b8c3fb | 414 | async function refreshVideoIfNeeded (options: { |
453e83ea | 415 | video: MVideoThumbnail, |
04b8c3fb C |
416 | fetchedType: VideoFetchByUrlType, |
417 | syncParam: SyncParam | |
453e83ea | 418 | }): Promise<MVideoThumbnail> { |
04b8c3fb C |
419 | if (!options.video.isOutdated()) return options.video |
420 | ||
421 | // We need more attributes if the argument video was fetched with not enough joints | |
b4055e1c | 422 | const video = options.fetchedType === 'all' |
0283eaac | 423 | ? options.video as MVideoAccountLightBlacklistAllFiles |
b4055e1c | 424 | : await VideoModel.loadByUrlAndPopulateAccount(options.video.url) |
04b8c3fb C |
425 | |
426 | try { | |
427 | const { response, videoObject } = await fetchRemoteVideo(video.url) | |
428 | if (response.statusCode === 404) { | |
429 | logger.info('Cannot refresh remote video %s: video does not exist anymore. Deleting it.', video.url) | |
430 | ||
431 | // Video does not exist anymore | |
432 | await video.destroy() | |
433 | return undefined | |
434 | } | |
435 | ||
436 | if (videoObject === undefined) { | |
437 | logger.warn('Cannot refresh remote video %s: invalid body.', video.url) | |
438 | ||
439 | await video.setAsRefreshed() | |
440 | return video | |
441 | } | |
442 | ||
443 | const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject) | |
04b8c3fb C |
444 | |
445 | const updateOptions = { | |
446 | video, | |
447 | videoObject, | |
453e83ea | 448 | account: channelActor.VideoChannel.Account, |
04b8c3fb C |
449 | channel: channelActor.VideoChannel |
450 | } | |
451 | await retryTransactionWrapper(updateVideoFromAP, updateOptions) | |
452 | await syncVideoExternalAttributes(video, videoObject, options.syncParam) | |
453 | ||
6b9c966f C |
454 | ActorFollowScoreCache.Instance.addGoodServerId(video.VideoChannel.Actor.serverId) |
455 | ||
04b8c3fb C |
456 | return video |
457 | } catch (err) { | |
458 | logger.warn('Cannot refresh video %s.', options.video.url, { err }) | |
459 | ||
6b9c966f C |
460 | ActorFollowScoreCache.Instance.addBadServerId(video.VideoChannel.Actor.serverId) |
461 | ||
04b8c3fb C |
462 | // Don't refresh in loop |
463 | await video.setAsRefreshed() | |
464 | return video | |
465 | } | |
892211e8 | 466 | } |
2186386c C |
467 | |
468 | export { | |
1297eb5d | 469 | updateVideoFromAP, |
04b8c3fb | 470 | refreshVideoIfNeeded, |
2186386c C |
471 | federateVideoIfNeeded, |
472 | fetchRemoteVideo, | |
1297eb5d | 473 | getOrCreateVideoAndAccountAndChannel, |
40e87e9e | 474 | fetchRemoteVideoStaticFile, |
2186386c | 475 | fetchRemoteVideoDescription, |
4157cdb1 | 476 | getOrCreateVideoChannelFromVideoObject |
2186386c | 477 | } |
c48e82b5 C |
478 | |
479 | // --------------------------------------------------------------------------- | |
480 | ||
09209296 | 481 | function isAPVideoUrlObject (url: ActivityUrlObject): url is ActivityVideoUrlObject { |
14e2014a | 482 | const mimeTypes = Object.keys(MIMETYPES.VIDEO.MIMETYPE_EXT) |
c48e82b5 | 483 | |
e27ff5da C |
484 | const urlMediaType = url.mediaType || url.mimeType |
485 | return mimeTypes.indexOf(urlMediaType) !== -1 && urlMediaType.startsWith('video/') | |
c48e82b5 | 486 | } |
4157cdb1 | 487 | |
09209296 C |
488 | function isAPStreamingPlaylistUrlObject (url: ActivityUrlObject): url is ActivityPlaylistUrlObject { |
489 | const urlMediaType = url.mediaType || url.mimeType | |
490 | ||
491 | return urlMediaType === 'application/x-mpegURL' | |
492 | } | |
493 | ||
494 | function isAPPlaylistSegmentHashesUrlObject (tag: any): tag is ActivityPlaylistSegmentHashesObject { | |
495 | const urlMediaType = tag.mediaType || tag.mimeType | |
496 | ||
497 | return tag.name === 'sha256' && tag.type === 'Link' && urlMediaType === 'application/json' | |
c48e82b5 | 498 | } |
4157cdb1 | 499 | |
453e83ea | 500 | async function createVideo (videoObject: VideoTorrentObject, channel: MChannelAccountLight, waitThumbnail = false) { |
4157cdb1 C |
501 | logger.debug('Adding remote video %s.', videoObject.id) |
502 | ||
453e83ea C |
503 | const videoData = await videoActivityObjectToDBAttributes(channel, videoObject, videoObject.to) |
504 | const video = VideoModel.build(videoData) as MVideoThumbnail | |
e8bafea3 | 505 | |
3acc5084 | 506 | const promiseThumbnail = createVideoMiniatureFromUrl(videoObject.icon.url, video, ThumbnailType.MINIATURE) |
e8bafea3 | 507 | |
453e83ea | 508 | let thumbnailModel: MThumbnail |
e8bafea3 C |
509 | if (waitThumbnail === true) { |
510 | thumbnailModel = await promiseThumbnail | |
511 | } | |
512 | ||
6691c522 | 513 | const { autoBlacklisted, videoCreated } = await sequelizeTypescript.transaction(async t => { |
4157cdb1 C |
514 | const sequelizeOptions = { transaction: t } |
515 | ||
453e83ea C |
516 | const videoCreated = await video.save(sequelizeOptions) as MVideoFullLight |
517 | videoCreated.VideoChannel = channel | |
e8bafea3 | 518 | |
3acc5084 | 519 | if (thumbnailModel) await videoCreated.addAndSaveThumbnail(thumbnailModel, t) |
e8bafea3 C |
520 | |
521 | // FIXME: use icon URL instead | |
522 | const previewUrl = buildRemoteBaseUrl(videoCreated, join(STATIC_PATHS.PREVIEWS, video.generatePreviewName())) | |
523 | const previewModel = createPlaceholderThumbnail(previewUrl, video, ThumbnailType.PREVIEW, PREVIEWS_SIZE) | |
3acc5084 | 524 | if (thumbnailModel) await videoCreated.addAndSaveThumbnail(previewModel, t) |
4157cdb1 C |
525 | |
526 | // Process files | |
527 | const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoCreated, videoObject) | |
528 | if (videoFileAttributes.length === 0) { | |
529 | throw new Error('Cannot find valid files for video %s ' + videoObject.url) | |
530 | } | |
531 | ||
532 | const videoFilePromises = videoFileAttributes.map(f => VideoFileModel.create(f, { transaction: t })) | |
ae9bbed4 | 533 | const videoFiles = await Promise.all(videoFilePromises) |
4157cdb1 | 534 | |
ae9bbed4 | 535 | const videoStreamingPlaylists = streamingPlaylistActivityUrlToDBAttributes(videoCreated, videoObject, videoFiles) |
09209296 | 536 | const playlistPromises = videoStreamingPlaylists.map(p => VideoStreamingPlaylistModel.create(p, { transaction: t })) |
6b9c966f | 537 | const streamingPlaylists = await Promise.all(playlistPromises) |
09209296 | 538 | |
4157cdb1 | 539 | // Process tags |
09209296 C |
540 | const tags = videoObject.tag |
541 | .filter(t => t.type === 'Hashtag') | |
542 | .map(t => t.name) | |
4157cdb1 C |
543 | const tagInstances = await TagModel.findOrCreateTags(tags, t) |
544 | await videoCreated.$set('Tags', tagInstances, sequelizeOptions) | |
545 | ||
546 | // Process captions | |
547 | const videoCaptionsPromises = videoObject.subtitleLanguage.map(c => { | |
548 | return VideoCaptionModel.insertOrReplaceLanguage(videoCreated.id, c.identifier, t) | |
549 | }) | |
453e83ea | 550 | await Promise.all(videoCaptionsPromises) |
6b9c966f | 551 | |
453e83ea C |
552 | videoCreated.VideoFiles = videoFiles |
553 | videoCreated.VideoStreamingPlaylists = streamingPlaylists | |
554 | videoCreated.Tags = tagInstances | |
4157cdb1 | 555 | |
6691c522 | 556 | const autoBlacklisted = await autoBlacklistVideoIfNeeded({ |
453e83ea | 557 | video: videoCreated, |
6691c522 C |
558 | user: undefined, |
559 | isRemote: true, | |
560 | isNew: true, | |
561 | transaction: t | |
562 | }) | |
563 | ||
4157cdb1 C |
564 | logger.info('Remote video with uuid %s inserted.', videoObject.uuid) |
565 | ||
6691c522 | 566 | return { autoBlacklisted, videoCreated } |
4157cdb1 C |
567 | }) |
568 | ||
e8bafea3 C |
569 | if (waitThumbnail === false) { |
570 | promiseThumbnail.then(thumbnailModel => { | |
571 | thumbnailModel = videoCreated.id | |
4157cdb1 | 572 | |
e8bafea3 C |
573 | return thumbnailModel.save() |
574 | }) | |
575 | } | |
4157cdb1 | 576 | |
6691c522 | 577 | return { autoBlacklisted, videoCreated } |
4157cdb1 C |
578 | } |
579 | ||
453e83ea | 580 | async function videoActivityObjectToDBAttributes (videoChannel: MChannelId, videoObject: VideoTorrentObject, to: string[] = []) { |
4157cdb1 C |
581 | const privacy = to.indexOf(ACTIVITY_PUB.PUBLIC) !== -1 ? VideoPrivacy.PUBLIC : VideoPrivacy.UNLISTED |
582 | const duration = videoObject.duration.replace(/[^\d]+/, '') | |
583 | ||
584 | let language: string | undefined | |
585 | if (videoObject.language) { | |
586 | language = videoObject.language.identifier | |
587 | } | |
588 | ||
589 | let category: number | undefined | |
590 | if (videoObject.category) { | |
591 | category = parseInt(videoObject.category.identifier, 10) | |
592 | } | |
593 | ||
594 | let licence: number | undefined | |
595 | if (videoObject.licence) { | |
596 | licence = parseInt(videoObject.licence.identifier, 10) | |
597 | } | |
598 | ||
599 | const description = videoObject.content || null | |
600 | const support = videoObject.support || null | |
601 | ||
602 | return { | |
603 | name: videoObject.name, | |
604 | uuid: videoObject.uuid, | |
605 | url: videoObject.id, | |
606 | category, | |
607 | licence, | |
608 | language, | |
609 | description, | |
610 | support, | |
611 | nsfw: videoObject.sensitive, | |
612 | commentsEnabled: videoObject.commentsEnabled, | |
7f2cfe3a | 613 | downloadEnabled: videoObject.downloadEnabled, |
4157cdb1 C |
614 | waitTranscoding: videoObject.waitTranscoding, |
615 | state: videoObject.state, | |
616 | channelId: videoChannel.id, | |
617 | duration: parseInt(duration, 10), | |
618 | createdAt: new Date(videoObject.published), | |
619 | publishedAt: new Date(videoObject.published), | |
7519127b | 620 | originallyPublishedAt: videoObject.originallyPublishedAt ? new Date(videoObject.originallyPublishedAt) : null, |
4157cdb1 C |
621 | // FIXME: updatedAt does not seems to be considered by Sequelize |
622 | updatedAt: new Date(videoObject.updated), | |
623 | views: videoObject.views, | |
624 | likes: 0, | |
625 | dislikes: 0, | |
626 | remote: true, | |
627 | privacy | |
628 | } | |
629 | } | |
630 | ||
453e83ea | 631 | function videoFileActivityUrlToDBAttributes (video: MVideo, videoObject: VideoTorrentObject) { |
09209296 | 632 | const fileUrls = videoObject.url.filter(u => isAPVideoUrlObject(u)) as ActivityVideoUrlObject[] |
4157cdb1 C |
633 | |
634 | if (fileUrls.length === 0) { | |
a3737cbf | 635 | throw new Error('Cannot find video files for ' + video.url) |
4157cdb1 C |
636 | } |
637 | ||
3acc5084 | 638 | const attributes: FilteredModelAttributes<VideoFileModel>[] = [] |
4157cdb1 C |
639 | for (const fileUrl of fileUrls) { |
640 | // Fetch associated magnet uri | |
641 | const magnet = videoObject.url.find(u => { | |
e27ff5da C |
642 | const mediaType = u.mediaType || u.mimeType |
643 | return mediaType === 'application/x-bittorrent;x-scheme-handler/magnet' && (u as any).height === fileUrl.height | |
4157cdb1 C |
644 | }) |
645 | ||
646 | if (!magnet) throw new Error('Cannot find associated magnet uri for file ' + fileUrl.href) | |
647 | ||
648 | const parsed = magnetUtil.decode(magnet.href) | |
649 | if (!parsed || isVideoFileInfoHashValid(parsed.infoHash) === false) { | |
650 | throw new Error('Cannot parse magnet URI ' + magnet.href) | |
651 | } | |
652 | ||
e27ff5da | 653 | const mediaType = fileUrl.mediaType || fileUrl.mimeType |
4157cdb1 | 654 | const attribute = { |
14e2014a | 655 | extname: MIMETYPES.VIDEO.MIMETYPE_EXT[ mediaType ], |
4157cdb1 C |
656 | infoHash: parsed.infoHash, |
657 | resolution: fileUrl.height, | |
658 | size: fileUrl.size, | |
a3737cbf | 659 | videoId: video.id, |
2e7cf5ae | 660 | fps: fileUrl.fps || -1 |
09209296 C |
661 | } |
662 | ||
663 | attributes.push(attribute) | |
664 | } | |
665 | ||
666 | return attributes | |
667 | } | |
668 | ||
453e83ea | 669 | function streamingPlaylistActivityUrlToDBAttributes (video: MVideoId, videoObject: VideoTorrentObject, videoFiles: MVideoFile[]) { |
09209296 C |
670 | const playlistUrls = videoObject.url.filter(u => isAPStreamingPlaylistUrlObject(u)) as ActivityPlaylistUrlObject[] |
671 | if (playlistUrls.length === 0) return [] | |
672 | ||
3acc5084 | 673 | const attributes: FilteredModelAttributes<VideoStreamingPlaylistModel>[] = [] |
09209296 | 674 | for (const playlistUrlObject of playlistUrls) { |
09209296 C |
675 | const segmentsSha256UrlObject = playlistUrlObject.tag |
676 | .find(t => { | |
677 | return isAPPlaylistSegmentHashesUrlObject(t) | |
678 | }) as ActivityPlaylistSegmentHashesObject | |
679 | if (!segmentsSha256UrlObject) { | |
680 | logger.warn('No segment sha256 URL found in AP playlist object.', { playlistUrl: playlistUrlObject }) | |
681 | continue | |
682 | } | |
683 | ||
684 | const attribute = { | |
685 | type: VideoStreamingPlaylistType.HLS, | |
686 | playlistUrl: playlistUrlObject.href, | |
687 | segmentsSha256Url: segmentsSha256UrlObject.href, | |
ae9bbed4 | 688 | p2pMediaLoaderInfohashes: VideoStreamingPlaylistModel.buildP2PMediaLoaderInfoHashes(playlistUrlObject.href, videoFiles), |
594d0c6a | 689 | p2pMediaLoaderPeerVersion: P2P_MEDIA_LOADER_PEER_VERSION, |
09209296 C |
690 | videoId: video.id |
691 | } | |
692 | ||
4157cdb1 C |
693 | attributes.push(attribute) |
694 | } | |
695 | ||
696 | return attributes | |
697 | } |