]>
Commit | Line | Data |
---|---|---|
63c93323 | 1 | import { JobCategory } from '../../../../shared' |
3fd3ab2d C |
2 | import { buildSignedActivity, logger } from '../../../helpers' |
3 | import { ACTIVITY_PUB } from '../../../initializers' | |
50d6de9c | 4 | import { ActorModel } from '../../../models/activitypub/actor' |
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[] | |
50d6de9c | 13 | signatureActorId?: number |
c986175d | 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 | ||
50d6de9c C |
47 | if (payload.signatureActorId) { |
48 | const actorSignature = await ActorModel.load(payload.signatureActorId) | |
49 | if (!actorSignature) throw new Error('Unknown signature account id.') | |
50 | body = await buildSignedActivity(actorSignature, payload.body) | |
63c93323 | 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 | } |