]>
Commit | Line | Data |
---|---|---|
afffe988 C |
1 | import { logger } from '../../../helpers' |
2 | import { buildSignedActivity } from '../../../helpers/activitypub' | |
3 | import { doRequest } from '../../../helpers/requests' | |
4 | import { database as db } from '../../../initializers' | |
0032ebe9 | 5 | import { ActivityPubHttpPayload, maybeRetryRequestLater } from './activitypub-http-job-scheduler' |
afffe988 C |
6 | |
7 | async function process (payload: ActivityPubHttpPayload, jobId: number) { | |
8 | logger.info('Processing ActivityPub broadcast in job %d.', jobId) | |
9 | ||
10 | const accountSignature = await db.Account.load(payload.signatureAccountId) | |
11 | if (!accountSignature) throw new Error('Unknown signature account id.') | |
12 | ||
13 | const signedBody = await buildSignedActivity(accountSignature, payload.body) | |
14 | ||
15 | const options = { | |
16 | method: 'POST', | |
17 | uri: '', | |
18 | json: signedBody | |
19 | } | |
20 | ||
21 | for (const uri of payload.uris) { | |
22 | options.uri = uri | |
0032ebe9 C |
23 | |
24 | try { | |
25 | await doRequest(options) | |
26 | } catch (err) { | |
27 | await maybeRetryRequestLater(err, payload, uri) | |
28 | } | |
afffe988 C |
29 | } |
30 | } | |
31 | ||
32 | function onError (err: Error, jobId: number) { | |
33 | logger.error('Error when broadcasting ActivityPub request in job %d.', jobId, err) | |
34 | return Promise.resolve() | |
35 | } | |
36 | ||
37 | function onSuccess (jobId: number) { | |
38 | logger.info('Job %d is a success.', jobId) | |
39 | return Promise.resolve() | |
40 | } | |
41 | ||
42 | // --------------------------------------------------------------------------- | |
43 | ||
44 | export { | |
45 | process, | |
46 | onError, | |
47 | onSuccess | |
48 | } |