Skip to content

Commit

Permalink
Use constants for common strings (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
mike182uk authored Jul 31, 2024
1 parent 4cffa0c commit 55dcc09
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 12 deletions.
5 changes: 3 additions & 2 deletions src/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,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 @@ -30,7 +31,7 @@ export async function actorDispatcher(
}

export async function keypairDispatcher(ctx: Context<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
8 changes: 4 additions & 4 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,7 +121,7 @@ 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,
Expand All @@ -141,7 +141,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, {
preferSharedInbox: true
});
} catch (err) {
Expand Down
15 changes: 10 additions & 5 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ import {
Context,
} 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 @@ -49,10 +54,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 @@ -67,14 +72,14 @@ export async function getUserData(ctx: RequestContext<ContextData>, handle: stri
name: data.name,
summary: data.summary,
preferredUsername: data.preferredUsername,
icon: 'https://ghost.org/favicon.ico',
icon: ACTOR_DEFAULT_ICON,
inbox: data.inbox.href,
outbox: data.outbox.href,
following: data.following.href,
followers: data.followers.href,
};

await ctx.data.db.set(['handle', handle], data);
await ctx.data.db.set(['handle', handle], dataToStore);

return data;
}
Expand Down

0 comments on commit 55dcc09

Please sign in to comment.