From 283220111ff11fde17db84be96bf3ee54832ae32 Mon Sep 17 00:00:00 2001 From: Gordon Pedersen Date: Wed, 27 Sep 2023 09:50:06 +1000 Subject: [PATCH] Adjusted admin follow to accept [@]user@domain.tld format --- src/admin.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/src/admin.ts b/src/admin.ts index 638400d..6d3ac53 100644 --- a/src/admin.ts +++ b/src/admin.ts @@ -2,7 +2,7 @@ import { idsFromValue } from "./activitypub" import * as db from "./db" import { ACTOR, ADMIN_PASSWORD, ADMIN_USERNAME, BASE_URL } from "./env" import outbox from "./outbox" -import { fetchObject } from "./request" +import { activityMimeTypes, fetchObject } from "./request" export default (req: Request): Response | Promise | undefined => { const url = new URL(req.url) @@ -78,13 +78,31 @@ const create = async (req:Request, inReplyTo:string|null = null):Promise => { + let url + if(handle.startsWith('@')) handle = handle.substring(1) + try { + url = new URL(handle).href + } + catch { + // this is not a valid url. Probably a someone@domain.tld format + const [user, host] = handle.split('@') + const res = await fetch(`https://${host}/.well-known/webfinger/?resource=acct:${handle}`) + const webfinger = await res.json() + if(!webfinger.links) return new Response("", { status: 404 }) + const links:any[] = webfinger.links + const actorLink = links.find(l => l.rel === "self" && (activityMimeTypes.includes(l.type))) + if(!actorLink) return new Response("", { status: 404 }) + url = actorLink.href + } + console.log(`Follow ${url}`) + // send the follow request to the supplied actor return await outbox({ "@context": "https://www.w3.org/ns/activitystreams", type: "Follow", actor: ACTOR, - object: handle, - to: [handle, "https://www.w3.org/ns/activitystreams#Public"] + object: url, + to: [url, "https://www.w3.org/ns/activitystreams#Public"] }) }