PeerTube/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-fetcher-ha...

68 lines
1.9 KiB
TypeScript
Raw Normal View History

2017-11-22 03:29:55 -06:00
import { logger } from '../../../helpers'
import { doRequest } from '../../../helpers/requests'
import { ACTIVITY_PUB } from '../../../initializers/constants'
2017-11-22 09:25:03 -06:00
import { processActivities } from '../../activitypub/process/process'
import { ActivityPubHttpPayload } from './activitypub-http-job-scheduler'
2017-11-22 03:29:55 -06:00
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)
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)
nextLink = body.next
2017-11-22 03:29:55 -06:00
i++
if (Array.isArray(body.orderedItems)) {
const activities = body.orderedItems
logger.info('Processing %i items ActivityPub fetcher for %s.', activities.length, options.uri)
2017-11-22 03:29:55 -06:00
await processActivities(activities)
}
}
}
}
function onError (err: Error, jobId: number) {
2017-11-23 09:55:13 -06:00
logger.error('Error when fetcher ActivityPub request in job %d.', jobId, err)
2017-11-22 03:29:55 -06:00
return Promise.resolve()
}
function onSuccess (jobId: number) {
logger.info('Job %d is a success.', jobId)
return Promise.resolve()
}
// ---------------------------------------------------------------------------
export {
process,
onError,
onSuccess
}