From 0fc002b3992756b8ef8f8c259d528506a48c8aed Mon Sep 17 00:00:00 2001 From: sam Date: Fri, 21 Jul 2023 03:12:00 +0200 Subject: [PATCH] add basic nodeinfo 2.0 --- src/routes/nodeinfo/nodeinfo_2.0.ts | 35 +++++++++++++++++++++++++++++ src/routes/well-known/nodeinfo.ts | 20 +++++++++++++++++ 2 files changed, 55 insertions(+) create mode 100644 src/routes/nodeinfo/nodeinfo_2.0.ts create mode 100644 src/routes/well-known/nodeinfo.ts diff --git a/src/routes/nodeinfo/nodeinfo_2.0.ts b/src/routes/nodeinfo/nodeinfo_2.0.ts new file mode 100644 index 0000000..197a32b --- /dev/null +++ b/src/routes/nodeinfo/nodeinfo_2.0.ts @@ -0,0 +1,35 @@ +import { RouteOptions } from "fastify"; +import { IsNull } from "typeorm"; + +import MercuryDataSource from "~/db/index.js"; +import { Blog } from "~/db/entities/blog.js"; +import { Post } from "~/db/entities/post.js"; + +const route: RouteOptions = { + method: "GET", + url: "/nodeinfo/2.0", + handler: async (_, res) => { + const userCount = await MercuryDataSource.getRepository(Blog).countBy({ + host: IsNull(), + }); + const postCount = await MercuryDataSource.getRepository(Post).count({ + relations: { blog: true }, + where: { blog: { host: IsNull() } }, + }); + + res.send({ + version: "2.0", + software: { name: "mercury", version: "0.1.0-dev" }, + protocols: ["activitypub"], + openRegistrations: false, // TODO: get from database + usage: { + users: { + total: userCount, + }, + localPosts: postCount, + }, + }); + }, +}; + +export default route; diff --git a/src/routes/well-known/nodeinfo.ts b/src/routes/well-known/nodeinfo.ts new file mode 100644 index 0000000..9eeb672 --- /dev/null +++ b/src/routes/well-known/nodeinfo.ts @@ -0,0 +1,20 @@ +import { RouteOptions } from "fastify"; + +import { BASE_URL } from "~/config.js"; + +const route: RouteOptions = { + method: "GET", + url: "/.well-known/nodeinfo", + handler: async (_, res) => { + res.send({ + links: [ + { + href: `${BASE_URL}/nodeinfo/2.0`, + rel: "http://nodeinfo.diaspora.software/ns/schema/2.0", + }, + ], + }); + }, +}; + +export default route;