2018-01-25 08:05:18 -06:00
|
|
|
import * as kue from 'kue'
|
2017-12-28 04:16:08 -06:00
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { doRequest } from '../../../helpers/requests'
|
2018-05-09 02:08:22 -05:00
|
|
|
import { ACTIVITY_PUB, JOB_REQUEST_TIMEOUT } from '../../../initializers'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { processActivities } from '../../activitypub/process'
|
2018-01-25 08:05:18 -06:00
|
|
|
import { ActivitypubHttpBroadcastPayload } from './activitypub-http-broadcast'
|
2017-11-22 03:29:55 -06:00
|
|
|
|
2018-01-25 08:05:18 -06:00
|
|
|
export type ActivitypubHttpFetcherPayload = {
|
|
|
|
uris: string[]
|
|
|
|
}
|
|
|
|
|
|
|
|
async function processActivityPubHttpFetcher (job: kue.Job) {
|
|
|
|
logger.info('Processing ActivityPub fetcher in job %d.', job.id)
|
|
|
|
|
|
|
|
const payload = job.data as ActivitypubHttpBroadcastPayload
|
2017-11-22 03:29:55 -06:00
|
|
|
|
|
|
|
const options = {
|
|
|
|
method: 'GET',
|
|
|
|
uri: '',
|
2017-12-28 04:16:08 -06:00
|
|
|
json: true,
|
2018-05-09 02:08:22 -05:00
|
|
|
activityPub: true,
|
|
|
|
timeout: JOB_REQUEST_TIMEOUT
|
2017-11-22 03:29:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2017-11-22 04:27:40 -06:00
|
|
|
logger.info('Processing %i items ActivityPub fetcher for %s.', activities.length, options.uri)
|
2017-11-22 03:29:55 -06:00
|
|
|
|
|
|
|
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)
|
2017-11-22 04:27:40 -06:00
|
|
|
nextLink = body.next
|
2017-11-22 03:29:55 -06:00
|
|
|
i++
|
|
|
|
|
|
|
|
if (Array.isArray(body.orderedItems)) {
|
|
|
|
const activities = body.orderedItems
|
2017-11-22 04:27:40 -06:00
|
|
|
logger.info('Processing %i items ActivityPub fetcher for %s.', activities.length, options.uri)
|
2017-11-22 03:29:55 -06:00
|
|
|
|
|
|
|
await processActivities(activities)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
|
|
export {
|
2018-01-25 08:05:18 -06:00
|
|
|
processActivityPubHttpFetcher
|
2017-11-22 03:29:55 -06:00
|
|
|
}
|