]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/activitypub/actors/get.ts
Fix schema.org context
[github/Chocobozzz/PeerTube.git] / server / lib / activitypub / actors / get.ts
index e7e87a96740451c77365ce734f30efb1fee3bd01..d2b651082478f19f7817c3e27422294596fead17 100644 (file)
@@ -1,11 +1,11 @@
-
-import { checkUrlsSameHost, getAPId } from '@server/helpers/activitypub'
 import { retryTransactionWrapper } from '@server/helpers/database-utils'
 import { logger } from '@server/helpers/logger'
 import { JobQueue } from '@server/lib/job-queue'
-import { ActorFetchByUrlType, fetchActorByUrl } from '@server/lib/model-loaders'
+import { ActorLoadByUrlType, loadActorByUrl } from '@server/lib/model-loaders'
 import { MActor, MActorAccountChannelId, MActorAccountChannelIdActor, MActorAccountId, MActorFullActor } from '@server/types/models'
 import { ActivityPubActor } from '@shared/models'
+import { getAPId } from '../activity'
+import { checkUrlsSameHost } from '../url'
 import { refreshActorIfNeeded } from './refresh'
 import { APActorCreator, fetchRemoteActor } from './shared'
 
@@ -25,7 +25,7 @@ function getOrCreateAPActor (
 
 async function getOrCreateAPActor (
   activityActor: string | ActivityPubActor,
-  fetchType: ActorFetchByUrlType = 'association-ids',
+  fetchType: ActorLoadByUrlType = 'association-ids',
   recurseIfNeeded = true,
   updateCollections = false
 ): Promise<MActorFullActor | MActorAccountChannelId> {
@@ -40,6 +40,9 @@ async function getOrCreateAPActor (
     const { actorObject } = await fetchRemoteActor(actorUrl)
     if (actorObject === undefined) throw new Error('Cannot fetch remote actor ' + actorUrl)
 
+    // actorUrl is just an alias/rediraction, so process object id instead
+    if (actorObject.id !== actorUrl) return getOrCreateAPActor(actorObject, 'all', recurseIfNeeded, updateCollections)
+
     // Create the attributed to actor
     // In PeerTube a video channel is owned by an account
     let ownerActor: MActorFullActor
@@ -56,7 +59,7 @@ async function getOrCreateAPActor (
   if (actor.Account) (actor as MActorAccountChannelIdActor).Account.Actor = actor
   if (actor.VideoChannel) (actor as MActorAccountChannelIdActor).VideoChannel.Actor = actor
 
-  const { actor: actorRefreshed, refreshed } = await retryTransactionWrapper(refreshActorIfNeeded, actor, fetchType)
+  const { actor: actorRefreshed, refreshed } = await refreshActorIfNeeded({ actor, fetchedType: fetchType })
   if (!actorRefreshed) throw new Error('Actor ' + actor.url + ' does not exist anymore.')
 
   await scheduleOutboxFetchIfNeeded(actor, created, refreshed, updateCollections)
@@ -65,26 +68,6 @@ async function getOrCreateAPActor (
   return actorRefreshed
 }
 
-// ---------------------------------------------------------------------------
-
-export {
-  getOrCreateAPActor
-}
-
-// ---------------------------------------------------------------------------
-
-async function loadActorFromDB (actorUrl: string, fetchType: ActorFetchByUrlType) {
-  let actor = await fetchActorByUrl(actorUrl, fetchType)
-
-  // Orphan actor (not associated to an account of channel) so recreate it
-  if (actor && (!actor.Account && !actor.VideoChannel)) {
-    await actor.destroy()
-    actor = null
-  }
-
-  return actor
-}
-
 function getOrCreateAPOwner (actorObject: ActivityPubActor, actorUrl: string) {
   const accountAttributedTo = actorObject.attributedTo.find(a => a.type === 'Person')
   if (!accountAttributedTo) throw new Error('Cannot find account attributed to video channel ' + actorUrl)
@@ -103,6 +86,27 @@ function getOrCreateAPOwner (actorObject: ActivityPubActor, actorUrl: string) {
   }
 }
 
+// ---------------------------------------------------------------------------
+
+export {
+  getOrCreateAPOwner,
+  getOrCreateAPActor
+}
+
+// ---------------------------------------------------------------------------
+
+async function loadActorFromDB (actorUrl: string, fetchType: ActorLoadByUrlType) {
+  let actor = await loadActorByUrl(actorUrl, fetchType)
+
+  // Orphan actor (not associated to an account of channel) so recreate it
+  if (actor && (!actor.Account && !actor.VideoChannel)) {
+    await actor.destroy()
+    actor = null
+  }
+
+  return actor
+}
+
 async function scheduleOutboxFetchIfNeeded (actor: MActor, created: boolean, refreshed: boolean, updateCollections: boolean) {
   if ((created === true || refreshed === true) && updateCollections === true) {
     const payload = { uri: actor.outboxUrl, type: 'activity' as 'activity' }
@@ -113,7 +117,7 @@ async function scheduleOutboxFetchIfNeeded (actor: MActor, created: boolean, ref
 async function schedulePlaylistFetchIfNeeded (actor: MActorAccountId, created: boolean, accountPlaylistsUrl: string) {
   // We created a new account: fetch the playlists
   if (created === true && actor.Account && accountPlaylistsUrl) {
-    const payload = { uri: accountPlaylistsUrl, accountId: actor.Account.id, type: 'account-playlists' as 'account-playlists' }
+    const payload = { uri: accountPlaylistsUrl, type: 'account-playlists' as 'account-playlists' }
     await JobQueue.Instance.createJobWithPromise({ type: 'activitypub-http-fetcher', payload })
   }
 }