shadowfacts.net/lib/activitypub/webfinger.ts

41 lines
1.1 KiB
TypeScript
Raw Permalink Normal View History

import express, { Router } from "express";
2022-05-16 04:41:01 +00:00
import fetch from "node-fetch";
const domain = process.env.DOMAIN;
2019-02-20 23:07:29 +00:00
export default function webfinger(router: Router) {
router.get("/.well-known/webfinger", (req, res) => {
2022-05-16 04:41:01 +00:00
res.json({
2022-06-17 00:02:47 +00:00
subject: `acct:blog@${domain}`,
2022-05-16 04:41:01 +00:00
aliases: [`https://${domain}/ap/actor`],
links: [
{
rel: "self",
type: "application/activity+json",
href: `https://${domain}/ap/actor`,
}
]
} as WebfingerResult);
res.end();
});
2022-05-16 04:41:01 +00:00
}
export async function queryWebfinger(acct: string): Promise<WebfingerResult> {
if (acct.startsWith("@")) {
acct = acct.substring(1);
}
const parts = acct.split("@");
if (parts.length !== 2) {
throw "Invalid account";
}
const response = await fetch(`https://${parts[1]}/.well-known/webfinger?resource=${acct}`);
const json = await response.json();
return json as WebfingerResult;
}
export interface WebfingerResult {
subject: string;
aliases: string[];
links: Array<{rel: string, type: string, href: string} | {rel: "https://ostatus.org/schema/1.0/subscribe", template: string}>;
}