From edd46a4a1bc0c117e1366e97bc70bcf140c695c7 Mon Sep 17 00:00:00 2001 From: Natty Date: Tue, 31 Oct 2023 18:45:43 +0100 Subject: [PATCH] Gracefully try host-meta if WebFinger fails --- create-circle.js | 29 +++++++++++++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/create-circle.js b/create-circle.js index e410e04..361a623 100644 --- a/create-circle.js +++ b/create-circle.js @@ -80,11 +80,36 @@ Handle.prototype.webFinger = async function () { return this; } - let url = `https://${this.instance}/.well-known/webfinger?` + new URLSearchParams({ + const defaultWebfingerUrl = `https://${this.instance}/.well-known/webfinger?` + new URLSearchParams({ resource: `acct:${this}` }); - let webFinger = await apiRequest(url); + let webFinger = await apiRequest(defaultWebfingerUrl); + + if (!webFinger) { + const contentTypeXrd = "application/xrd+xml"; + const hostMetaUrl = `https://${this.instance}/.well-known/host-meta`; + + console.log(`Fetching :: ${hostMetaUrl}`); + let hostMeta = await fetch(hostMetaUrl, { + headers: { + "Accept": contentTypeXrd + }, + redirect: "follow" + }).then(async res => { + return new DOMParser() + .parseFromString(await res.text(), "text/xml"); + }).catch(e => { + console.error(`Error fetching ${hostMetaUrl}: ${e}`); + return null; + }); + + const webfingerTemplate = hostMeta.querySelector("Link[rel='lrdd']") + ?.getAttribute("template"); + + if (webfingerTemplate) + webFinger = await apiRequest(webfingerTemplate.replace("{uri}", `acct:${this}`)); + } if (!webFinger) return this;