]>
Commit | Line | Data |
---|---|---|
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 | import { ActivitypubHttpBroadcastPayload } from '@shared/models' | |
9 | ||
10 | async function processActivityPubHttpBroadcast (job: Bull.Job) { | |
11 | logger.info('Processing ActivityPub broadcast in job %d.', job.id) | |
12 | ||
13 | const payload = job.data as ActivitypubHttpBroadcastPayload | |
14 | ||
15 | const body = await computeBody(payload) | |
16 | const httpSignatureOptions = await buildSignedRequestOptions(payload) | |
17 | ||
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 | ||
27 | const badUrls: string[] = [] | |
28 | const goodUrls: string[] = [] | |
29 | ||
30 | await Bluebird.map(payload.uris, uri => { | |
31 | return doRequest(Object.assign({}, options, { uri })) | |
32 | .then(() => goodUrls.push(uri)) | |
33 | .catch(() => badUrls.push(uri)) | |
34 | }, { concurrency: BROADCAST_CONCURRENCY }) | |
35 | ||
36 | return ActorFollowScoreCache.Instance.updateActorFollowsScore(goodUrls, badUrls) | |
37 | } | |
38 | ||
39 | // --------------------------------------------------------------------------- | |
40 | ||
41 | export { | |
42 | processActivityPubHttpBroadcast | |
43 | } |