Gracefully try host-meta if WebFinger fails

This commit is contained in:
Natty 2023-10-31 18:45:43 +01:00
parent b0e30dbe72
commit edd46a4a1b
No known key found for this signature in database
GPG key ID: BF6CB659ADEE60EC

View file

@ -80,11 +80,36 @@ Handle.prototype.webFinger = async function () {
return this; 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}` 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) if (!webFinger)
return this; return this;