send accept to follow request

This commit is contained in:
Moon Man 2023-12-27 11:17:09 -05:00
parent 56c77e078f
commit e98d6f75c8
1 changed files with 27 additions and 8 deletions

View File

@ -3,7 +3,7 @@ import { Article } from "./article.js";
import { User, getByActor } from "./user.js"; import { User, getByActor } from "./user.js";
import { fillRoute } from "./router.js"; import { fillRoute } from "./router.js";
import { streamToString, hashDigest } from "./util.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 { getById as getUserById, getKeyId } from "./user.js";
import parser from "activitypub-http-signatures"; import parser from "activitypub-http-signatures";
import type { Request, Response } from "express"; import type { Request, Response } from "express";
@ -28,8 +28,8 @@ export const handleInboxPost = async (req: Request, res: Response) => {
} }
catch (e) { catch (e) {
console.warn("body json parse failed"); console.warn("body json parse failed");
console.error(typeof req.body); console.error(typeof req.body);
console.error(req.body, body); console.error(req.body, body);
res.status(403).end(); res.status(403).end();
return; return;
} }
@ -40,7 +40,7 @@ export const handleInboxPost = async (req: Request, res: Response) => {
if (actor) { if (actor) {
const followerUrl: string = activity.actor; const followerUrl: string = activity.actor;
const signer = await getUserById(1); const signer = actor;
if (!signer) { if (!signer) {
res.status(500).end(); res.status(500).end();
@ -75,10 +75,9 @@ export const handleInboxPost = async (req: Request, res: Response) => {
return; 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"); console.log("Done handling inbox POST follow request");
res.status(200); res.status(200);
@ -98,7 +97,7 @@ export const handleInboxPost = async (req: Request, res: Response) => {
res.status(200).end(); res.status(200).end();
} }
} }
catch(e) { catch (e) {
console.warn("Failed to handle inbox POST request.", e); console.warn("Failed to handle inbox POST request.", e);
res.status(500).end(); res.status(500).end();
} }
@ -188,6 +187,26 @@ export const createArticleObject = (article: Article, nickname: string) => {
return obj; 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, any> | string, inboxes: string[]) => { export const sendAll = async (keyId: string, privateKey: string, activity: Record<string, any> | string, inboxes: string[]) => {
activity = typeof activity === "string" activity = typeof activity === "string"
? activity ? activity