]>
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 { ActorFollowModel } from '../../../models/activitypub/actor-follow' | |
6 | import { buildSignedRequestOptions, computeBody } from './utils/activitypub-http-utils' | |
7 | import { BROADCAST_CONCURRENCY, JOB_REQUEST_TIMEOUT } from '../../../initializers' | |
8 | ||
9 | export type ActivitypubHttpBroadcastPayload = { | |
10 | uris: string[] | |
11 | signatureActorId?: number | |
12 | body: any | |
13 | } | |
14 | ||
15 | async function processActivityPubHttpBroadcast (job: Bull.Job) { | |
16 | logger.info('Processing ActivityPub broadcast in job %d.', job.id) | |
17 | ||
18 | const payload = job.data as ActivitypubHttpBroadcastPayload | |
19 | ||
20 | const body = await computeBody(payload) | |
21 | const httpSignatureOptions = await buildSignedRequestOptions(payload) | |
22 | ||
23 | const options = { | |
24 | method: 'POST', | |
25 | uri: '', | |
26 | json: body, | |
27 | httpSignature: httpSignatureOptions, | |
28 | timeout: JOB_REQUEST_TIMEOUT | |
29 | } | |
30 | ||
31 | const badUrls: string[] = [] | |
32 | const goodUrls: string[] = [] | |
33 | ||
34 | await Bluebird.map(payload.uris, uri => { | |
35 | return doRequest(Object.assign({}, options, { uri })) | |
36 | .then(() => goodUrls.push(uri)) | |
37 | .catch(() => badUrls.push(uri)) | |
38 | }, { concurrency: BROADCAST_CONCURRENCY }) | |
39 | ||
40 | return ActorFollowModel.updateActorFollowsScore(goodUrls, badUrls, undefined) | |
41 | } | |
42 | ||
43 | // --------------------------------------------------------------------------- | |
44 | ||
45 | export { | |
46 | processActivityPubHttpBroadcast | |
47 | } |