From f967da4ddaa090bb0bc3ce31af19fc66a160433a Mon Sep 17 00:00:00 2001 From: April John Date: Mon, 4 Sep 2023 17:55:17 +0200 Subject: [PATCH] [AkkomaAPI] Add endpoint /nodeinfo/2.0.json --- packages/backend/src/server/index.ts | 19 ++++++++++++++++++- packages/backend/src/server/nodeinfo.ts | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/packages/backend/src/server/index.ts b/packages/backend/src/server/index.ts index efff6dd23..593bf2aba 100644 --- a/packages/backend/src/server/index.ts +++ b/packages/backend/src/server/index.ts @@ -24,7 +24,7 @@ import * as Acct from "@/misc/acct.js"; import { envOption } from "@/env.js"; import megalodon, { MegalodonInterface } from "megalodon"; import activityPub from "./activitypub.js"; -import nodeinfo from "./nodeinfo.js"; +import nodeinfo, { nodeinfo2 } from "./nodeinfo.js"; import wellKnown from "./well-known.js"; import apiServer from "./api/index.js"; import fileServer from "./file/index.js"; @@ -34,6 +34,7 @@ import { initializeStreamingServer } from "./api/streaming.js"; import { koaBody } from "koa-body"; import removeTrailingSlash from "koa-remove-trailing-slashes"; import { v4 as uuid } from "uuid"; +import { Cache } from "@/misc/cache.js"; export const serverLogger = new Logger("server", "gray", false); @@ -149,6 +150,22 @@ mastoRouter.get("/oauth/authorize", async (ctx) => { ); }); +const cache = new Cache>>( + "nodeinfo", + 60 * 10, +); + + +mastoRouter.get("/nodeinfo/2.0.json", async (ctx) => { + const base = await cache.fetch(null, () => nodeinfo2()); + + // @ts-ignore + base.software.repository = undefined; + + ctx.body = { version: "2.0", ...base }; + ctx.set("Cache-Control", "public, max-age=600"); +}); + mastoRouter.post("/oauth/token", async (ctx) => { const body: any = ctx.request.body || ctx.request.query; console.log("token-request", body); diff --git a/packages/backend/src/server/nodeinfo.ts b/packages/backend/src/server/nodeinfo.ts index 0a042f365..9cf3ab636 100644 --- a/packages/backend/src/server/nodeinfo.ts +++ b/packages/backend/src/server/nodeinfo.ts @@ -23,7 +23,7 @@ export const links = [ }, ]; -const nodeinfo2 = async () => { +export const nodeinfo2 = async () => { const now = Date.now(); const [meta, total, activeHalfyear, activeMonth, localPosts] = await Promise.all([