]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/actor.ts
Fix broken local actors
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / actor.ts
index fb5558ff6b7bbc605f891f7fea4db9c8bb7754bc..a726f9e209d0093f8510bec3cad4cd1ecbb1f2ae 100644 (file)
@@ -36,19 +36,16 @@ import {
 } from '../../types/models'
 import { extname } from 'path'
 import { getServerActor } from '@server/models/application/application'
+import { HttpStatusCode } from '../../../shared/core-utils/miscs/http-error-codes'
 
 // Set account keys, this could be long so process after the account creation and do not block the client
-function setAsyncActorKeys <T extends MActor> (actor: T) {
-  return createPrivateAndPublicKeys()
-    .then(({ publicKey, privateKey }) => {
-      actor.publicKey = publicKey
-      actor.privateKey = privateKey
-      return actor.save()
-    })
-    .catch(err => {
-      logger.error('Cannot set public/private keys of actor %d.', actor.url, { err })
-      return actor
-    })
+async function generateAndSaveActorKeys <T extends MActor> (actor: T) {
+  const { publicKey, privateKey } = await createPrivateAndPublicKeys()
+
+  actor.publicKey = publicKey
+  actor.privateKey = privateKey
+
+  return actor.save()
 }
 
 function getOrCreateActorAndServerAndModel (
@@ -103,7 +100,7 @@ async function getOrCreateActorAndServerAndModel (
         const recurseIfNeeded = false
         ownerActor = await getOrCreateActorAndServerAndModel(accountAttributedTo.id, 'all', recurseIfNeeded)
       } catch (err) {
-        logger.error('Cannot get or create account attributed to video channel ' + actor.url)
+        logger.error('Cannot get or create account attributed to video channel ' + actorUrl)
         throw new Error(err)
       }
     }
@@ -198,6 +195,19 @@ async function updateActorAvatarInstance (actor: MActorDefault, info: AvatarInfo
   return actor
 }
 
+async function deleteActorAvatarInstance (actor: MActorDefault, t: Transaction) {
+  try {
+    await actor.Avatar.destroy({ transaction: t })
+  } catch (err) {
+    logger.error('Cannot remove old avatar of actor %s.', actor.url, { err })
+  }
+
+  actor.avatarId = null
+  actor.Avatar = null
+
+  return actor
+}
+
 async function fetchActorTotalItems (url: string) {
   const options = {
     uri: url,
@@ -277,7 +287,7 @@ async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannel
 
     const { result, statusCode } = await fetchRemoteActor(actorUrl)
 
-    if (statusCode === 404) {
+    if (statusCode === HttpStatusCode.NOT_FOUND_404) {
       logger.info('Deleting actor %s because there is a 404 in refresh actor.', actor.url)
       actor.Account
         ? await actor.Account.destroy()
@@ -332,10 +342,11 @@ async function refreshActorIfNeeded <T extends MActorFull | MActorAccountChannel
 export {
   getOrCreateActorAndServerAndModel,
   buildActorInstance,
-  setAsyncActorKeys,
+  generateAndSaveActorKeys,
   fetchActorTotalItems,
   getAvatarInfoIfExists,
   updateActorInstance,
+  deleteActorAvatarInstance,
   refreshActorIfNeeded,
   updateActorAvatarInstance,
   addFetchOutboxJob