]>
Commit | Line | Data |
---|---|---|
1 | import { logger } from '../../../helpers/logger' | |
2 | import { doRequest } from '../../../helpers/requests' | |
3 | import { ActorFollowModel } from '../../../models/activitypub/actor-follow' | |
4 | import { ActivityPubHttpPayload, buildSignedRequestOptions, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler' | |
5 | ||
6 | async function process (payload: ActivityPubHttpPayload, jobId: number) { | |
7 | logger.info('Processing ActivityPub broadcast in job %d.', jobId) | |
8 | ||
9 | const body = await computeBody(payload) | |
10 | const httpSignatureOptions = await buildSignedRequestOptions(payload) | |
11 | ||
12 | const options = { | |
13 | method: 'POST', | |
14 | uri: '', | |
15 | json: body, | |
16 | httpSignature: httpSignatureOptions | |
17 | } | |
18 | ||
19 | const badUrls: string[] = [] | |
20 | const goodUrls: string[] = [] | |
21 | ||
22 | for (const uri of payload.uris) { | |
23 | options.uri = uri | |
24 | ||
25 | try { | |
26 | await doRequest(options) | |
27 | goodUrls.push(uri) | |
28 | } catch (err) { | |
29 | const isRetryingLater = await maybeRetryRequestLater(err, payload, uri) | |
30 | if (isRetryingLater === false) badUrls.push(uri) | |
31 | } | |
32 | } | |
33 | ||
34 | return ActorFollowModel.updateActorFollowsScoreAndRemoveBadOnes(goodUrls, badUrls, undefined) | |
35 | } | |
36 | ||
37 | function onError (err: Error, jobId: number) { | |
38 | logger.error('Error when broadcasting ActivityPub request in job %d.', jobId, err) | |
39 | return Promise.resolve() | |
40 | } | |
41 | ||
42 | function onSuccess (jobId: number) { | |
43 | logger.info('Job %d is a success.', jobId) | |
44 | return Promise.resolve() | |
45 | } | |
46 | ||
47 | // --------------------------------------------------------------------------- | |
48 | ||
49 | export { | |
50 | process, | |
51 | onError, | |
52 | onSuccess | |
53 | } |