aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/tests/utils/requests/activitypub.ts
blob: 96fee60a87211c958865cf74c98d386f8fc547a6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { doRequest } from '../../../helpers/requests'
import { HTTP_SIGNATURE } from '../../../initializers'
import { buildGlobalHeaders } from '../../../lib/job-queue/handlers/utils/activitypub-http-utils'
import { activityPubContextify } from '../../../helpers/activitypub'

function makePOSTAPRequest (url: string, body: any, httpSignature: any, headers: any) {
  const options = {
    method: 'POST',
    uri: url,
    json: body,
    httpSignature,
    headers
  }

  return doRequest(options)
}

async function makeFollowRequest (to: { url: string }, by: { url: string, privateKey }) {
  const follow = {
    type: 'Follow',
    id: by.url + '/toto',
    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)

  return makePOSTAPRequest(to.url, body, httpSignature, headers)
}

export {
  makePOSTAPRequest,
  makeFollowRequest
}