]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/videos.ts
Check video channel name is unique on our instance
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / videos.ts
index 91231a187f386e6a04e7e429ecca2ba4d99340bb..b1e7f20b9686a4618086d106f04e1cdd77c020d3 100644 (file)
@@ -176,7 +176,7 @@ async function getOrCreateVideoAndAccountAndChannel (options: {
       syncParam,
       refreshViews
     }
-    const p = retryTransactionWrapper(refreshVideoIfNeeded, refreshOptions)
+    const p = refreshVideoIfNeeded(refreshOptions)
     if (syncParam.refreshVideo === true) videoFromDatabase = await p
 
     return { video: videoFromDatabase }
@@ -245,29 +245,37 @@ async function updateVideoFromAP (options: {
       generateThumbnailFromUrl(options.video, options.videoObject.icon)
         .catch(err => logger.warn('Cannot generate thumbnail of %s.', options.videoObject.id, { err }))
 
-      // Remove old video files
-      const videoFileDestroyTasks: Bluebird<void>[] = []
-      for (const videoFile of options.video.VideoFiles) {
-        videoFileDestroyTasks.push(videoFile.destroy(sequelizeOptions))
-      }
-      await Promise.all(videoFileDestroyTasks)
+      {
+        const videoFileAttributes = videoFileActivityUrlToDBAttributes(options.video, options.videoObject)
+        const newVideoFiles = videoFileAttributes.map(a => new VideoFileModel(a))
 
-      const videoFileAttributes = videoFileActivityUrlToDBAttributes(options.video, options.videoObject)
-      const tasks = videoFileAttributes.map(f => VideoFileModel.create(f, sequelizeOptions))
-      await Promise.all(tasks)
+        // Remove video files that do not exist anymore
+        const destroyTasks = options.video.VideoFiles
+                                    .filter(f => !newVideoFiles.find(newFile => newFile.hasSameUniqueKeysThan(f)))
+                                    .map(f => f.destroy(sequelizeOptions))
+        await Promise.all(destroyTasks)
 
-      // Update Tags
-      const tags = options.videoObject.tag.map(tag => tag.name)
-      const tagInstances = await TagModel.findOrCreateTags(tags, t)
-      await options.video.$set('Tags', tagInstances, sequelizeOptions)
+        // Update or add other one
+        const upsertTasks = videoFileAttributes.map(a => VideoFileModel.upsert(a, sequelizeOptions))
+        await Promise.all(upsertTasks)
+      }
 
-      // Update captions
-      await VideoCaptionModel.deleteAllCaptionsOfRemoteVideo(options.video.id, t)
+      {
+        // Update Tags
+        const tags = options.videoObject.tag.map(tag => tag.name)
+        const tagInstances = await TagModel.findOrCreateTags(tags, t)
+        await options.video.$set('Tags', tagInstances, sequelizeOptions)
+      }
 
-      const videoCaptionsPromises = options.videoObject.subtitleLanguage.map(c => {
-        return VideoCaptionModel.insertOrReplaceLanguage(options.video.id, c.identifier, t)
-      })
-      await Promise.all(videoCaptionsPromises)
+      {
+        // Update captions
+        await VideoCaptionModel.deleteAllCaptionsOfRemoteVideo(options.video.id, t)
+
+        const videoCaptionsPromises = options.videoObject.subtitleLanguage.map(c => {
+          return VideoCaptionModel.insertOrReplaceLanguage(options.video.id, c.identifier, t)
+        })
+        await Promise.all(videoCaptionsPromises)
+      }
     })
 
     logger.info('Remote video with uuid %s updated', options.videoObject.uuid)
@@ -354,21 +362,23 @@ async function refreshVideoIfNeeded (options: {
   syncParam: SyncParam,
   refreshViews: boolean
 }): Promise<VideoModel> {
+  if (!options.video.isOutdated()) return options.video
+
   // We need more attributes if the argument video was fetched with not enough joints
   const video = options.fetchedType === 'all' ? options.video : await VideoModel.loadByUrlAndPopulateAccount(options.video.url)
 
-  if (!video.isOutdated()) return video
-
   try {
     const { response, videoObject } = await fetchRemoteVideo(video.url)
     if (response.statusCode === 404) {
+      logger.info('Cannot refresh remote video %s: video does not exist anymore. Deleting it.', video.url)
+
       // Video does not exist anymore
       await video.destroy()
       return undefined
     }
 
     if (videoObject === undefined) {
-      logger.warn('Cannot refresh remote video: invalid body.')
+      logger.warn('Cannot refresh remote video %s: invalid body.', video.url)
       return video
     }
 
@@ -382,8 +392,10 @@ async function refreshVideoIfNeeded (options: {
       channel: channelActor.VideoChannel,
       updateViews: options.refreshViews
     }
-    await updateVideoFromAP(updateOptions)
-    await syncVideoExternalAttributes(video, videoObject, options.syncParam)
+    const videoUpdated = await retryTransactionWrapper(updateVideoFromAP, updateOptions)
+    await syncVideoExternalAttributes(videoUpdated, videoObject, options.syncParam)
+
+    return videoUpdated
   } catch (err) {
     logger.warn('Cannot refresh video.', { err })
     return video
@@ -470,7 +482,7 @@ function videoFileActivityUrlToDBAttributes (videoCreated: VideoModel, videoObje
       resolution: fileUrl.height,
       size: fileUrl.size,
       videoId: videoCreated.id,
-      fps: fileUrl.fps
+      fps: fileUrl.fps || -1
     } as VideoFileModel
     attributes.push(attribute)
   }