Skip to content

Commit

Permalink
Merge branch 'main' into refactor/use-constants
Browse files Browse the repository at this point in the history
  • Loading branch information
mike182uk authored Jul 31, 2024
2 parents 5df89c8 + 4cffa0c commit c4d30fa
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 53 deletions.
3 changes: 1 addition & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ jobs:
image: $IMAGE_NAME:$DOCKER_METADATA_OUTPUT_VERSION" > docker-compose.ci.yml
- name: "Run tests"
run: |
docker compose -f docker-compose.testing.yml -f docker-compose.ci.yml up --abort-on-container-exit --exit-code-from activitypub
run: yarn test

- name: Login to GAR
uses: docker/login-action@v3
Expand Down
31 changes: 0 additions & 31 deletions docker-compose.testing.yml

This file was deleted.

9 changes: 6 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
"main": "src/app.ts",
"type": "module",
"scripts": {
"dev": "docker compose up",
"dev": "docker compose up -d --no-recreate",
"stop": "docker compose down",
"fix": "docker compose rm activitypub nginx -sf && docker compose build activitypub nginx",
"logs": "docker compose logs activitypub -f",
"test": "yarn dev && docker compose exec activitypub yarn test:all",
"test:types": "tsc --noEmit",
"test:unit": "c8 --src src --all --reporter text --reporter cobertura mocha -r tsx './src/**/*.unit.test.ts'",
"test:integration": "c8 --src src --all --reporter text --reporter cobertura mocha -r tsx './src/**/*.integration.test.ts'",
"test:code": "c8 --src src --all --reporter text --reporter cobertura mocha -r tsx './src/**/*.test.ts'",
"test:all": "yarn test:types && yarn test:code",
"test": "docker compose -f docker-compose.testing.yml up --abort-on-container-exit --exit-code-from activitypub",
"lint:code": "eslint *.js lib/ --ext .js --cache",
"lint": "yarn lint:code"
},
Expand All @@ -30,7 +33,7 @@
"typescript": "5.4.5"
},
"dependencies": {
"@fedify/fedify": "0.12.1",
"@fedify/fedify": "0.13.0-dev.318",
"@hono/node-server": "1.11.1",
"@sentry/node": "8.13.0",
"hono": "4.4.6",
Expand Down
39 changes: 30 additions & 9 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
Article,
Accept,
Object as ActivityPubObject,
Federation,
createFederation,
Follow,
KvKey,
KvStore,
Expand All @@ -15,9 +15,10 @@ import {
Organization,
Service,
Update,
Context,
} from '@fedify/fedify';
import { federation } from '@fedify/fedify/x/hono';
import { Hono, Context } from 'hono';
import { Hono, Context as HonoContext } from 'hono';
import { cors } from 'hono/cors';
import { behindProxy } from 'x-forwarded-fetch';
import { configure, getConsoleSink } from '@logtape/logtape';
Expand Down Expand Up @@ -65,30 +66,50 @@ export type ContextData = {

const fedifyKv = await KnexKvStore.create(client, 'key_value');

export const fedify = new Federation<ContextData>({
export const fedify = createFederation<ContextData>({
kv: fedifyKv,
treatHttps: true,
});

export const db = await KnexKvStore.create(client, 'key_value');

/** Fedify */

/**
* Fedify does not pass the correct context object when running outside of the request context
* for example in the context of the Inbox Queue - so we need to wrap handlers with this.
*/
function ensureCorrectContext<B, R>(fn: (ctx: Context<ContextData>, b: B) => Promise<R>) {
return async function (ctx: Context<any>, b: B) {
const host = ctx.host;
if (!ctx.data) {
(ctx as any).data = {};
}
if (!ctx.data.globaldb) {
ctx.data.globaldb = db;
}
if (!ctx.data.db) {
ctx.data.db = scopeKvStore(db, ['sites', host]);
}
return fn(ctx, b);
}
}

fedify
// actorDispatcher uses RequestContext so doesn't need the ensureCorrectContext wrapper
.setActorDispatcher('/.ghost/activitypub/users/{handle}', actorDispatcher)
.setKeyPairsDispatcher(keypairDispatcher);
.setKeyPairsDispatcher(ensureCorrectContext(keypairDispatcher));

const inboxListener = fedify.setInboxListeners(
'/.ghost/activitypub/inbox/{handle}',
'/.ghost/activitypub/inbox',
);

inboxListener
.on(Follow, handleFollow)
.on(Follow, ensureCorrectContext(handleFollow))
.onError(inboxErrorHandler)
.on(Accept, handleAccept)
.on(Accept, ensureCorrectContext(handleAccept))
.onError(inboxErrorHandler)
.on(Create, handleCreate)
.on(Create, ensureCorrectContext(handleCreate))
.onError(inboxErrorHandler);

fedify
Expand Down Expand Up @@ -227,7 +248,7 @@ app.post('/.ghost/activitypub/actions/follow/:handle', followAction);
app.use(
federation(
fedify,
(ctx: Context<{ Variables: HonoContextVariables }>): ContextData => {
(ctx: HonoContext<{ Variables: HonoContextVariables }>): ContextData => {
return {
db: ctx.get('db'),
globaldb: ctx.get('globaldb'),
Expand Down
14 changes: 10 additions & 4 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ export async function postPublishedWebhook(
actor,
object: article,
id: apCtx.getObjectUri(Create, { id: uuidv4() }),
to: apCtx.getFollowersUri(ACTOR_DEFAULT_HANDLE),
to: PUBLIC_COLLECTION,
cc: apCtx.getFollowersUri('index'),
});
try {
await article.toJsonLd();
Expand All @@ -140,7 +141,9 @@ 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: ACTOR_DEFAULT_HANDLE }, 'followers', create);
await apCtx.sendActivity({ handle: ACTOR_DEFAULT_HANDLE }, 'followers', create, {
preferSharedInbox: true
});
} catch (err) {
console.log(err);
}
Expand Down Expand Up @@ -189,12 +192,15 @@ export async function siteChangedWebhook(
id: apCtx.getObjectUri(Update, { id: uuidv4() }),
actor: actor?.id,
to: PUBLIC_COLLECTION,
object: actor
object: actor,
cc: apCtx.getFollowersUri('index'),
});

await ctx.get('globaldb').set([update.id!.href], await update.toJsonLd());
await addToList(db, ['outbox'], update.id!.href);
await apCtx.sendActivity({ handle }, 'followers', update);
await apCtx.sendActivity({ handle }, 'followers', update, {
preferSharedInbox: true
});
} catch (err) {
console.log(err);
}
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,10 @@
resolved "https://registry.yarnpkg.com/@fastify/busboy/-/busboy-2.1.1.tgz#b9da6a878a371829a0502c9b6c1c143ef6663f4d"
integrity sha512-vBZP4NlzfOlerQTnba4aqZoMhE/a9HY7HRqoOPaETQcSQuWEIyZMHGfVu6w9wGtGK5fED5qRs2DteVCjOH60sA==

"@fedify/fedify@0.12.1":
version "0.12.1"
resolved "https://registry.yarnpkg.com/@fedify/fedify/-/fedify-0.12.1.tgz#6aa0decaa8d435a0e597bf689468c4a32502ce9c"
integrity sha512-d0NLlraraq2A3rSnhWAvZd4tk0UDYgK7Q8/SFkLBWYmVNLsTTGby4R6ejo3GpOw10fjRux2KTJcoHlvX6lgToQ==
"@fedify/fedify@0.13.0-dev.318":
version "0.13.0-dev.318"
resolved "https://registry.yarnpkg.com/@fedify/fedify/-/fedify-0.13.0-dev.318.tgz#96146859d01888cb163908049bdb4d928ade0938"
integrity sha512-xzT3iz5IA9/9Wj30X+o0t0INDeLzn/7dq75nw5u966myzT0Ejq5L//fKNmVB04rFE6vx0+DWmZtpdxOL26YoNw==
dependencies:
"@deno/shim-crypto" "~0.3.1"
"@deno/shim-deno" "~0.18.0"
Expand Down

0 comments on commit c4d30fa

Please sign in to comment.