]> 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 6764a403710c62137439ce1f082e433dacc0f7af..76b6fcaae94d1a7e4b879d428f8f23f0d9e7c4a5 100644 (file)
@@ -1,37 +1,42 @@
-import * as kue from 'kue'
-import { logger } from '../../../helpers/logger'
-import { getServerActor } from '../../../helpers/utils'
-import { REMOTE_SCHEME, sequelizeTypescript, SERVER_ACTOR_NAME } from '../../../initializers'
-import { sendFollow } from '../../activitypub/send'
+import * as Bull from 'bull'
+import { getLocalActorFollowActivityPubUrl } from '@server/lib/activitypub/url'
+import { ActivitypubFollowPayload } from '@shared/models'
 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'
-
-export type ActivitypubFollowPayload = {
-  host: string
-}
+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 { Notifier } from '../../notifier'
 
-async function processActivityPubFollow (job: kue.Job) {
+async function processActivityPubFollow (job: Bull.Job) {
   const payload = job.data as ActivitypubFollowPayload
   const host = payload.host
 
   logger.info('Processing ActivityPub follow in job %d.', job.id)
 
-  const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP)
-
-  const actorUrl = await loadActorUrlOrGetFromWebfinger(SERVER_ACTOR_NAME, sanitizedHost)
-  const targetActor = await getOrCreateActorAndServerAndModel(actorUrl)
+  let targetActor: MActorFull
+  if (!host || host === WEBSERVER.HOST) {
+    targetActor = await ActorModel.loadLocalByName(payload.name)
+  } else {
+    const sanitizedHost = sanitizeHost(host, REMOTE_SCHEME.HTTP)
+    const actorUrl = await loadActorUrlOrGetFromWebfinger(payload.name + '@' + sanitizedHost)
+    targetActor = await getOrCreateAPActor(actorUrl, 'all')
+  }
 
-  const fromActor = await getServerActor()
-  const options = {
-    arguments: [ fromActor, targetActor ],
-    errorMessage: 'Cannot follow with many retries.'
+  if (payload.assertIsChannel && !targetActor.VideoChannel) {
+    logger.warn('Do not follow %s@%s because it is not a channel.', payload.name, host)
+    return
   }
 
-  return retryTransactionWrapper(follow, options)
+  const fromActor = await ActorModel.load(payload.followerActorId)
+
+  return retryTransactionWrapper(follow, fromActor, targetActor, payload.isAutoFollow)
 }
 // ---------------------------------------------------------------------------
 
@@ -41,19 +46,23 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function follow (fromActor: ActorModel, targetActor: ActorModel) {
+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.')
   }
 
-  return sequelizeTypescript.transaction(async t => {
-    const [ actorFollow ] = await ActorFollowModel.findOrCreate({
+  // Same server, direct accept
+  const state = !fromActor.serverId && !targetActor.serverId ? 'accepted' : 'pending'
+
+  const actorFollow = await sequelizeTypescript.transaction(async t => {
+    const [ actorFollow ] = await ActorFollowModel.findOrCreate<MActorFollowActors>({
       where: {
         actorId: fromActor.id,
         targetActorId: targetActor.id
       },
       defaults: {
-        state: 'pending',
+        state,
+        url: getLocalActorFollowActivityPubUrl(fromActor, targetActor),
         actorId: fromActor.id,
         targetActorId: targetActor.id
       },
@@ -62,7 +71,21 @@ function follow (fromActor: ActorModel, targetActor: ActorModel) {
     actorFollow.ActorFollowing = targetActor
     actorFollow.ActorFollower = fromActor
 
-    // Send a notification to remote server
-    await sendFollow(actorFollow)
+    // Send a notification to remote server if our follow is not already accepted
+    if (actorFollow.state !== 'accepted') sendFollow(actorFollow, t)
+
+    return actorFollow
   })
+
+  const followerFull = await ActorModel.loadFull(fromActor.id)
+
+  const actorFollowFull = Object.assign(actorFollow, {
+    ActorFollowing: targetActor,
+    ActorFollower: followerFull
+  })
+
+  if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollowFull)
+  if (isAutoFollow === true) Notifier.Instance.notifyOfAutoInstanceFollowing(actorFollowFull)
+
+  return actorFollow
 }