change video type
[oweals/peertube.git] / server / lib / job-queue / handlers / activitypub-http-unicast.ts
1 import * as Bull from 'bull'
2 import { logger } from '../../../helpers/logger'
3 import { doRequest } from '../../../helpers/requests'
4 import { ActorFollowModel } from '../../../models/activitypub/actor-follow'
5 import { buildGlobalHeaders, buildSignedRequestOptions, computeBody } from './utils/activitypub-http-utils'
6 import { JOB_REQUEST_TIMEOUT } from '../../../initializers'
7
8 export type ActivitypubHttpUnicastPayload = {
9   uri: string
10   signatureActorId?: number
11   body: any
12 }
13
14 async function processActivityPubHttpUnicast (job: Bull.Job) {
15   logger.info('Processing ActivityPub unicast in job %d.', job.id)
16
17   const payload = job.data as ActivitypubHttpUnicastPayload
18   const uri = payload.uri
19
20   const body = await computeBody(payload)
21   const httpSignatureOptions = await buildSignedRequestOptions(payload)
22
23   const options = {
24     method: 'POST',
25     uri,
26     json: body,
27     httpSignature: httpSignatureOptions,
28     timeout: JOB_REQUEST_TIMEOUT,
29     headers: buildGlobalHeaders(body)
30   }
31
32   try {
33     await doRequest(options)
34     ActorFollowModel.updateActorFollowsScore([ uri ], [], undefined)
35   } catch (err) {
36     ActorFollowModel.updateActorFollowsScore([], [ uri ], undefined)
37
38     throw err
39   }
40 }
41
42 // ---------------------------------------------------------------------------
43
44 export {
45   processActivityPubHttpUnicast
46 }