]>
Commit | Line | Data |
---|---|---|
da854ddd C |
1 | import { logger } from '../../../helpers/logger' |
2 | import { doRequest } from '../../../helpers/requests' | |
3fd3ab2d C |
3 | import { ACTIVITY_PUB } from '../../../initializers' |
4 | import { processActivities } from '../../activitypub/process' | |
40ff5707 | 5 | import { ActivityPubHttpPayload } from './activitypub-http-job-scheduler' |
c986175d C |
6 | |
7 | async function process (payload: ActivityPubHttpPayload, jobId: number) { | |
8 | logger.info('Processing ActivityPub fetcher in job %d.', jobId) | |
9 | ||
10 | const options = { | |
11 | method: 'GET', | |
12 | uri: '', | |
da854ddd C |
13 | json: true, |
14 | activityPub: true | |
c986175d C |
15 | } |
16 | ||
17 | for (const uri of payload.uris) { | |
18 | options.uri = uri | |
19 | logger.info('Fetching ActivityPub data on %s.', uri) | |
20 | ||
21 | const response = await doRequest(options) | |
22 | const firstBody = response.body | |
23 | ||
24 | if (firstBody.first && Array.isArray(firstBody.first.orderedItems)) { | |
25 | const activities = firstBody.first.orderedItems | |
26 | ||
c46edbc2 | 27 | logger.info('Processing %i items ActivityPub fetcher for %s.', activities.length, options.uri) |
c986175d C |
28 | |
29 | await processActivities(activities) | |
30 | } | |
31 | ||
32 | let limit = ACTIVITY_PUB.FETCH_PAGE_LIMIT | |
33 | let i = 0 | |
34 | let nextLink = firstBody.first.next | |
35 | while (nextLink && i < limit) { | |
36 | options.uri = nextLink | |
37 | ||
38 | const { body } = await doRequest(options) | |
c46edbc2 | 39 | nextLink = body.next |
c986175d C |
40 | i++ |
41 | ||
42 | if (Array.isArray(body.orderedItems)) { | |
43 | const activities = body.orderedItems | |
c46edbc2 | 44 | logger.info('Processing %i items ActivityPub fetcher for %s.', activities.length, options.uri) |
c986175d C |
45 | |
46 | await processActivities(activities) | |
47 | } | |
48 | } | |
49 | } | |
50 | } | |
51 | ||
52 | function onError (err: Error, jobId: number) { | |
16b90975 | 53 | logger.error('Error when fetcher ActivityPub request in job %d.', jobId, err) |
c986175d C |
54 | return Promise.resolve() |
55 | } | |
56 | ||
57 | function onSuccess (jobId: number) { | |
58 | logger.info('Job %d is a success.', jobId) | |
59 | return Promise.resolve() | |
60 | } | |
61 | ||
62 | // --------------------------------------------------------------------------- | |
63 | ||
64 | export { | |
65 | process, | |
66 | onError, | |
67 | onSuccess | |
68 | } |