Skip to content

Commit

Permalink
Use constants for common strings
Browse files Browse the repository at this point in the history
  • Loading branch information
mike182uk committed Jul 25, 2024
1 parent c8df524 commit 39f007a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 23 deletions.
5 changes: 3 additions & 2 deletions src/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<ContextData>,
handle: string,
) {
if (handle !== 'index') return null;
if (handle !== ACTOR_DEFAULT_HANDLE) return null;

const data = await getUserData(ctx, handle);

Expand All @@ -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);

Expand Down
3 changes: 2 additions & 1 deletion src/dispatchers.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>;
const handle = 'anything';

Expand Down
10 changes: 5 additions & 5 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
});
Expand All @@ -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',
Expand All @@ -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();
Expand All @@ -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);
}
Expand Down
23 changes: 8 additions & 15 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -48,10 +53,10 @@ export async function getUserData(ctx: RequestContext<ContextData>, 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),
Expand All @@ -61,18 +66,6 @@ export async function getUserData(ctx: RequestContext<ContextData>, 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;
Expand Down

0 comments on commit 39f007a

Please sign in to comment.