From e98d6f75c8c2275dcccb93ffef89e4f7632f009b Mon Sep 17 00:00:00 2001 From: Moon Man Date: Wed, 27 Dec 2023 11:17:09 -0500 Subject: [PATCH] send accept to follow request --- src/activity.ts | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/src/activity.ts b/src/activity.ts index 5ede2ed..0eac8be 100644 --- a/src/activity.ts +++ b/src/activity.ts @@ -3,7 +3,7 @@ import { Article } from "./article.js"; import { User, getByActor } from "./user.js"; import { fillRoute } from "./router.js"; import { streamToString, hashDigest } from "./util.js"; -import { signedFetch, SignedInit, getActor} from "./net.js"; +import { signedFetch, SignedInit, getActor } from "./net.js"; import { getById as getUserById, getKeyId } from "./user.js"; import parser from "activitypub-http-signatures"; import type { Request, Response } from "express"; @@ -28,8 +28,8 @@ export const handleInboxPost = async (req: Request, res: Response) => { } catch (e) { console.warn("body json parse failed"); - console.error(typeof req.body); - console.error(req.body, body); + console.error(typeof req.body); + console.error(req.body, body); res.status(403).end(); return; } @@ -40,7 +40,7 @@ export const handleInboxPost = async (req: Request, res: Response) => { if (actor) { const followerUrl: string = activity.actor; - const signer = await getUserById(1); + const signer = actor; if (!signer) { res.status(500).end(); @@ -75,10 +75,9 @@ export const handleInboxPost = async (req: Request, res: Response) => { return; } - await addFollower(actor.id, follower.id, follower.preferredUsername, follower.name, follower.inbox, follower.endpoints?.sharedInbox); + await sendAccept(actor, follower.id, follower.inbox); - const response = { - }; + await addFollower(actor.id, follower.id, follower.preferredUsername, follower.name, follower.inbox, follower.endpoints?.sharedInbox); console.log("Done handling inbox POST follow request"); res.status(200); @@ -98,7 +97,7 @@ export const handleInboxPost = async (req: Request, res: Response) => { res.status(200).end(); } } - catch(e) { + catch (e) { console.warn("Failed to handle inbox POST request.", e); res.status(500).end(); } @@ -188,6 +187,26 @@ export const createArticleObject = (article: Article, nickname: string) => { return obj; }; +export const sendAccept = async (user: User, follower: string, inbox: string) => { + const actor = fillRoute("actor", user.nickname); + + const activity = { + id: fillRoute("activity", Date.now() * 1_000_000), + "@context": CONTEXT, + type: "Accept", + actor, + object: follower + }; + + const payload = JSON.stringify(activity, null, 4); + const hash = hashDigest(payload); + + const init: RequestInit = { method: "POST", body: payload }; + const init2 = { hash, privateKey: user.private_key, keyId: getKeyId(user.nickname) }; + + await signedFetch(inbox, init, init2); +}; + export const sendAll = async (keyId: string, privateKey: string, activity: Record | string, inboxes: string[]) => { activity = typeof activity === "string" ? activity