From db4b15f21fbf4e33434e930ffc7fb768cdcf9d42 Mon Sep 17 00:00:00 2001 From: Chocobozzz Date: Mon, 8 Mar 2021 14:24:11 +0100 Subject: Use got instead of request --- server/lib/job-queue/handlers/activitypub-cleaner.ts | 11 +++-------- server/lib/job-queue/handlers/activitypub-http-broadcast.ts | 5 ++--- server/lib/job-queue/handlers/activitypub-http-unicast.ts | 5 ++--- server/lib/job-queue/handlers/utils/activitypub-http-utils.ts | 9 ++++++--- 4 files changed, 13 insertions(+), 17 deletions(-) (limited to 'server/lib/job-queue') diff --git a/server/lib/job-queue/handlers/activitypub-cleaner.ts b/server/lib/job-queue/handlers/activitypub-cleaner.ts index 0e75b0a6e..9dcc778fa 100644 --- a/server/lib/job-queue/handlers/activitypub-cleaner.ts +++ b/server/lib/job-queue/handlers/activitypub-cleaner.ts @@ -7,7 +7,7 @@ import { isLikeActivityValid } from '@server/helpers/custom-validators/activitypub/activity' import { sanitizeAndCheckVideoCommentObject } from '@server/helpers/custom-validators/activitypub/video-comments' -import { doRequest } from '@server/helpers/requests' +import { doJSONRequest } from '@server/helpers/requests' import { AP_CLEANER_CONCURRENCY } from '@server/initializers/constants' import { VideoModel } from '@server/models/video/video' import { VideoCommentModel } from '@server/models/video/video-comment' @@ -81,15 +81,10 @@ async function updateObjectIfNeeded ( updater: (url: string, newUrl: string) => Promise, deleter: (url: string) => Promise ): Promise<{ data: T, status: 'deleted' | 'updated' } | null> { - // Fetch url - const { response, body } = await doRequest({ - uri: url, - json: true, - activityPub: true - }) + const { statusCode, body } = await doJSONRequest(url, { activityPub: true }) // Does not exist anymore, remove entry - if (response.statusCode === HttpStatusCode.NOT_FOUND_404) { + if (statusCode === HttpStatusCode.NOT_FOUND_404) { logger.info('Removing remote AP object %s.', url) const data = await deleter(url) diff --git a/server/lib/job-queue/handlers/activitypub-http-broadcast.ts b/server/lib/job-queue/handlers/activitypub-http-broadcast.ts index 7174786d6..c69ff9e83 100644 --- a/server/lib/job-queue/handlers/activitypub-http-broadcast.ts +++ b/server/lib/job-queue/handlers/activitypub-http-broadcast.ts @@ -16,8 +16,7 @@ async function processActivityPubHttpBroadcast (job: Bull.Job) { const httpSignatureOptions = await buildSignedRequestOptions(payload) const options = { - method: 'POST', - uri: '', + method: 'POST' as 'POST', json: body, httpSignature: httpSignatureOptions, timeout: REQUEST_TIMEOUT, @@ -28,7 +27,7 @@ async function processActivityPubHttpBroadcast (job: Bull.Job) { const goodUrls: string[] = [] await Bluebird.map(payload.uris, uri => { - return doRequest(Object.assign({}, options, { uri })) + return doRequest(uri, options) .then(() => goodUrls.push(uri)) .catch(() => badUrls.push(uri)) }, { concurrency: BROADCAST_CONCURRENCY }) diff --git a/server/lib/job-queue/handlers/activitypub-http-unicast.ts b/server/lib/job-queue/handlers/activitypub-http-unicast.ts index 74989d62e..585dad671 100644 --- a/server/lib/job-queue/handlers/activitypub-http-unicast.ts +++ b/server/lib/job-queue/handlers/activitypub-http-unicast.ts @@ -16,8 +16,7 @@ async function processActivityPubHttpUnicast (job: Bull.Job) { const httpSignatureOptions = await buildSignedRequestOptions(payload) const options = { - method: 'POST', - uri, + method: 'POST' as 'POST', json: body, httpSignature: httpSignatureOptions, timeout: REQUEST_TIMEOUT, @@ -25,7 +24,7 @@ async function processActivityPubHttpUnicast (job: Bull.Job) { } try { - await doRequest(options) + await doRequest(uri, options) ActorFollowScoreCache.Instance.updateActorFollowsScore([ uri ], []) } catch (err) { ActorFollowScoreCache.Instance.updateActorFollowsScore([], [ uri ]) diff --git a/server/lib/job-queue/handlers/utils/activitypub-http-utils.ts b/server/lib/job-queue/handlers/utils/activitypub-http-utils.ts index c030d31ef..4116a9c0e 100644 --- a/server/lib/job-queue/handlers/utils/activitypub-http-utils.ts +++ b/server/lib/job-queue/handlers/utils/activitypub-http-utils.ts @@ -6,21 +6,24 @@ import { getServerActor } from '@server/models/application/application' import { buildDigest } from '@server/helpers/peertube-crypto' import { ContextType } from '@shared/models/activitypub/context' -type Payload = { body: any, contextType?: ContextType, signatureActorId?: number } +type Payload = { body: T, contextType?: ContextType, signatureActorId?: number } -async function computeBody (payload: Payload) { +async function computeBody ( + payload: Payload +): Promise { let body = payload.body if (payload.signatureActorId) { const actorSignature = await ActorModel.load(payload.signatureActorId) if (!actorSignature) throw new Error('Unknown signature actor id.') + body = await buildSignedActivity(actorSignature, payload.body, payload.contextType) } return body } -async function buildSignedRequestOptions (payload: Payload) { +async function buildSignedRequestOptions (payload: Payload) { let actor: MActor | null if (payload.signatureActorId) { -- cgit v1.2.3