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