activitypub-http-broadcast.ts 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. import * as Bull from 'bull'
  2. import * as Bluebird from 'bluebird'
  3. import { logger } from '../../../helpers/logger'
  4. import { doRequest } from '../../../helpers/requests'
  5. import { buildGlobalHeaders, buildSignedRequestOptions, computeBody } from './utils/activitypub-http-utils'
  6. import { BROADCAST_CONCURRENCY, JOB_REQUEST_TIMEOUT } from '../../../initializers/constants'
  7. import { ActorFollowScoreCache } from '../../files-cache'
  8. export type ActivitypubHttpBroadcastPayload = {
  9. uris: string[]
  10. signatureActorId?: number
  11. body: any
  12. }
  13. async function processActivityPubHttpBroadcast (job: Bull.Job) {
  14. logger.info('Processing ActivityPub broadcast in job %d.', job.id)
  15. const payload = job.data as ActivitypubHttpBroadcastPayload
  16. const body = await computeBody(payload)
  17. const httpSignatureOptions = await buildSignedRequestOptions(payload)
  18. const options = {
  19. method: 'POST',
  20. uri: '',
  21. json: body,
  22. httpSignature: httpSignatureOptions,
  23. timeout: JOB_REQUEST_TIMEOUT,
  24. headers: buildGlobalHeaders(body)
  25. }
  26. const badUrls: string[] = []
  27. const goodUrls: string[] = []
  28. await Bluebird.map(payload.uris, uri => {
  29. return doRequest(Object.assign({}, options, { uri }))
  30. .then(() => goodUrls.push(uri))
  31. .catch(() => badUrls.push(uri))
  32. }, { concurrency: BROADCAST_CONCURRENCY })
  33. return ActorFollowScoreCache.Instance.updateActorFollowsScore(goodUrls, badUrls)
  34. }
  35. // ---------------------------------------------------------------------------
  36. export {
  37. processActivityPubHttpBroadcast
  38. }