Skip to content

Commit

Permalink
✨ Accept follow
Browse files Browse the repository at this point in the history
  • Loading branch information
BeiyanYunyi committed Oct 27, 2023
1 parent d95266a commit 507cda9
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
/* eslint-disable import/prefer-default-export */

export const GET = () =>
import { Env } from './types';

export const onRequestGet: PagesFunction<Env> = (ctx) =>
new Response(
JSON.stringify({
'@context': ['https://www.w3.org/ns/activitystreams', 'https://w3id.org/security/v1'],
Expand Down Expand Up @@ -37,9 +39,7 @@ export const GET = () =>
publicKey: {
id: 'https://blog.yunyi.beiyan.us/api/activitypub/actor#main-key',
owner: 'https://blog.yunyi.beiyan.us/api/activitypub/actor',
publicKeyPem: `-----BEGIN PUBLIC KEY-----
MCowBQYDK2VwAyEAK32mZ6NHWvErlVDNipED0uv0WoxpHvcL2OWCQ/mCLBw=
-----END PUBLIC KEY-----`,
publicKeyPem: JSON.parse(ctx.env.PUBLIC_KEY),
},
}),
{ headers: { 'Content-Type': 'application/activity+json' } },
Expand Down
15 changes: 12 additions & 3 deletions functions/api/activitypub/inbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ import type { AP } from 'activitypub-core-types';
import { Kysely } from 'kysely';
import { D1Dialect } from 'kysely-d1';
import type { Database, Env } from './types';
import { getPrivateKey } from '../../src/utils/getKey';
import { signRequest } from '../../src/utils/http-signing';

const handleFollow = async (body: AP.Follow, db: Kysely<Database>) => {
const handleFollow = async (body: AP.Follow, db: Kysely<Database>, env: Env) => {
if (Array.isArray(body.actor)) throw new Error('Not Implemented');
let aid = '';
if (typeof body.actor === 'string') aid = body.actor;
Expand All @@ -19,7 +21,7 @@ const handleFollow = async (body: AP.Follow, db: Kysely<Database>) => {
oc.column('actorId').doUpdateSet({ inbox: info.inbox as unknown as string }),
)
.execute();
await fetch(info.inbox as unknown as string, {
const acceptReq = new Request(info.inbox as unknown as string, {
method: 'post',
headers: { 'Content-Type': 'application/activity+json', Accept: 'application/activity+json' },
body: JSON.stringify({
Expand All @@ -37,6 +39,13 @@ const handleFollow = async (body: AP.Follow, db: Kysely<Database>) => {
},
}),
});
const privKey = await getPrivateKey(env);
await signRequest(
acceptReq,
privKey,
new URL('https://blog.yunyi.beiyan.us/api/activitypub/actor'),
);
await fetch(acceptReq);
return new Response('Ok');
};

Expand All @@ -59,7 +68,7 @@ export const onRequestPost: PagesFunction<Env> = async (ctx) => {
if (!['Follow', 'Undo'].includes(body.type)) throw new Error('Not Implemented');
switch (body.type) {
case 'Follow':
return handleFollow(body as AP.Follow, db);
return handleFollow(body as AP.Follow, db, ctx.env);
case 'Undo':
return handleUnfollow(body as AP.Undo, db);
default:
Expand Down
5 changes: 0 additions & 5 deletions functions/api/genKeyPair/test.ts

This file was deleted.

6 changes: 6 additions & 0 deletions functions/src/utils/getKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Env } from '../../api/activitypub/types';
import { importPrivKey, importPublicKey } from './key-ops';

export const getPublicKey = async (env: Env) => importPublicKey(JSON.parse(env.PUBLIC_KEY));

export const getPrivateKey = async (env: Env) => importPrivKey(JSON.parse(env.PRIV_KEY));
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
"@assets/*": ["src/assets/*"]
}
},
"include": ["src/**/*.astro", "src/**/*.ts", "src/**/*.tsx", "uno.config.ts", "astro.config.ts"],
"include": ["src/**/*.astro", "src/**/*.ts", "src/**/*.tsx", "uno.config.ts", "astro.config.ts"]
// "references": [{ "path": "./functions/tsconfig.json" }]
}

0 comments on commit 507cda9

Please sign in to comment.