+ const { videoObject: fetchedVideo } = await fetchRemoteVideo(videoUrl)
+ if (!fetchedVideo) throw new Error('Cannot fetch remote video with url: ' + videoUrl)
+
+ const actor = await getOrCreateVideoChannelFromVideoObject(fetchedVideo)
+ const videoChannel = actor.VideoChannel
+ const { autoBlacklisted, videoCreated } = await retryTransactionWrapper(createVideo, fetchedVideo, videoChannel, syncParam.thumbnail)
+
+ await syncVideoExternalAttributes(videoCreated, fetchedVideo, syncParam)
+
+ return { video: videoCreated, created: true, autoBlacklisted }
+}
+
+async function updateVideoFromAP (options: {
+ video: MVideoAccountLightBlacklistAllFiles
+ videoObject: VideoTorrentObject
+ account: MAccountIdActor
+ channel: MChannelDefault
+ overrideTo?: string[]
+}) {
+ const { video, videoObject, account, channel, overrideTo } = options
+
+ logger.debug('Updating remote video "%s".', options.videoObject.uuid, { account, channel })
+
+ let videoFieldsSave: any
+ const wasPrivateVideo = video.privacy === VideoPrivacy.PRIVATE
+ const wasUnlistedVideo = video.privacy === VideoPrivacy.UNLISTED
+
+ try {
+ let thumbnailModel: MThumbnail
+
+ try {
+ thumbnailModel = await createVideoMiniatureFromUrl(getThumbnailFromIcons(videoObject).url, video, ThumbnailType.MINIATURE)
+ } catch (err) {
+ logger.warn('Cannot generate thumbnail of %s.', videoObject.id, { err })
+ }
+
+ const videoUpdated = await sequelizeTypescript.transaction(async t => {
+ const sequelizeOptions = { transaction: t }
+
+ videoFieldsSave = video.toJSON()
+
+ // Check actor has the right to update the video
+ const videoChannel = video.VideoChannel
+ if (videoChannel.Account.id !== account.id) {
+ throw new Error('Account ' + account.Actor.url + ' does not own video channel ' + videoChannel.Actor.url)
+ }
+
+ const to = overrideTo || videoObject.to
+ const videoData = await videoActivityObjectToDBAttributes(channel, videoObject, to)
+ video.name = videoData.name
+ video.uuid = videoData.uuid
+ video.url = videoData.url
+ video.category = videoData.category
+ video.licence = videoData.licence
+ video.language = videoData.language
+ video.description = videoData.description
+ video.support = videoData.support
+ video.nsfw = videoData.nsfw
+ video.commentsEnabled = videoData.commentsEnabled
+ video.downloadEnabled = videoData.downloadEnabled
+ video.waitTranscoding = videoData.waitTranscoding
+ video.state = videoData.state
+ video.duration = videoData.duration
+ video.createdAt = videoData.createdAt
+ video.publishedAt = videoData.publishedAt
+ video.originallyPublishedAt = videoData.originallyPublishedAt
+ video.privacy = videoData.privacy
+ video.channelId = videoData.channelId
+ video.views = videoData.views
+
+ const videoUpdated = await video.save(sequelizeOptions) as MVideoFullLight
+
+ if (thumbnailModel) await videoUpdated.addAndSaveThumbnail(thumbnailModel, t)
+
+ if (videoUpdated.getPreview()) {
+ const previewUrl = videoUpdated.getPreview().getFileUrl(videoUpdated)
+ const previewModel = createPlaceholderThumbnail(previewUrl, video, ThumbnailType.PREVIEW, PREVIEWS_SIZE)
+ await videoUpdated.addAndSaveThumbnail(previewModel, t)
+ }
+
+ {
+ const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoUpdated, videoObject.url)
+ const newVideoFiles = videoFileAttributes.map(a => new VideoFileModel(a))
+
+ // Remove video files that do not exist anymore
+ const destroyTasks = deleteNonExistingModels(videoUpdated.VideoFiles, newVideoFiles, t)
+ await Promise.all(destroyTasks)
+
+ // Update or add other one
+ const upsertTasks = newVideoFiles.map(f => VideoFileModel.customUpsert(f, 'video', t))
+ videoUpdated.VideoFiles = await Promise.all(upsertTasks)
+ }
+
+ {
+ const streamingPlaylistAttributes = streamingPlaylistActivityUrlToDBAttributes(videoUpdated, videoObject, videoUpdated.VideoFiles)
+ const newStreamingPlaylists = streamingPlaylistAttributes.map(a => new VideoStreamingPlaylistModel(a))
+
+ // Remove video playlists that do not exist anymore
+ const destroyTasks = deleteNonExistingModels(videoUpdated.VideoStreamingPlaylists, newStreamingPlaylists, t)
+ await Promise.all(destroyTasks)
+
+ let oldStreamingPlaylistFiles: MVideoFile[] = []
+ for (const videoStreamingPlaylist of videoUpdated.VideoStreamingPlaylists) {
+ oldStreamingPlaylistFiles = oldStreamingPlaylistFiles.concat(videoStreamingPlaylist.VideoFiles)
+ }
+
+ videoUpdated.VideoStreamingPlaylists = []
+
+ for (const playlistAttributes of streamingPlaylistAttributes) {
+ const streamingPlaylistModel = await VideoStreamingPlaylistModel.upsert(playlistAttributes, { returning: true, transaction: t })
+ .then(([ streamingPlaylist ]) => streamingPlaylist)
+
+ const newVideoFiles: MVideoFile[] = videoFileActivityUrlToDBAttributes(streamingPlaylistModel, playlistAttributes.tagAPObject)
+ .map(a => new VideoFileModel(a))
+ const destroyTasks = deleteNonExistingModels(oldStreamingPlaylistFiles, newVideoFiles, t)
+ await Promise.all(destroyTasks)
+
+ // Update or add other one
+ const upsertTasks = newVideoFiles.map(f => VideoFileModel.customUpsert(f, 'streaming-playlist', t))
+ streamingPlaylistModel.VideoFiles = await Promise.all(upsertTasks)
+
+ videoUpdated.VideoStreamingPlaylists.push(streamingPlaylistModel)
+ }
+ }
+
+ {
+ // Update Tags
+ const tags = videoObject.tag
+ .filter(isAPHashTagObject)
+ .map(tag => tag.name)
+ const tagInstances = await TagModel.findOrCreateTags(tags, t)
+ await videoUpdated.$set('Tags', tagInstances, sequelizeOptions)
+ }
+
+ {
+ // Update captions
+ await VideoCaptionModel.deleteAllCaptionsOfRemoteVideo(videoUpdated.id, t)
+
+ const videoCaptionsPromises = videoObject.subtitleLanguage.map(c => {
+ return VideoCaptionModel.insertOrReplaceLanguage(videoUpdated.id, c.identifier, c.url, t)
+ })
+ await Promise.all(videoCaptionsPromises)
+ }
+
+ return videoUpdated