]> git.immae.eu Git - github/Chocobozzz/PeerTube.git/blob - server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts
Add scores to follows and remove bad ones
[github/Chocobozzz/PeerTube.git] / server / lib / jobs / activitypub-http-job-scheduler / activitypub-http-unicast-handler.ts
1 import { logger } from '../../../helpers/logger'
2 import { doRequest } from '../../../helpers/requests'
3 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
4 import { ActivityPubHttpPayload, buildSignedRequestOptions, computeBody, maybeRetryRequestLater } from './activitypub-http-job-scheduler'
5
6 async function process (payload: ActivityPubHttpPayload, jobId: number) {
7 logger.info('Processing ActivityPub unicast in job %d.', jobId)
8
9 const body = await computeBody(payload)
10 const httpSignatureOptions = await buildSignedRequestOptions(payload)
11
12 const uri = payload.uris[0]
13 const options = {
14 method: 'POST',
15 uri,
16 json: body,
17 httpSignature: httpSignatureOptions
18 }
19
20 try {
21 await doRequest(options)
22 await ActorFollowModel.updateActorFollowsScoreAndRemoveBadOnes([ uri ], [], undefined)
23 } catch (err) {
24 const isRetryingLater = await maybeRetryRequestLater(err, payload, uri)
25 if (isRetryingLater === false) {
26 await ActorFollowModel.updateActorFollowsScoreAndRemoveBadOnes([], [ uri ], undefined)
27 }
28
29 throw err
30 }
31 }
32
33 function onError (err: Error, jobId: number) {
34 logger.error('Error when sending ActivityPub request in job %d.', jobId, err)
35 return Promise.resolve()
36 }
37
38 function onSuccess (jobId: number) {
39 logger.info('Job %d is a success.', jobId)
40 return Promise.resolve()
41 }
42
43 // ---------------------------------------------------------------------------
44
45 export {
46 process,
47 onError,
48 onSuccess
49 }