]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/process/process-update.ts
Fetch remote AP objects
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / process / process-update.ts
index 05ea7d2728f46e26853ca1a12b235579cc3e6e81..a5431c76b93f7904c9932337a14ffa2933db47e1 100644 (file)
@@ -8,12 +8,10 @@ import { resetSequelizeInstance } from '../../../helpers/utils'
 import { sequelizeTypescript } from '../../../initializers'
 import { AccountModel } from '../../../models/account/account'
 import { ActorModel } from '../../../models/activitypub/actor'
-import { AvatarModel } from '../../../models/avatar/avatar'
 import { TagModel } from '../../../models/video/tag'
-import { VideoModel } from '../../../models/video/video'
 import { VideoFileModel } from '../../../models/video/video-file'
-import { fetchActorTotalItems, fetchAvatarIfExists, getOrCreateActorAndServerAndModel } from '../actor'
-import { videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from './misc'
+import { fetchAvatarIfExists, getOrCreateActorAndServerAndModel, updateActorAvatarInstance, updateActorInstance } from '../actor'
+import { getOrCreateAccountAndVideoAndChannel, videoActivityObjectToDBAttributes, videoFileActivityUrlToDBAttributes } from '../videos'
 
 async function processUpdateActivity (activity: ActivityUpdate) {
   const actor = await getOrCreateActorAndServerAndModel(activity.actor)
@@ -47,8 +45,10 @@ function processUpdateVideo (actor: ActorModel, activity: ActivityUpdate) {
 async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) {
   const videoAttributesToUpdate = activity.object as VideoTorrentObject
 
+  const res = await getOrCreateAccountAndVideoAndChannel(videoAttributesToUpdate.id)
+
   logger.debug('Updating remote video "%s".', videoAttributesToUpdate.uuid)
-  let videoInstance: VideoModel
+  let videoInstance = res.video
   let videoFieldsSave: any
 
   try {
@@ -57,9 +57,6 @@ async function updateRemoteVideo (actor: ActorModel, activity: ActivityUpdate) {
         transaction: t
       }
 
-      const videoInstance = await VideoModel.loadByUrlAndPopulateAccount(videoAttributesToUpdate.id, t)
-      if (!videoInstance) throw new Error('Video ' + videoAttributesToUpdate.id + ' not found.')
-
       videoFieldsSave = videoInstance.toJSON()
 
       const videoChannel = videoInstance.VideoChannel
@@ -124,7 +121,6 @@ async function updateRemoteAccount (actor: ActorModel, activity: ActivityUpdate)
   const accountAttributesToUpdate = activity.object as ActivityPubActor
 
   logger.debug('Updating remote account "%s".', accountAttributesToUpdate.uuid)
-  let actorInstance: ActorModel
   let accountInstance: AccountModel
   let actorFieldsSave: object
   let accountFieldsSave: object
@@ -134,39 +130,14 @@ async function updateRemoteAccount (actor: ActorModel, activity: ActivityUpdate)
 
   try {
     await sequelizeTypescript.transaction(async t => {
-      actorInstance = await ActorModel.loadByUrl(accountAttributesToUpdate.id, t)
-      if (!actorInstance) throw new Error('Actor ' + accountAttributesToUpdate.id + ' not found.')
-
-      actorFieldsSave = actorInstance.toJSON()
-      accountInstance = actorInstance.Account
-      accountFieldsSave = actorInstance.Account.toJSON()
-
-      const followersCount = await fetchActorTotalItems(accountAttributesToUpdate.followers)
-      const followingCount = await fetchActorTotalItems(accountAttributesToUpdate.following)
-
-      actorInstance.set('type', accountAttributesToUpdate.type)
-      actorInstance.set('uuid', accountAttributesToUpdate.uuid)
-      actorInstance.set('preferredUsername', accountAttributesToUpdate.preferredUsername)
-      actorInstance.set('url', accountAttributesToUpdate.id)
-      actorInstance.set('publicKey', accountAttributesToUpdate.publicKey.publicKeyPem)
-      actorInstance.set('followersCount', followersCount)
-      actorInstance.set('followingCount', followingCount)
-      actorInstance.set('inboxUrl', accountAttributesToUpdate.inbox)
-      actorInstance.set('outboxUrl', accountAttributesToUpdate.outbox)
-      actorInstance.set('sharedInboxUrl', accountAttributesToUpdate.endpoints.sharedInbox)
-      actorInstance.set('followersUrl', accountAttributesToUpdate.followers)
-      actorInstance.set('followingUrl', accountAttributesToUpdate.following)
+      actorFieldsSave = actor.toJSON()
+      accountInstance = actor.Account
+      accountFieldsSave = actor.Account.toJSON()
 
-      if (avatarName !== undefined) {
-        if (actorInstance.avatarId) {
-          await actorInstance.Avatar.destroy({ transaction: t })
-        }
+      await updateActorInstance(actor, accountAttributesToUpdate)
 
-        const avatar = await AvatarModel.create({
-          filename: avatarName
-        }, { transaction: t })
-
-        actor.set('avatarId', avatar.id)
+      if (avatarName !== undefined) {
+        await updateActorAvatarInstance(actor, avatarName, t)
       }
 
       await actor.save({ transaction: t })
@@ -177,8 +148,8 @@ async function updateRemoteAccount (actor: ActorModel, activity: ActivityUpdate)
 
     logger.info('Remote account with uuid %s updated', accountAttributesToUpdate.uuid)
   } catch (err) {
-    if (actorInstance !== undefined && actorFieldsSave !== undefined) {
-      resetSequelizeInstance(actorInstance, actorFieldsSave)
+    if (actor !== undefined && actorFieldsSave !== undefined) {
+      resetSequelizeInstance(actor, actorFieldsSave)
     }
 
     if (accountInstance !== undefined && accountFieldsSave !== undefined) {