backend: refactor server/nodeinfo.ts #221

Merged
norm merged 5 commits from refactor-nodeinfo into main 2022-11-02 21:42:52 +00:00

View file

@ -3,7 +3,6 @@ import { IsNull, MoreThan } from 'typeorm';
import config from '@/config/index.js'; import config from '@/config/index.js';
import { fetchMeta } from '@/misc/fetch-meta.js'; import { fetchMeta } from '@/misc/fetch-meta.js';
import { Users, Notes } from '@/models/index.js'; import { Users, Notes } from '@/models/index.js';
import { Cache } from '@/misc/cache.js';
const router = new Router(); const router = new Router();
@ -18,7 +17,33 @@ export const links = [{
href: config.url + nodeinfo2_0path, href: config.url + nodeinfo2_0path,
}]; }];
const nodeinfo2 = async () => { const repository = 'https://akkoma.dev/FoundKeyGang/FoundKey';
type NodeInfo2Base = {
software: {
name: string;
version: string;
repository?: string; // Not used in NodeInfo 2.0; used in 2.1
};
protocols: string[];
services: {
inbound: string[];
outbound: string[];
};
openRegistrations: boolean;
usage: {
users: {
total: number;
activeHalfyear: number;
activeMonth: number;
};
localPosts: number;
localComments: number;
};
metadata: Record<string, any>;
};
const nodeinfo2 = async (): Promise<NodeInfo2Base> => {
const now = Date.now(); const now = Date.now();
const [ const [
meta, meta,
@ -40,7 +65,7 @@ const nodeinfo2 = async () => {
software: { software: {
name: 'foundkey', name: 'foundkey',
version: config.version, version: config.version,
repository: 'https://akkoma.dev/FoundKeyGang/FoundKey', repository,
}, },
protocols: ['activitypub'], protocols: ['activitypub'],
services: { services: {
@ -62,7 +87,7 @@ const nodeinfo2 = async () => {
}, },
langs: meta.langs, langs: meta.langs,
tosUrl: meta.ToSUrl, tosUrl: meta.ToSUrl,
repositoryUrl: meta.repositoryUrl, repositoryUrl: repository,
feedbackUrl: 'ircs://irc.akkoma.dev/foundkey', feedbackUrl: 'ircs://irc.akkoma.dev/foundkey',
disableRegistration: meta.disableRegistration, disableRegistration: meta.disableRegistration,
disableLocalTimeline: meta.disableLocalTimeline, disableLocalTimeline: meta.disableLocalTimeline,
@ -82,17 +107,15 @@ const nodeinfo2 = async () => {
}; };
}; };
const cache = new Cache<Awaited<ReturnType<typeof nodeinfo2>>>(1000 * 60 * 10);
router.get(nodeinfo2_1path, async ctx => { router.get(nodeinfo2_1path, async ctx => {
const base = await cache.fetch(null, () => nodeinfo2()); const base = await nodeinfo2();
norm marked this conversation as resolved Outdated

Is this cache even necessary? Maybe we should rely on the caching of the reverse proxy more.

Is this cache even necessary? Maybe we should rely on the caching of the reverse proxy more.

Does the reverse proxy cache it by default?

Does the reverse proxy cache it by default?
ctx.body = { version: '2.1', ...base }; ctx.body = { version: '2.1', ...base };
ctx.set('Cache-Control', 'public, max-age=600'); ctx.set('Cache-Control', 'public, max-age=600');
}); });
router.get(nodeinfo2_0path, async ctx => { router.get(nodeinfo2_0path, async ctx => {
const base = await cache.fetch(null, () => nodeinfo2()); const base = await nodeinfo2();
delete base.software.repository; delete base.software.repository;