]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/job-queue/handlers/activitypub-follow.ts
Refactor AP actors
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / activitypub-follow.ts
index af7c8a8383afe42bcdcadb7e6e56242f1dcb745b..76b6fcaae94d1a7e4b879d428f8f23f0d9e7c4a5 100644 (file)
@@ -1,23 +1,18 @@
 import * as Bull from 'bull'
+import { getLocalActorFollowActivityPubUrl } from '@server/lib/activitypub/url'
+import { ActivitypubFollowPayload } from '@shared/models'
+import { sanitizeHost } from '../../../helpers/core-utils'
+import { retryTransactionWrapper } from '../../../helpers/database-utils'
 import { logger } from '../../../helpers/logger'
+import { loadActorUrlOrGetFromWebfinger } from '../../../helpers/webfinger'
 import { REMOTE_SCHEME, WEBSERVER } from '../../../initializers/constants'
+import { sequelizeTypescript } from '../../../initializers/database'
+import { ActorModel } from '../../../models/actor/actor'
+import { ActorFollowModel } from '../../../models/actor/actor-follow'
+import { MActor, MActorFollowActors, MActorFull } from '../../../types/models'
+import { getOrCreateAPActor } from '../../activitypub/actors'
 import { sendFollow } from '../../activitypub/send'
-import { sanitizeHost } from '../../../helpers/core-utils'
-import { loadActorUrlOrGetFromWebfinger } from '../../../helpers/webfinger'
-import { getOrCreateActorAndServerAndModel } from '../../activitypub/actor'
-import { retryTransactionWrapper } from '../../../helpers/database-utils'
-import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
-import { ActorModel } from '../../../models/activitypub/actor'
 import { Notifier } from '../../notifier'
-import { sequelizeTypescript } from '../../../initializers/database'
-import { MActor, MActorFollowActors, MActorFull } from '../../../typings/models'
-
-export type ActivitypubFollowPayload = {
-  followerActorId: number
-  name: string
-  host: string
-  isAutoFollow?: boolean
-}
 
 async function processActivityPubFollow (job: Bull.Job) {
   const payload = job.data as ActivitypubFollowPayload
@@ -31,7 +26,12 @@ async function processActivityPubFollow (job: Bull.Job) {
   } else {
     const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP)
     const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost)
-    targetActor = await getOrCreateActorAndServerAndModel(actorUrl, 'all')
+    targetActor = await getOrCreateAPActor(actorUrl, 'all')
+  }
+
+  if (payload.assertIsChannel && !targetActor.VideoChannel) {
+    logger.warn('Do not follow %s@%s because it is not a channel.', payload.name, host)
+    return
   }
 
   const fromActor = await ActorModel.load(payload.followerActorId)
@@ -48,7 +48,7 @@ export {
 
 async function follow (fromActor: MActor, targetActor: MActorFull, isAutoFollow = false) {
   if (fromActor.id === targetActor.id) {
-    throw new Error('Follower is the same than target actor.')
+    throw new Error('Follower is the same as target actor.')
   }
 
   // Same server, direct accept
@@ -62,6 +62,7 @@ async function follow (fromActor: MActor, targetActor: MActorFull, isAutoFollow
       },
       defaults: {
         state,
+        url: getLocalActorFollowActivityPubUrl(fromActor, targetActor),
         actorId: fromActor.id,
         targetActorId: targetActor.id
       },