activitypub-http-unicast.ts 1.3 KB

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