]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-update.ts
Remove unnecessary actor existance check
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-update.ts
index 07a5ff92f1227936fb848e40d59c7461646b8d86..ed3489ebfe0ab52b25f9ce6bdc9a2c0a2853b2af 100644 (file)
@@ -1,4 +1,4 @@
-import { ActivityUpdate, VideoTorrentObject } from '../../../../shared/models/activitypub'
+import { ActivityUpdate, CacheFileObject, VideoTorrentObject } from '../../../../shared/models/activitypub'
 import { ActivityPubActor } from '../../../../shared/models/activitypub/activitypub-actor'
 import { resetSequelizeInstance, retryTransactionWrapper } from '../../../helpers/database-utils'
 import { logger } from '../../../helpers/logger'
@@ -6,18 +6,30 @@ import { sequelizeTypescript } from '../../../initializers'
 import { AccountModel } from '../../../models/account/account'
 import { ActorModel } from '../../../models/activitypub/actor'
 import { VideoChannelModel } from '../../../models/video/video-channel'
-import { fetchAvatarIfExists, getOrCreateActorAndServerAndModel, updateActorAvatarInstance, updateActorInstance } from '../actor'
-import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannel, updateVideoFromAP } from '../videos'
+import { fetchAvatarIfExists, updateActorAvatarInstance, updateActorInstance } from '../actor'
+import { getOrCreateVideoAndAccountAndChannel, getOrCreateVideoChannelFromVideoObject, updateVideoFromAP } from '../videos'
 import { sanitizeAndCheckVideoTorrentObject } from '../../../helpers/custom-validators/activitypub/videos'
+import { isCacheFileObjectValid } from '../../../helpers/custom-validators/activitypub/cache-file'
+import { VideoRedundancyModel } from '../../../models/redundancy/video-redundancy'
+import { createCacheFile, updateCacheFile } from '../cache-file'
 
-async function processUpdateActivity (activity: ActivityUpdate) {
-  const actor = await getOrCreateActorAndServerAndModel(activity.actor)
+async function processUpdateActivity (activity: ActivityUpdate, byActor: ActorModel) {
   const objectType = activity.object.type
 
   if (objectType === 'Video') {
-    return retryTransactionWrapper(processUpdateVideo, actor, activity)
-  } else if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
-    return retryTransactionWrapper(processUpdateActor, actor, activity)
+    return retryTransactionWrapper(processUpdateVideo, byActor, activity)
+  }
+
+  if (objectType === 'Person' || objectType === 'Application' || objectType === 'Group') {
+    // We need more attributes
+    const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
+    return retryTransactionWrapper(processUpdateActor, byActorFull, activity)
+  }
+
+  if (objectType === 'CacheFile') {
+    // We need more attributes
+    const byActorFull = await ActorModel.loadByUrlAndPopulateAccountAndChannel(byActor.url)
+    return retryTransactionWrapper(processUpdateCacheFile, byActorFull, activity)
   }
 
   return undefined
@@ -39,10 +51,35 @@ async function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate)
     return undefined
   }
 
-  const { video } = await getOrCreateVideoAndAccountAndChannel(videoObject.id)
-  const channelActor = await getOrCreateVideoChannel(videoObject)
+  const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: videoObject.id })
+  const channelActor = await getOrCreateVideoChannelFromVideoObject(videoObject)
+
+  const updateOptions = {
+    video,
+    videoObject,
+    account: actor.Account,
+    channel: channelActor.VideoChannel,
+    updateViews: true,
+    overrideTo: activity.to
+  }
+  return updateVideoFromAP(updateOptions)
+}
+
+async function processUpdateCacheFile (byActor: ActorModel, activity: ActivityUpdate) {
+  const cacheFileObject = activity.object as CacheFileObject
+
+  if (!isCacheFileObjectValid(cacheFileObject) === false) {
+    logger.debug('Cahe file object sent by update is not valid.', { cacheFileObject })
+    return undefined
+  }
+
+  const redundancyModel = await VideoRedundancyModel.loadByUrl(cacheFileObject.id)
+  if (!redundancyModel) {
+    const { video } = await getOrCreateVideoAndAccountAndChannel({ videoObject: cacheFileObject.id })
+    return createCacheFile(cacheFileObject, video, byActor)
+  }
 
-  return updateVideoFromAP(video, videoObject, actor, channelActor, activity.to)
+  return updateCacheFile(cacheFileObject, redundancyModel, byActor)
 }
 
 async function processUpdateActor (actor: ActorModel, activity: ActivityUpdate) {