]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-update.ts
Flat shared module directory
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-update.ts
index 77de8c1554352e3f40d92738113b834260d14918..82b661a0329dc6cc24802e1ed8a72630a5abe708 100644 (file)
@@ -1,5 +1,5 @@
 import * as Bluebird from 'bluebird'
-import { ActivityUpdate } from '../../../../shared/models/activitypub'
+import { ActivityUpdate, VideoTorrentObject } from '../../../../shared/models/activitypub'
 import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
 import { retryTransactionWrapper } from '../../../helpers/database-utils'
 import { logger } from '../../../helpers/logger'
@@ -12,25 +12,26 @@ import { VideoChannelModel } from '../../../models/video/video-channel'
 import { VideoFileModel } from '../../../models/video/video-file'
 import { fetchAvatarIfExists, getOrCreateActorAndServerAndModel, updateActorAvatarInstance, updateActorInstance } from '../actor'
 import {
-  fetchRemoteVideo,
   generateThumbnailFromUrl,
   getOrCreateAccountAndVideoAndChannel,
   getOrCreateVideoChannel,
   videoActivityObjectToDBAttributes,
   videoFileActivityUrlToDBAttributes
 } from '../videos'
+import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
+import { VideoCaptionModel } from '../../../models/video/video-caption'
 
 async function processUpdateActivity (activity: ActivityUpdate) {
   const actor = await getOrCreateActorAndServerAndModel(activity.actor)
   const objectType = activity.object.type
 
   if (objectType === 'Video') {
-    return processUpdateVideo(actor, activity)
+    return retryTransactionWrapper(processUpdateVideo, actor, activity)
   } else if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
-    return processUpdateActor(actor, activity)
+    return retryTransactionWrapper(processUpdateActor, actor, activity)
   }
 
-  return
+  return undefined
 }
 
 // ---------------------------------------------------------------------------
@@ -41,20 +42,13 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) {
-  const options = {
-    arguments: [ actor, activity ],
-    errorMessage: 'Cannot update the remote video with many retries'
-  }
-
-  return retryTransactionWrapper(updateRemoteVideo, options)
-}
+async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) {
+  const videoObject = activity.object as VideoTorrentObject
 
-async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) {
-  const videoUrl = activity.object.id
-
-  const videoObject = await fetchRemoteVideo(videoUrl)
-  if (!videoObject) throw new Error('Cannot fetch remote video with url: ' + videoUrl)
+  if (sanitizeAndCheckVideoTorrentObject(videoObject) === false) {
+    logger.debug('Video sent by update is not valid.', { videoObject })
+    return undefined
+  }
 
   const res = await getOrCreateAccountAndVideoAndChannel(videoObject.id)
 
@@ -114,12 +108,21 @@ async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) {
       await Promise.all(videoFileDestroyTasks)
 
       const videoFileAttributes = videoFileActivityUrlToDBAttributes(videoInstance, videoObject)
-      const tasks = videoFileAttributes.map(f => VideoFileModel.create(f))
+      const tasks = videoFileAttributes.map(f => VideoFileModel.create(f, sequelizeOptions))
       await Promise.all(tasks)
 
-      const tags = videoObject.tag.map(t => t.name)
+      // Update Tags
+      const tags = videoObject.tag.map(tag => tag.name)
       const tagInstances = await TagModel.findOrCreateTags(tags, t)
       await videoInstance.$set('Tags', tagInstances, sequelizeOptions)
+
+      // Update captions
+      await VideoCaptionModel.deleteAllCaptionsOfRemoteVideo(videoInstance.id, t)
+
+      const videoCaptionsPromises = videoObject.subtitleLanguage.map(c => {
+        return VideoCaptionModel.insertOrReplaceLanguage(videoInstance.id, c.identifier, t)
+      })
+      await Promise.all(videoCaptionsPromises)
     })
 
     logger.info('Remote video with uuid %s updated', videoObject.uuid)
@@ -134,16 +137,7 @@ async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) {
   }
 }
 
-function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
-  const options = {
-    arguments: [ actor, activity ],
-    errorMessage: 'Cannot update the remote actor with many retries'
-  }
-
-  return retryTransactionWrapper(updateRemoteActor, options)
-}
-
-async function updateRemoteActor (actor: ActorModel, activity: ActivityUpdate) {
+async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {
   const actorAttributesToUpdate = activity.object as ActivityPubActor
 
   logger.debug('Updating remote account "%s".', actorAttributesToUpdate.uuid)