activitypub-http-unicast.ts 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. import { ActivitypubHttpUnicastPayload } from '@shared/models'
  8. async function processActivityPubHttpUnicast (job: Bull.Job) {
  9. logger.info('Processing ActivityPub unicast in job %d.', job.id)
  10. const payload = job.data as ActivitypubHttpUnicastPayload
  11. const uri = payload.uri
  12. const body = await computeBody(payload)
  13. const httpSignatureOptions = await buildSignedRequestOptions(payload)
  14. const options = {
  15. method: 'POST',
  16. uri,
  17. json: body,
  18. httpSignature: httpSignatureOptions,
  19. timeout: JOB_REQUEST_TIMEOUT,
  20. headers: buildGlobalHeaders(body)
  21. }
  22. try {
  23. await doRequest(options)
  24. ActorFollowScoreCache.Instance.updateActorFollowsScore([ uri ], [])
  25. } catch (err) {
  26. ActorFollowScoreCache.Instance.updateActorFollowsScore([], [ uri ])
  27. throw err
  28. }
  29. }
  30. // ---------------------------------------------------------------------------
  31. export {
  32. processActivityPubHttpUnicast
  33. }