]>
Commit | Line | Data |
---|---|---|
63c93323 | 1 | import { JobCategory } from '../../../../shared' |
3fd3ab2d C |
2 | import { buildSignedActivity, logger } from '../../../helpers' |
3 | import { ACTIVITY_PUB } from '../../../initializers' | |
4 | import { AccountModel } from '../../../models/account/account' | |
63c93323 | 5 | import { JobHandler, JobScheduler } from '../job-scheduler' |
afffe988 C |
6 | |
7 | import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-handler' | |
c986175d | 8 | import * as activitypubHttpFetcherHandler from './activitypub-http-fetcher-handler' |
63c93323 | 9 | import * as activitypubHttpUnicastHandler from './activitypub-http-unicast-handler' |
afffe988 C |
10 | |
11 | type ActivityPubHttpPayload = { | |
12 | uris: string[] | |
c986175d C |
13 | signatureAccountId?: number |
14 | body?: any | |
0032ebe9 | 15 | attemptNumber?: number |
afffe988 | 16 | } |
0032ebe9 | 17 | |
afffe988 C |
18 | const jobHandlers: { [ handlerName: string ]: JobHandler<ActivityPubHttpPayload, void> } = { |
19 | activitypubHttpBroadcastHandler, | |
c986175d C |
20 | activitypubHttpUnicastHandler, |
21 | activitypubHttpFetcherHandler | |
afffe988 C |
22 | } |
23 | const jobCategory: JobCategory = 'activitypub-http' | |
24 | ||
25 | const activitypubHttpJobScheduler = new JobScheduler(jobCategory, jobHandlers) | |
26 | ||
0032ebe9 C |
27 | function maybeRetryRequestLater (err: Error, payload: ActivityPubHttpPayload, uri: string) { |
28 | logger.warn('Cannot make request to %s.', uri, err) | |
29 | ||
30 | let attemptNumber = payload.attemptNumber || 1 | |
31 | attemptNumber += 1 | |
32 | ||
33 | if (attemptNumber < ACTIVITY_PUB.MAX_HTTP_ATTEMPT) { | |
34 | logger.debug('Retrying request to %s (attempt %d/%d).', uri, attemptNumber, ACTIVITY_PUB.MAX_HTTP_ATTEMPT, err) | |
35 | ||
36 | const newPayload = Object.assign(payload, { | |
37 | uris: [ uri ], | |
38 | attemptNumber | |
39 | }) | |
40 | return activitypubHttpJobScheduler.createJob(undefined, 'activitypubHttpUnicastHandler', newPayload) | |
41 | } | |
42 | } | |
43 | ||
63c93323 C |
44 | async function computeBody (payload: ActivityPubHttpPayload) { |
45 | let body = payload.body | |
46 | ||
47 | if (payload.signatureAccountId) { | |
3fd3ab2d | 48 | const accountSignature = await AccountModel.load(payload.signatureAccountId) |
63c93323 C |
49 | if (!accountSignature) throw new Error('Unknown signature account id.') |
50 | body = await buildSignedActivity(accountSignature, payload.body) | |
51 | } | |
df1966c9 | 52 | |
63c93323 C |
53 | return body |
54 | } | |
55 | ||
afffe988 C |
56 | export { |
57 | ActivityPubHttpPayload, | |
0032ebe9 | 58 | activitypubHttpJobScheduler, |
63c93323 C |
59 | maybeRetryRequestLater, |
60 | computeBody | |
afffe988 | 61 | } |