2021-12-17 11:58:15 +01:00
|
|
|
import { buildDigest } from '@server/helpers/peertube-crypto'
|
|
|
|
import { doRequest } from '@server/helpers/requests'
|
|
|
|
import { ACTIVITY_PUB, HTTP_SIGNATURE } from '@server/initializers/constants'
|
2022-03-23 14:24:50 +01:00
|
|
|
import { activityPubContextify } from '@server/lib/activitypub/context'
|
2018-10-23 11:38:48 +02:00
|
|
|
|
2021-11-02 19:11:20 +01:00
|
|
|
export function makePOSTAPRequest (url: string, body: any, httpSignature: any, headers: any) {
|
2018-10-23 11:38:48 +02:00
|
|
|
const options = {
|
2021-03-08 14:24:11 +01:00
|
|
|
method: 'POST' as 'POST',
|
2018-10-23 11:38:48 +02:00
|
|
|
json: body,
|
|
|
|
httpSignature,
|
|
|
|
headers
|
|
|
|
}
|
|
|
|
|
2021-03-08 14:24:11 +01:00
|
|
|
return doRequest(url, options)
|
2018-10-23 11:38:48 +02:00
|
|
|
}
|
|
|
|
|
2021-11-02 19:11:20 +01:00
|
|
|
export async function makeFollowRequest (to: { url: string }, by: { url: string, privateKey }) {
|
2018-10-23 11:38:48 +02:00
|
|
|
const follow = {
|
|
|
|
type: 'Follow',
|
2021-03-10 11:17:20 +01:00
|
|
|
id: by.url + '/' + new Date().getTime(),
|
2018-10-23 11:38:48 +02:00
|
|
|
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
|
|
|
|
}
|
2021-12-17 11:58:15 +01:00
|
|
|
const headers = {
|
|
|
|
'digest': buildDigest(body),
|
|
|
|
'content-type': 'application/activity+json',
|
|
|
|
'accept': ACTIVITY_PUB.ACCEPT_HEADER
|
|
|
|
}
|
2018-10-23 11:38:48 +02:00
|
|
|
|
2021-03-10 11:17:20 +01:00
|
|
|
return makePOSTAPRequest(to.url + '/inbox', body, httpSignature, headers)
|
2018-10-23 11:38:48 +02:00
|
|
|
}
|