diff options
Diffstat (limited to 'server/lib/jobs')
-rw-r--r-- | server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts | 69 | ||||
-rw-r--r-- | server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts | 8 |
2 files changed, 74 insertions, 3 deletions
diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts new file mode 100644 index 000000000..b8ead32a4 --- /dev/null +++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-handler.ts | |||
@@ -0,0 +1,69 @@ | |||
1 | import { logger } from '../../../helpers' | ||
2 | import { buildSignedActivity } from '../../../helpers/activitypub' | ||
3 | import { doRequest } from '../../../helpers/requests' | ||
4 | import { database as db } from '../../../initializers' | ||
5 | import { ActivityPubHttpPayload } from './activitypub-http-job-scheduler' | ||
6 | import { processActivities } from '../../activitypub/process/process' | ||
7 | import { ACTIVITY_PUB } from '../../../initializers/constants' | ||
8 | |||
9 | async function process (payload: ActivityPubHttpPayload, jobId: number) { | ||
10 | logger.info('Processing ActivityPub fetcher in job %d.', jobId) | ||
11 | |||
12 | const options = { | ||
13 | method: 'GET', | ||
14 | uri: '', | ||
15 | json: true | ||
16 | } | ||
17 | |||
18 | for (const uri of payload.uris) { | ||
19 | options.uri = uri | ||
20 | logger.info('Fetching ActivityPub data on %s.', uri) | ||
21 | |||
22 | const response = await doRequest(options) | ||
23 | const firstBody = response.body | ||
24 | |||
25 | if (firstBody.first && Array.isArray(firstBody.first.orderedItems)) { | ||
26 | const activities = firstBody.first.orderedItems | ||
27 | |||
28 | logger.info('Processing %i items ActivityPub fetcher for %s.', activities.length, uri) | ||
29 | |||
30 | await processActivities(activities) | ||
31 | } | ||
32 | |||
33 | let limit = ACTIVITY_PUB.FETCH_PAGE_LIMIT | ||
34 | let i = 0 | ||
35 | let nextLink = firstBody.first.next | ||
36 | while (nextLink && i < limit) { | ||
37 | options.uri = nextLink | ||
38 | |||
39 | const { body } = await doRequest(options) | ||
40 | nextLink = body.nextLink | ||
41 | i++ | ||
42 | |||
43 | if (Array.isArray(body.orderedItems)) { | ||
44 | const activities = body.orderedItems | ||
45 | logger.info('Processing %i items ActivityPub fetcher for %s.', activities.length, uri) | ||
46 | |||
47 | await processActivities(activities) | ||
48 | } | ||
49 | } | ||
50 | } | ||
51 | } | ||
52 | |||
53 | function onError (err: Error, jobId: number) { | ||
54 | logger.error('Error when broadcasting ActivityPub request in job %d.', jobId, err) | ||
55 | return Promise.resolve() | ||
56 | } | ||
57 | |||
58 | function onSuccess (jobId: number) { | ||
59 | logger.info('Job %d is a success.', jobId) | ||
60 | return Promise.resolve() | ||
61 | } | ||
62 | |||
63 | // --------------------------------------------------------------------------- | ||
64 | |||
65 | export { | ||
66 | process, | ||
67 | onError, | ||
68 | onSuccess | ||
69 | } | ||
diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts index e4f6c94a5..aef217ce7 100644 --- a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts +++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-job-scheduler.ts | |||
@@ -2,16 +2,18 @@ import { JobScheduler, JobHandler } from '../job-scheduler' | |||
2 | 2 | ||
3 | import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-handler' | 3 | import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-handler' |
4 | import * as activitypubHttpUnicastHandler from './activitypub-http-unicast-handler' | 4 | import * as activitypubHttpUnicastHandler from './activitypub-http-unicast-handler' |
5 | import * as activitypubHttpFetcherHandler from './activitypub-http-fetcher-handler' | ||
5 | import { JobCategory } from '../../../../shared' | 6 | import { JobCategory } from '../../../../shared' |
6 | 7 | ||
7 | type ActivityPubHttpPayload = { | 8 | type ActivityPubHttpPayload = { |
8 | uris: string[] | 9 | uris: string[] |
9 | signatureAccountId: number | 10 | signatureAccountId?: number |
10 | body: any | 11 | body?: any |
11 | } | 12 | } |
12 | const jobHandlers: { [ handlerName: string ]: JobHandler<ActivityPubHttpPayload, void> } = { | 13 | const jobHandlers: { [ handlerName: string ]: JobHandler<ActivityPubHttpPayload, void> } = { |
13 | activitypubHttpBroadcastHandler, | 14 | activitypubHttpBroadcastHandler, |
14 | activitypubHttpUnicastHandler | 15 | activitypubHttpUnicastHandler, |
16 | activitypubHttpFetcherHandler | ||
15 | } | 17 | } |
16 | const jobCategory: JobCategory = 'activitypub-http' | 18 | const jobCategory: JobCategory = 'activitypub-http' |
17 | 19 | ||