server: do AP sent statistics in request function

This commit is contained in:
Johann150 2023-03-11 00:01:51 +01:00
parent 180a1c968d
commit 2164fda2fb
Signed by: Johann150
GPG key ID: 9EE6577A2A06F8F1
2 changed files with 31 additions and 17 deletions

View file

@ -4,7 +4,6 @@ import { request } from '@/remote/activitypub/request.js';
import { registerOrFetchInstanceDoc } from '@/services/register-or-fetch-instance-doc.js'; import { registerOrFetchInstanceDoc } from '@/services/register-or-fetch-instance-doc.js';
import Logger from '@/services/logger.js'; import Logger from '@/services/logger.js';
import { Instances } from '@/models/index.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 { fetchInstanceMetadata } from '@/services/fetch-instance-metadata.js';
import { toPuny } from '@/misc/convert-host.js'; import { toPuny } from '@/misc/convert-host.js';
import { StatusError } from '@/misc/fetch.js'; import { StatusError } from '@/misc/fetch.js';
@ -38,10 +37,6 @@ export default async (job: Bull.Job<DeliverJobData>) => {
}); });
fetchInstanceMetadata(i); fetchInstanceMetadata(i);
instanceChart.requestSent(i.host, true);
apRequestChart.deliverSucc();
federationChart.deliverd(i.host, true);
}); });
return 'Success'; return 'Success';
@ -53,10 +48,6 @@ export default async (job: Bull.Job<DeliverJobData>) => {
latestStatus: res instanceof StatusError ? res.statusCode : null, latestStatus: res instanceof StatusError ? res.statusCode : null,
isNotResponding: true, isNotResponding: true,
}); });
instanceChart.requestSent(i.host, false);
apRequestChart.deliverFail();
federationChart.deliverd(i.host, false);
}); });
if (res instanceof StatusError) { if (res instanceof StatusError) {

View file

@ -4,7 +4,16 @@ import { getUserKeypair } from '@/misc/keypair-store.js';
import { User } from '@/models/entities/user.js'; import { User } from '@/models/entities/user.js';
import { getResponse } from '@/misc/fetch.js'; import { getResponse } from '@/misc/fetch.js';
import { createSignedPost, createSignedGet } from './ap-request.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<void> { export async function request(user: { id: User['id'] }, url: string, object: any): Promise<void> {
const body = JSON.stringify(object); const body = JSON.stringify(object);
@ -22,14 +31,28 @@ export async function request(user: { id: User['id'] }, url: string, object: any
}, },
}); });
await getResponse({ const { host } = new URL(url);
url,
method: req.request.method, try {
headers: req.request.headers, await getResponse({
body, url,
// don't allow redirects on the inbox method: req.request.method,
redirect: 'error', 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;
}
} }
/** /**