aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/job-queue/handlers/activitypub-http-broadcast.ts
blob: 57ecf0acc41793b4eb87a526f652d6b6cb6ba53e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { Job } from 'bullmq'
import { buildGlobalHeaders, buildSignedRequestOptions, computeBody } from '@server/lib/activitypub/send'
import { ActorFollowHealthCache } from '@server/lib/actor-follow-health-cache'
import { parallelHTTPBroadcastFromWorker, sequentialHTTPBroadcastFromWorker } from '@server/lib/worker/parent-process'
import { ActivitypubHttpBroadcastPayload } from '@shared/models'
import { logger } from '../../../helpers/logger'

// Prefer using a worker thread for HTTP requests because on high load we may have to sign many requests, which can be CPU intensive

async function processActivityPubHttpSequentialBroadcast (job: Job<ActivitypubHttpBroadcastPayload>) {
  logger.info('Processing ActivityPub broadcast in job %s.', job.id)

  const requestOptions = await buildRequestOptions(job.data)

  const { badUrls, goodUrls } = await sequentialHTTPBroadcastFromWorker({ uris: job.data.uris, requestOptions })

  return ActorFollowHealthCache.Instance.updateActorFollowsHealth(goodUrls, badUrls)
}

async function processActivityPubParallelHttpBroadcast (job: Job<ActivitypubHttpBroadcastPayload>) {
  logger.info('Processing ActivityPub parallel broadcast in job %s.', job.id)

  const requestOptions = await buildRequestOptions(job.data)

  const { badUrls, goodUrls } = await parallelHTTPBroadcastFromWorker({ uris: job.data.uris, requestOptions })

  return ActorFollowHealthCache.Instance.updateActorFollowsHealth(goodUrls, badUrls)
}

// ---------------------------------------------------------------------------

export {
  processActivityPubHttpSequentialBroadcast,
  processActivityPubParallelHttpBroadcast
}

// ---------------------------------------------------------------------------

async function buildRequestOptions (payload: ActivitypubHttpBroadcastPayload) {
  const body = await computeBody(payload)
  const httpSignatureOptions = await buildSignedRequestOptions(payload)

  return {
    method: 'POST' as 'POST',
    json: body,
    httpSignature: httpSignatureOptions,
    headers: buildGlobalHeaders(body)
  }
}