1
0
Fork 0
peertube/shared/extra-utils/requests/activitypub.ts

43 lines
1.2 KiB
TypeScript
Raw Normal View History

import { doRequest } from '../../../server/helpers/requests'
import { HTTP_SIGNATURE } from '../../../server/initializers/constants'
import { buildGlobalHeaders } from '../../../server/lib/job-queue/handlers/utils/activitypub-http-utils'
import { activityPubContextify } from '../../../server/helpers/activitypub'
2018-11-14 09:01:28 -05:00
function makePOSTAPRequest (url: string, body: any, httpSignature: any, headers: any) {
const options = {
2021-03-08 08:24:11 -05:00
method: 'POST' as 'POST',
json: body,
httpSignature,
headers
}
2021-03-08 08:24:11 -05:00
return doRequest(url, options)
}
async function makeFollowRequest (to: { url: string }, by: { url: string, privateKey }) {
const follow = {
type: 'Follow',
2021-03-10 05:17:20 -05:00
id: by.url + '/' + new Date().getTime(),
actor: by.url,
object: to.url
}
const body = activityPubContextify(follow)
const httpSignature = {
algorithm: HTTP_SIGNATURE.ALGORITHM,
authorizationHeaderName: HTTP_SIGNATURE.HEADER_NAME,
keyId: by.url,
key: by.privateKey,
headers: HTTP_SIGNATURE.HEADERS_TO_SIGN
}
const headers = buildGlobalHeaders(body)
2021-03-10 05:17:20 -05:00
return makePOSTAPRequest(to.url + '/inbox', body, httpSignature, headers)
}
export {
2018-11-14 09:01:28 -05:00
makePOSTAPRequest,
makeFollowRequest
}