]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/actor.ts
Try to fix non HTTPS remote accounts
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / actor.ts
index 0e5742071e3fd35b8965568a2aefca8bc8e7d1d3..63975021c920fc53cf02db83cde4dbce2cb0308b 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'
@@ -33,7 +33,7 @@ import {
   MActorFullActor,
   MActorId,
   MChannel
-} from '../../typings/models'
+} from '../../types/models'
 import { extname } from 'path'
 import { getServerActor } from '@server/models/application/application'
 
@@ -384,14 +384,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