From 38b8c34a050cbf5baaa754b66cabea141a5e8b1e Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Tue, 17 Sep 2019 14:36:54 -0400 Subject: [PATCH] Add nodeinfo support --- lib/activitypub/index.ts | 4 ++- lib/activitypub/nodeinfo.ts | 59 +++++++++++++++++++++++++++++++++++++ lib/index.ts | 1 + 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 lib/activitypub/nodeinfo.ts diff --git a/lib/activitypub/index.ts b/lib/activitypub/index.ts index 0f7bfdf..901f969 100644 --- a/lib/activitypub/index.ts +++ b/lib/activitypub/index.ts @@ -4,6 +4,7 @@ import comments from "./comments"; import federate from "./federate"; import followers from "./followers"; import inbox from "./inbox"; +import nodeinfo from "./nodeinfo"; import webfinger from "./webfinger"; export = { @@ -13,5 +14,6 @@ export = { federate, followers, inbox, + nodeinfo, webfinger -}; \ No newline at end of file +}; diff --git a/lib/activitypub/nodeinfo.ts b/lib/activitypub/nodeinfo.ts new file mode 100644 index 0000000..753a5df --- /dev/null +++ b/lib/activitypub/nodeinfo.ts @@ -0,0 +1,59 @@ +import {Request, Response, Router} from "express"; +import { promises as fs } from "fs"; +import {getConnection} from "typeorm"; +import Article from "../entity/Article"; +import Note from "../entity/Note"; + +const domain = process.env.DOMAIN; + +export default async function nodeinfo(router: Router) { + router.get("/.well-known/nodeinfo", (req, res) => { + res.json({ + links: [ + { + href: `https://${domain}/nodeinfo/2.0.json`, + rel: "https://nodeinfo.diaspora.software/ns/schema/2.0" + }, + { + href: `https://${domain}/nodeinfo/2.1.json`, + rel: "https://nodeinfo.diaspora.software/ns/schema/2.1" + } + ] + }); + res.end(); + }); + + router.get("/nodeinfo/2.0.json", await handleNodeInfo("2.0")); + router.get("/nodeinfo/2.1.json", await handleNodeInfo("2.1")); +} + +async function handleNodeInfo(version: string) { + const softwareVersion = JSON.parse((await fs.readFile("package.json")).toString()).version; + const postCount = await getConnection().getRepository(Article).count(); + return async (req: Request, res: Response) => { + res.json({ + version: version, + software: { + name: "shadowfacts_blog", + version: softwareVersion, + repository: version === "2.1" ? "https://git.shadowfacts.net/shadowfacts/shadowfacts.net" : undefined + }, + protocols: ["activitypub"], + services: { + inbound: [], + outbound: ["atom1.0"] + }, + openRegistrations: false, + usage: { + users: { + total: 1, + activeHalfyear: 1, + activeMonth: 1 + }, + localPosts: postCount, + localComments: await getConnection().getRepository(Note).count() + } + }); + res.end(); + }; +} diff --git a/lib/index.ts b/lib/index.ts index 478a4d9..d7d1b42 100644 --- a/lib/index.ts +++ b/lib/index.ts @@ -68,6 +68,7 @@ async function generate(): Promise { activitypub.comments(apRouter); activitypub.followers(apRouter); activitypub.inbox(apRouter); + await activitypub.nodeinfo(apRouter); activitypub.webfinger(apRouter); activitypub.articles.route(apRouter); app.use(apRouter);