]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/actor.ts
Generate a name for thumbnails
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / actor.ts
index 8132ac135ca8e374506c3086b590bbe18e4bb0d2..dbb243d3af6c3c231335116f8f1d33ae3f9824b9 100644 (file)
@@ -1,5 +1,5 @@
 import * as Bluebird from 'bluebird'
-import { Transaction } from 'sequelize'
+import { Op, Transaction } from 'sequelize'
 import { URL } from 'url'
 import { v4 as uuidv4 } from 'uuid'
 import { ActivityPubActor, ActivityPubActorType, ActivityPubOrderedCollection } from '../../../shared/models/activitypub'
@@ -19,7 +19,6 @@ import { AvatarModel } from '../../models/avatar/avatar'
 import { ServerModel } from '../../models/server/server'
 import { VideoChannelModel } from '../../models/video/video-channel'
 import { JobQueue } from '../job-queue'
-import { getServerActor } from '../../helpers/utils'
 import { ActorFetchByUrlType, fetchActorByUrl } from '../../helpers/actor'
 import { sequelizeTypescript } from '../../initializers/database'
 import {
@@ -34,8 +33,10 @@ import {
   MActorFullActor,
   MActorId,
   MChannel
-} from '../../typings/models'
+} 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) {
@@ -103,7 +104,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)
       }
     }
@@ -166,7 +167,7 @@ async function updateActorInstance (actorInstance: ActorModel, attributes: Activ
   actorInstance.followersUrl = attributes.followers
   actorInstance.followingUrl = attributes.following
 
-  if (attributes.endpoints && attributes.endpoints.sharedInbox) {
+  if (attributes.endpoints?.sharedInbox) {
     actorInstance.sharedInboxUrl = attributes.endpoints.sharedInbox
   }
 }
@@ -198,6 +199,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 +291,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()
@@ -336,6 +350,7 @@ export {
   fetchActorTotalItems,
   getAvatarInfoIfExists,
   updateActorInstance,
+  deleteActorAvatarInstance,
   refreshActorIfNeeded,
   updateActorAvatarInstance,
   addFetchOutboxJob
@@ -384,14 +399,33 @@ function saveActorAndServerAndModelIfNotExist (
 
     // Force the actor creation, sometimes Sequelize skips the save() when it thinks the instance already exists
     // (which could be false in a retried query)
-    const [ actorCreated ] = await ActorModel.findOrCreate<MActorFullActor>({
+    const [ actorCreated, created ] = await ActorModel.findOrCreate<MActorFullActor>({
       defaults: actor.toJSON(),
       where: {
-        url: actor.url
+        [Op.or]: [
+          {
+            url: actor.url
+          },
+          {
+            serverId: actor.serverId,
+            preferredUsername: actor.preferredUsername
+          }
+        ]
       },
       transaction: t
     })
 
+    // Try to fix non HTTPS accounts of remote instances that fixed their URL afterwards
+    if (created !== true && actorCreated.url !== actor.url) {
+      // Only fix http://example.com/account/djidane to https://example.com/account/djidane
+      if (actorCreated.url.replace(/^http:\/\//, '') !== actor.url.replace(/^https:\/\//, '')) {
+        throw new Error(`Actor from DB with URL ${actorCreated.url} does not correspond to actor ${actor.url}`)
+      }
+
+      actorCreated.url = actor.url
+      await actorCreated.save({ transaction: t })
+    }
+
     if (actorCreated.type === 'Person' || actorCreated.type === 'Application') {
       actorCreated.Account = await saveAccount(actorCreated, result, t) as MAccountDefault
       actorCreated.Account.Actor = actorCreated
@@ -457,7 +491,7 @@ async function fetchRemoteActor (actorUrl: string): Promise<{ statusCode?: numbe
     followersUrl: actorJSON.followers,
     followingUrl: actorJSON.following,
 
-    sharedInboxUrl: actorJSON.endpoints && actorJSON.endpoints.sharedInbox
+    sharedInboxUrl: actorJSON.endpoints?.sharedInbox
       ? actorJSON.endpoints.sharedInbox
       : null
   })