]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts
Misc cleanup
[github/Chocobozzz/PeerTube.git] / server / lib / jobs / activitypub-http-job-scheduler / activitypub-http-broadcast-handler.ts
CommitLineData
afffe988
C
1import { logger } from '../../../helpers'
2import { buildSignedActivity } from '../../../helpers/activitypub'
3import { doRequest } from '../../../helpers/requests'
4import { database as db } from '../../../initializers'
0032ebe9 5import { ActivityPubHttpPayload, maybeRetryRequestLater } from './activitypub-http-job-scheduler'
afffe988
C
6
7async function process (payload: ActivityPubHttpPayload, jobId: number) {
8 logger.info('Processing ActivityPub broadcast in job %d.', jobId)
9
10 const accountSignature = await db.Account.load(payload.signatureAccountId)
11 if (!accountSignature) throw new Error('Unknown signature account id.')
12
13 const signedBody = await buildSignedActivity(accountSignature, payload.body)
14
15 const options = {
16 method: 'POST',
17 uri: '',
18 json: signedBody
19 }
20
21 for (const uri of payload.uris) {
22 options.uri = uri
0032ebe9
C
23
24 try {
25 await doRequest(options)
26 } catch (err) {
27 await maybeRetryRequestLater(err, payload, uri)
28 }
afffe988
C
29 }
30}
31
32function onError (err: Error, jobId: number) {
33 logger.error('Error when broadcasting ActivityPub request in job %d.', jobId, err)
34 return Promise.resolve()
35}
36
37function onSuccess (jobId: number) {
38 logger.info('Job %d is a success.', jobId)
39 return Promise.resolve()
40}
41
42// ---------------------------------------------------------------------------
43
44export {
45 process,
46 onError,
47 onSuccess
48}