diff --git a/packages/backend/src/queue/processors/deliver.ts b/packages/backend/src/queue/processors/deliver.ts index 60355e39e..7ffc62d7f 100644 --- a/packages/backend/src/queue/processors/deliver.ts +++ b/packages/backend/src/queue/processors/deliver.ts @@ -4,7 +4,6 @@ import { request } from '@/remote/activitypub/request.js'; import { registerOrFetchInstanceDoc } from '@/services/register-or-fetch-instance-doc.js'; import Logger from '@/services/logger.js'; import { Instances } from '@/models/index.js'; -import { apRequestChart, federationChart, instanceChart } from '@/services/chart/index.js'; import { fetchInstanceMetadata } from '@/services/fetch-instance-metadata.js'; import { toPuny } from '@/misc/convert-host.js'; import { StatusError } from '@/misc/fetch.js'; @@ -38,10 +37,6 @@ export default async (job: Bull.Job) => { }); fetchInstanceMetadata(i); - - instanceChart.requestSent(i.host, true); - apRequestChart.deliverSucc(); - federationChart.deliverd(i.host, true); }); return 'Success'; @@ -53,10 +48,6 @@ export default async (job: Bull.Job) => { latestStatus: res instanceof StatusError ? res.statusCode : null, isNotResponding: true, }); - - instanceChart.requestSent(i.host, false); - apRequestChart.deliverFail(); - federationChart.deliverd(i.host, false); }); if (res instanceof StatusError) { diff --git a/packages/backend/src/remote/activitypub/request.ts b/packages/backend/src/remote/activitypub/request.ts index 233bc025f..cd70da69f 100644 --- a/packages/backend/src/remote/activitypub/request.ts +++ b/packages/backend/src/remote/activitypub/request.ts @@ -4,7 +4,16 @@ import { getUserKeypair } from '@/misc/keypair-store.js'; import { User } from '@/models/entities/user.js'; import { getResponse } from '@/misc/fetch.js'; import { createSignedPost, createSignedGet } from './ap-request.js'; +import { apRequestChart, federationChart, instanceChart } from '@/services/chart/index.js'; +/** + * Post an activity to an inbox. Automatically updates the statistics + * on succeeded or failed delivery attempts. + * + * @param user http-signature user + * @param url The URL of the inbox. + * @param object The Activity or other object to be posted to the inbox. + */ export async function request(user: { id: User['id'] }, url: string, object: any): Promise { const body = JSON.stringify(object); @@ -22,14 +31,28 @@ export async function request(user: { id: User['id'] }, url: string, object: any }, }); - await getResponse({ - url, - method: req.request.method, - headers: req.request.headers, - body, - // don't allow redirects on the inbox - redirect: 'error', - }); + const { host } = new URL(url); + + try { + await getResponse({ + url, + method: req.request.method, + headers: req.request.headers, + body, + // don't allow redirects on the inbox + redirect: 'error', + }); + + instanceChart.requestSent(host, true); + apRequestChart.deliverSucc(); + federationChart.deliverd(host, true); + } catch (err) { + instanceChart.requestSent(host, false); + apRequestChart.deliverFail(); + federationChart.deliverd(host, false); + + throw err; + } } /**