]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts
Status are sent to mastodon
[github/Chocobozzz/PeerTube.git] / server / lib / jobs / activitypub-http-job-scheduler / activitypub-http-broadcast-handler.ts
1 import { doRequest, logger } from '../../../helpers'
2 import { ActivityPubHttpPayload, buildSignedRequestOptions, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler'
3
4 async function process (payload: ActivityPubHttpPayload, jobId: number) {
5 logger.info('Processing ActivityPub broadcast in job %d.', jobId)
6
7 const body = await computeBody(payload)
8 const httpSignatureOptions = await buildSignedRequestOptions(payload)
9
10 const options = {
11 method: 'POST',
12 uri: '',
13 json: body,
14 httpSignature: httpSignatureOptions
15 }
16
17 for (const uri of payload.uris) {
18 options.uri = uri
19
20 try {
21 await doRequest(options)
22 } catch (err) {
23 await maybeRetryRequestLater(err, payload, uri)
24 }
25 }
26 }
27
28 function onError (err: Error, jobId: number) {
29 logger.error('Error when broadcasting ActivityPub request in job %d.', jobId, err)
30 return Promise.resolve()
31 }
32
33 function onSuccess (jobId: number) {
34 logger.info('Job %d is a success.', jobId)
35 return Promise.resolve()
36 }
37
38 // ---------------------------------------------------------------------------
39
40 export {
41 process,
42 onError,
43 onSuccess
44 }