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