]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blobdiff - server/lib/job-queue/handlers/activitypub-follow.ts
Use worker thread to send HTTP requests
[github/Chocobozzz/PeerTube.git] / server / lib / job-queue / handlers / activitypub-follow.ts
index 76b6fcaae94d1a7e4b879d428f8f23f0d9e7c4a5..a68c32ba069f1de826613c8cd0e325fb0dd1f1aa 100644 (file)
@@ -1,24 +1,23 @@
-import * as Bull from 'bull'
+import { Job } from 'bullmq'
 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 { MActor, MActorFull } from '../../../types/models'
+import { getOrCreateAPActor, loadActorUrlOrGetFromWebfinger } from '../../activitypub/actors'
 import { sendFollow } from '../../activitypub/send'
 import { Notifier } from '../../notifier'
 
-async function processActivityPubFollow (job: Bull.Job) {
+async function processActivityPubFollow (job: Job) {
   const payload = job.data as ActivitypubFollowPayload
   const host = payload.host
 
-  logger.info('Processing ActivityPub follow in job %d.', job.id)
+  logger.info('Processing ActivityPub follow in job %s.', job.id)
 
   let targetActor: MActorFull
   if (!host || host === WEBSERVER.HOST) {
@@ -55,21 +54,13 @@ async function follow (fromActor: MActor, targetActor: MActorFull, isAutoFollow
   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,
-        url: getLocalActorFollowActivityPubUrl(fromActor, targetActor),
-        actorId: fromActor.id,
-        targetActorId: targetActor.id
-      },
+    const [ actorFollow ] = await ActorFollowModel.findOrCreateCustom({
+      byActor: fromActor,
+      state,
+      targetActor,
+      activityId: getLocalActorFollowActivityPubUrl(fromActor, targetActor),
       transaction: t
     })
-    actorFollow.ActorFollowing = targetActor
-    actorFollow.ActorFollower = fromActor
 
     // Send a notification to remote server if our follow is not already accepted
     if (actorFollow.state !== 'accepted') sendFollow(actorFollow, t)