]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts
Federate likes/dislikes
[github/Chocobozzz/PeerTube.git] / server / lib / jobs / activitypub-http-job-scheduler / activitypub-http-unicast-handler.ts
1 import { logger } from '../../../helpers'
2 import { doRequest } from '../../../helpers/requests'
3 import { ActivityPubHttpPayload, maybeRetryRequestLater } from './activitypub-http-job-scheduler'
4 import { database as db } from '../../../initializers/database'
5 import { buildSignedActivity } from '../../../helpers/activitypub'
6
7 async function process (payload: ActivityPubHttpPayload, jobId: number) {
8 logger.info('Processing ActivityPub unicast 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 const uri = payload.uris[0]
15 const options = {
16 method: 'POST',
17 uri,
18 json: signedBody
19 }
20
21 try {
22 await doRequest(options)
23 } catch (err) {
24 await maybeRetryRequestLater(err, payload, uri)
25 throw err
26 }
27 }
28
29 function onError (err: Error, jobId: number) {
30 logger.error('Error when sending ActivityPub request in job %d.', jobId, err)
31 return Promise.resolve()
32 }
33
34 function onSuccess (jobId: number) {
35 logger.info('Job %d is a success.', jobId)
36 return Promise.resolve()
37 }
38
39 // ---------------------------------------------------------------------------
40
41 export {
42 process,
43 onError,
44 onSuccess
45 }