]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/job-queue/handlers/activitypub-follow.ts
Move config in its own file
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / activitypub-follow.ts
index 6764a403710c62137439ce1f082e433dacc0f7af..7c84aa93718b3f15a73af6f1d475e12726c2dc67 100644 (file)
@@ -1,7 +1,6 @@
-import * as kue from 'kue'
+import * as Bull from 'bull'
 import { logger } from '../../../helpers/logger'
-import { getServerActor } from '../../../helpers/utils'
-import { REMOTE_SCHEME, sequelizeTypescript, SERVER_ACTOR_NAME } from '../../../initializers'
+import { REMOTE_SCHEME, sequelizeTypescript, WEBSERVER } from '../../../initializers'
 import { sendFollow } from '../../activitypub/send'
 import { sanitizeHost } from '../../../helpers/core-utils'
 import { loadActorUrlOrGetFromWebfinger } from '../../../helpers/webfinger'
@@ -9,29 +8,32 @@ 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'
 
 export type ActivitypubFollowPayload = {
+  followerActorId: number
+  name: string
   host: string
 }
 
-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)
-
-  const fromActor = await getServerActor()
-  const options = {
-    arguments: [ fromActor, targetActor ],
-    errorMessage: 'Cannot follow with many retries.'
+  let targetActor: ActorModel
+  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 getOrCreateActorAndServerAndModel(actorUrl)
   }
 
-  return retryTransactionWrapper(follow, options)
+  const fromActor = await ActorModel.load(payload.followerActorId)
+
+  return retryTransactionWrapper(follow, fromActor, targetActor)
 }
 // ---------------------------------------------------------------------------
 
@@ -41,19 +43,22 @@ export {
 
 // ---------------------------------------------------------------------------
 
-function follow (fromActor: ActorModel, targetActor: ActorModel) {
+async function follow (fromActor: ActorModel, targetActor: ActorModel) {
   if (fromActor.id === targetActor.id) {
     throw new Error('Follower is the same than target actor.')
   }
 
-  return sequelizeTypescript.transaction(async t => {
+  // Same server, direct accept
+  const state = !fromActor.serverId && !targetActor.serverId ? 'accepted' : 'pending'
+
+  const actorFollow = await sequelizeTypescript.transaction(async t => {
     const [ actorFollow ] = await ActorFollowModel.findOrCreate({
       where: {
         actorId: fromActor.id,
         targetActorId: targetActor.id
       },
       defaults: {
-        state: 'pending',
+        state,
         actorId: fromActor.id,
         targetActorId: targetActor.id
       },
@@ -62,7 +67,11 @@ 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') await sendFollow(actorFollow)
+
+    return actorFollow
   })
+
+  if (actorFollow.state === 'accepted') Notifier.Instance.notifyOfNewUserFollow(actorFollow)
 }