aboutsummaryrefslogtreecommitdiffhomepage
path: root/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts
diff options
context:
space:
mode:
authorChocobozzz <florian.bigard@gmail.com>2017-11-17 11:35:10 +0100
committerChocobozzz <florian.bigard@gmail.com>2017-11-27 19:40:52 +0100
commitafffe98839db7ccbfa9fb8b7d1413b97900fdc73 (patch)
treef0b3cbe58f73e81a5ba4bb31dabd9691994cf2ca /server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts
parent1b3989b0961d22a5a45ad16239e3c3f58f66180c (diff)
downloadPeerTube-afffe98839db7ccbfa9fb8b7d1413b97900fdc73.tar.gz
PeerTube-afffe98839db7ccbfa9fb8b7d1413b97900fdc73.tar.zst
PeerTube-afffe98839db7ccbfa9fb8b7d1413b97900fdc73.zip
Speed up activity pub http requests
Diffstat (limited to 'server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts')
-rw-r--r--server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts40
1 files changed, 40 insertions, 0 deletions
diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts
new file mode 100644
index 000000000..8d3b755ad
--- /dev/null
+++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-unicast-handler.ts
@@ -0,0 +1,40 @@
1import { logger } from '../../../helpers'
2import { doRequest } from '../../../helpers/requests'
3import { ActivityPubHttpPayload } from './activitypub-http-job-scheduler'
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
21 await doRequest(options)
22}
23
24function onError (err: Error, jobId: number) {
25 logger.error('Error when sending ActivityPub request in job %d.', jobId, err)
26 return Promise.resolve()
27}
28
29function onSuccess (jobId: number) {
30 logger.info('Job %d is a success.', jobId)
31 return Promise.resolve()
32}
33
34// ---------------------------------------------------------------------------
35
36export {
37 process,
38 onError,
39 onSuccess
40}