PeerTube/server/tests/shared/requests.ts

42 lines
1.2 KiB
TypeScript
Raw Normal View History

import { activityPubContextify } from '@server/helpers/activitypub'
import { buildDigest } from '@server/helpers/peertube-crypto'
import { doRequest } from '@server/helpers/requests'
import { ACTIVITY_PUB, HTTP_SIGNATURE } from '@server/initializers/constants'
export function makePOSTAPRequest (url: string, body: any, httpSignature: any, headers: any) {
const options = {
2021-03-08 07:24:11 -06:00
method: 'POST' as 'POST',
json: body,
httpSignature,
headers
}
2021-03-08 07:24:11 -06:00
return doRequest(url, options)
}
export async function makeFollowRequest (to: { url: string }, by: { url: string, privateKey }) {
const follow = {
type: 'Follow',
2021-03-10 04:17:20 -06: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 = {
'digest': buildDigest(body),
'content-type': 'application/activity+json',
'accept': ACTIVITY_PUB.ACCEPT_HEADER
}
2021-03-10 04:17:20 -06:00
return makePOSTAPRequest(to.url + '/inbox', body, httpSignature, headers)
}