diff options
author | Chocobozzz <florian.bigard@gmail.com> | 2017-11-17 11:35:10 +0100 |
---|---|---|
committer | Chocobozzz <florian.bigard@gmail.com> | 2017-11-27 19:40:52 +0100 |
commit | afffe98839db7ccbfa9fb8b7d1413b97900fdc73 (patch) | |
tree | f0b3cbe58f73e81a5ba4bb31dabd9691994cf2ca /server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts | |
parent | 1b3989b0961d22a5a45ad16239e3c3f58f66180c (diff) | |
download | PeerTube-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-broadcast-handler.ts')
-rw-r--r-- | server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts new file mode 100644 index 000000000..111fc88a4 --- /dev/null +++ b/server/lib/jobs/activitypub-http-job-scheduler/activitypub-http-broadcast-handler.ts | |||
@@ -0,0 +1,43 @@ | |||
1 | import { logger } from '../../../helpers' | ||
2 | import { buildSignedActivity } from '../../../helpers/activitypub' | ||
3 | import { doRequest } from '../../../helpers/requests' | ||
4 | import { database as db } from '../../../initializers' | ||
5 | import { ActivityPubHttpPayload } from './activitypub-http-job-scheduler' | ||
6 | |||
7 | async function process (payload: ActivityPubHttpPayload, jobId: number) { | ||
8 | logger.info('Processing ActivityPub broadcast 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 | |||
15 | const options = { | ||
16 | method: 'POST', | ||
17 | uri: '', | ||
18 | json: signedBody | ||
19 | } | ||
20 | |||
21 | for (const uri of payload.uris) { | ||
22 | options.uri = uri | ||
23 | await doRequest(options) | ||
24 | } | ||
25 | } | ||
26 | |||
27 | function onError (err: Error, jobId: number) { | ||
28 | logger.error('Error when broadcasting ActivityPub request in job %d.', jobId, err) | ||
29 | return Promise.resolve() | ||
30 | } | ||
31 | |||
32 | function onSuccess (jobId: number) { | ||
33 | logger.info('Job %d is a success.', jobId) | ||
34 | return Promise.resolve() | ||
35 | } | ||
36 | |||
37 | // --------------------------------------------------------------------------- | ||
38 | |||
39 | export { | ||
40 | process, | ||
41 | onError, | ||
42 | onSuccess | ||
43 | } | ||