2017-11-24 06:41:10 -06:00
|
|
|
import { JobCategory } from '../../../../shared'
|
2017-12-28 04:16:08 -06:00
|
|
|
import { buildSignedActivity } from '../../../helpers/activitypub'
|
|
|
|
import { logger } from '../../../helpers/logger'
|
|
|
|
import { getServerActor } from '../../../helpers/utils'
|
2017-12-12 10:53:50 -06:00
|
|
|
import { ACTIVITY_PUB } from '../../../initializers'
|
2017-12-14 10:38:41 -06:00
|
|
|
import { ActorModel } from '../../../models/activitypub/actor'
|
2018-01-12 03:02:11 -06:00
|
|
|
import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
|
2017-11-24 06:41:10 -06:00
|
|
|
import { JobHandler, JobScheduler } from '../job-scheduler'
|
2017-11-17 04:35:10 -06:00
|
|
|
|
|
|
|
import * as activitypubHttpBroadcastHandler from './activitypub-http-broadcast-handler'
|
2017-11-22 03:29:55 -06:00
|
|
|
import * as activitypubHttpFetcherHandler from './activitypub-http-fetcher-handler'
|
2017-11-24 06:41:10 -06:00
|
|
|
import * as activitypubHttpUnicastHandler from './activitypub-http-unicast-handler'
|
2017-11-17 04:35:10 -06:00
|
|
|
|
|
|
|
type ActivityPubHttpPayload = {
|
|
|
|
uris: string[]
|
2017-12-14 10:38:41 -06:00
|
|
|
signatureActorId?: number
|
2017-11-22 03:29:55 -06:00
|
|
|
body?: any
|
2017-11-23 07:19:55 -06:00
|
|
|
attemptNumber?: number
|
2017-11-17 04:35:10 -06:00
|
|
|
}
|
2017-11-23 07:19:55 -06:00
|
|
|
|
2017-11-17 04:35:10 -06:00
|
|
|
const jobHandlers: { [ handlerName: string ]: JobHandler<ActivityPubHttpPayload, void> } = {
|
|
|
|
activitypubHttpBroadcastHandler,
|
2017-11-22 03:29:55 -06:00
|
|
|
activitypubHttpUnicastHandler,
|
|
|
|
activitypubHttpFetcherHandler
|
2017-11-17 04:35:10 -06:00
|
|
|
}
|
|
|
|
const jobCategory: JobCategory = 'activitypub-http'
|
|
|
|
|
|
|
|
const activitypubHttpJobScheduler = new JobScheduler(jobCategory, jobHandlers)
|
|
|
|
|
2018-01-11 02:35:50 -06:00
|
|
|
async function maybeRetryRequestLater (err: Error, payload: ActivityPubHttpPayload, uri: string) {
|
2017-11-23 07:19:55 -06:00
|
|
|
logger.warn('Cannot make request to %s.', uri, err)
|
|
|
|
|
|
|
|
let attemptNumber = payload.attemptNumber || 1
|
|
|
|
attemptNumber += 1
|
|
|
|
|
|
|
|
if (attemptNumber < ACTIVITY_PUB.MAX_HTTP_ATTEMPT) {
|
|
|
|
logger.debug('Retrying request to %s (attempt %d/%d).', uri, attemptNumber, ACTIVITY_PUB.MAX_HTTP_ATTEMPT, err)
|
|
|
|
|
2018-01-12 03:02:11 -06:00
|
|
|
const actor = await ActorFollowModel.loadByFollowerInbox(uri, undefined)
|
|
|
|
if (!actor) {
|
|
|
|
logger.debug('Actor %s is not a follower, do not retry the request.', uri)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-11-23 07:19:55 -06:00
|
|
|
const newPayload = Object.assign(payload, {
|
|
|
|
uris: [ uri ],
|
|
|
|
attemptNumber
|
|
|
|
})
|
2018-01-11 02:35:50 -06:00
|
|
|
await activitypubHttpJobScheduler.createJob(undefined, 'activitypubHttpUnicastHandler', newPayload)
|
|
|
|
|
|
|
|
return true
|
2017-11-23 07:19:55 -06:00
|
|
|
}
|
2018-01-11 02:35:50 -06:00
|
|
|
|
|
|
|
return false
|
2017-11-23 07:19:55 -06:00
|
|
|
}
|
|
|
|
|
2017-11-24 06:41:10 -06:00
|
|
|
async function computeBody (payload: ActivityPubHttpPayload) {
|
|
|
|
let body = payload.body
|
|
|
|
|
2017-12-14 10:38:41 -06:00
|
|
|
if (payload.signatureActorId) {
|
|
|
|
const actorSignature = await ActorModel.load(payload.signatureActorId)
|
2017-12-19 03:34:56 -06:00
|
|
|
if (!actorSignature) throw new Error('Unknown signature actor id.')
|
2017-12-14 10:38:41 -06:00
|
|
|
body = await buildSignedActivity(actorSignature, payload.body)
|
2017-11-24 06:41:10 -06:00
|
|
|
}
|
2017-11-24 08:00:10 -06:00
|
|
|
|
2017-11-24 06:41:10 -06:00
|
|
|
return body
|
|
|
|
}
|
|
|
|
|
2017-12-19 03:34:56 -06:00
|
|
|
async function buildSignedRequestOptions (payload: ActivityPubHttpPayload) {
|
|
|
|
let actor: ActorModel
|
|
|
|
if (payload.signatureActorId) {
|
|
|
|
actor = await ActorModel.load(payload.signatureActorId)
|
|
|
|
if (!actor) throw new Error('Unknown signature actor id.')
|
|
|
|
} else {
|
|
|
|
// We need to sign the request, so use the server
|
|
|
|
actor = await getServerActor()
|
|
|
|
}
|
|
|
|
|
|
|
|
const keyId = actor.getWebfingerUrl()
|
|
|
|
return {
|
|
|
|
algorithm: 'rsa-sha256',
|
|
|
|
authorizationHeaderName: 'Signature',
|
|
|
|
keyId,
|
|
|
|
key: actor.privateKey
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 04:35:10 -06:00
|
|
|
export {
|
|
|
|
ActivityPubHttpPayload,
|
2017-11-23 07:19:55 -06:00
|
|
|
activitypubHttpJobScheduler,
|
2017-11-24 06:41:10 -06:00
|
|
|
maybeRetryRequestLater,
|
2017-12-19 03:34:56 -06:00
|
|
|
computeBody,
|
|
|
|
buildSignedRequestOptions
|
2017-11-17 04:35:10 -06:00
|
|
|
}
|