2021-07-16 07:27:30 -05:00
|
|
|
import { activityPubContextify } from '../../../server/helpers/activitypub'
|
2018-12-07 09:09:57 -06:00
|
|
|
import { doRequest } from '../../../server/helpers/requests'
|
2019-04-11 07:26:41 -05:00
|
|
|
import { HTTP_SIGNATURE } from '../../../server/initializers/constants'
|
2018-12-07 09:09:57 -06:00
|
|
|
import { buildGlobalHeaders } from '../../../server/lib/job-queue/handlers/utils/activitypub-http-utils'
|
2018-10-23 04:38:48 -05:00
|
|
|
|
2018-11-14 08:01:28 -06:00
|
|
|
function makePOSTAPRequest (url: string, body: any, httpSignature: any, headers: any) {
|
2018-10-23 04:38:48 -05:00
|
|
|
const options = {
|
2021-03-08 07:24:11 -06:00
|
|
|
method: 'POST' as 'POST',
|
2018-10-23 04:38:48 -05:00
|
|
|
json: body,
|
|
|
|
httpSignature,
|
|
|
|
headers
|
|
|
|
}
|
|
|
|
|
2021-03-08 07:24:11 -06:00
|
|
|
return doRequest(url, options)
|
2018-10-23 04:38:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
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(),
|
2018-10-23 04:38:48 -05: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
|
|
|
|
}
|
|
|
|
const headers = buildGlobalHeaders(body)
|
|
|
|
|
2021-03-10 04:17:20 -06:00
|
|
|
return makePOSTAPRequest(to.url + '/inbox', body, httpSignature, headers)
|
2018-10-23 04:38:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
export {
|
2018-11-14 08:01:28 -06:00
|
|
|
makePOSTAPRequest,
|
2018-10-23 04:38:48 -05:00
|
|
|
makeFollowRequest
|
|
|
|
}
|