]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blame - 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
CommitLineData
afffe988
C
1import { logger } from '../../../helpers'
2import { doRequest } from '../../../helpers/requests'
0032ebe9 3import { ActivityPubHttpPayload, maybeRetryRequestLater } from './activitypub-http-job-scheduler'
afffe988
C
4import { database as db } from '../../../initializers/database'
5import { buildSignedActivity } from '../../../helpers/activitypub'
6
7async 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
0032ebe9
C
21 try {
22 await doRequest(options)
23 } catch (err) {
24 await maybeRetryRequestLater(err, payload, uri)
25 throw err
26 }
afffe988
C
27}
28
29function onError (err: Error, jobId: number) {
30 logger.error('Error when sending ActivityPub request in job %d.', jobId, err)
31 return Promise.resolve()
32}
33
34function onSuccess (jobId: number) {
35 logger.info('Job %d is a success.', jobId)
36 return Promise.resolve()
37}
38
39// ---------------------------------------------------------------------------
40
41export {
42 process,
43 onError,
44 onSuccess
45}