From 39f007a730004779ebf5b592e8b211d34b761fbe Mon Sep 17 00:00:00 2001 From: Michael Barrett Date: Thu, 25 Jul 2024 15:57:36 +0100 Subject: [PATCH] Use constants for common strings --- src/dispatchers.ts | 5 +++-- src/dispatchers.unit.test.ts | 3 ++- src/handlers.ts | 10 +++++----- src/user.ts | 23 ++++++++--------------- 4 files changed, 18 insertions(+), 23 deletions(-) diff --git a/src/dispatchers.ts b/src/dispatchers.ts index 229299f8..2672e71f 100644 --- a/src/dispatchers.ts +++ b/src/dispatchers.ts @@ -13,13 +13,14 @@ import { import { v4 as uuidv4 } from 'uuid'; import { addToList } from './kv-helpers'; import { ContextData } from './app'; +import { ACTOR_DEFAULT_HANDLE } from './constants'; import { getUserData, getUserKeypair } from './user'; export async function actorDispatcher( ctx: RequestContext, handle: string, ) { - if (handle !== 'index') return null; + if (handle !== ACTOR_DEFAULT_HANDLE) return null; const data = await getUserData(ctx, handle); @@ -29,7 +30,7 @@ export async function actorDispatcher( } export async function keypairDispatcher(ctx: ContextData, handle: string) { - if (handle !== 'index') return []; + if (handle !== ACTOR_DEFAULT_HANDLE) return []; const data = await getUserKeypair(ctx, handle); diff --git a/src/dispatchers.unit.test.ts b/src/dispatchers.unit.test.ts index 32f84bc1..c424590a 100644 --- a/src/dispatchers.unit.test.ts +++ b/src/dispatchers.unit.test.ts @@ -19,10 +19,11 @@ import { createDispatcher, } from './dispatchers'; import { RequestContext } from '@fedify/fedify'; +import { ACTOR_DEFAULT_HANDLE } from './constants'; describe('dispatchers', function () { describe('actorDispatcher', function () { - it('returns null if the handle is not "index"', async function () { + it(`returns null if the handle is not "${ACTOR_DEFAULT_HANDLE}"`, async function () { const ctx = {} as RequestContext; const handle = 'anything'; diff --git a/src/handlers.ts b/src/handlers.ts index 4da448c1..9b63396d 100644 --- a/src/handlers.ts +++ b/src/handlers.ts @@ -85,7 +85,7 @@ export async function followAction( db: ctx.get('db'), globaldb: ctx.get('globaldb'), }); - const actor = await apCtx.getActor('index'); // TODO This should be the actor making the request + const actor = await apCtx.getActor(ACTOR_DEFAULT_HANDLE); // TODO This should be the actor making the request const followId = apCtx.getObjectUri(Follow, { id: uuidv4(), }); @@ -97,7 +97,7 @@ export async function followAction( const followJson = await follow.toJsonLd(); ctx.get('globaldb').set([follow.id!.href], followJson); - apCtx.sendActivity({ handle: 'index' }, actorToFollow, follow); + apCtx.sendActivity({ handle: ACTOR_DEFAULT_HANDLE }, actorToFollow, follow); return new Response(JSON.stringify(followJson), { headers: { 'Content-Type': 'application/activity+json', @@ -121,12 +121,12 @@ export async function postPublishedWebhook( data?.post?.current, ); if (article) { - const actor = await apCtx.getActor('index'); + const actor = await apCtx.getActor(ACTOR_DEFAULT_HANDLE); const create = new Create({ actor, object: article, id: apCtx.getObjectUri(Create, { id: uuidv4() }), - to: apCtx.getFollowersUri('index'), + to: apCtx.getFollowersUri(ACTOR_DEFAULT_HANDLE), }); try { await article.toJsonLd(); @@ -140,7 +140,7 @@ export async function postPublishedWebhook( .get('globaldb') .set([article.id!.href], await article.toJsonLd()); await addToList(ctx.get('db'), ['outbox'], create.id!.href); - await apCtx.sendActivity({ handle: 'index' }, 'followers', create); + await apCtx.sendActivity({ handle: ACTOR_DEFAULT_HANDLE }, 'followers', create); } catch (err) { console.log(err); } diff --git a/src/user.ts b/src/user.ts index ce074854..f4fcfc84 100644 --- a/src/user.ts +++ b/src/user.ts @@ -6,6 +6,11 @@ import { importJwk, } from '@fedify/fedify'; import { ContextData } from './app'; +import { + ACTOR_DEFAULT_ICON, + ACTOR_DEFAULT_NAME, + ACTOR_DEFAULT_SUMMARY +} from './constants'; export type PersonData = { id: string; @@ -48,10 +53,10 @@ export async function getUserData(ctx: RequestContext, handle: stri const data = { id: ctx.getActorUri(handle), - name: `Local Ghost site`, - summary: 'This is a summary', + name: ACTOR_DEFAULT_NAME, + summary: ACTOR_DEFAULT_SUMMARY, preferredUsername: handle, - icon: new Image({ url: new URL('https://ghost.org/favicon.ico') }), + icon: new Image({ url: new URL(ACTOR_DEFAULT_ICON) }), inbox: ctx.getInboxUri(handle), outbox: ctx.getOutboxUri(handle), following: ctx.getFollowingUri(handle), @@ -61,18 +66,6 @@ export async function getUserData(ctx: RequestContext, handle: stri ), }; - const dataToStore: PersonData = { - id: data.id.href, - name: data.name, - summary: data.summary, - preferredUsername: data.preferredUsername, - icon: 'https://ghost.org/favicon.ico', - inbox: data.inbox.href, - outbox: data.outbox.href, - following: data.following.href, - followers: data.followers.href, - }; - await ctx.data.db.set(['handle', handle], data); return data;