Skip to content

Commit

Permalink
Add inbox dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
mike182uk committed Jul 30, 2024
1 parent 5157b8e commit 5dcbce2
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"typescript": "5.4.5"
},
"dependencies": {
"@fedify/fedify": "0.11.0-dev.223",
"@fedify/fedify": "0.11.3",
"@hono/node-server": "1.11.1",
"@sentry/node": "8.13.0",
"hono": "4.4.6",
Expand Down
9 changes: 9 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ import {
followersCounter,
followingDispatcher,
followingCounter,
inboxDispatcher,
inboxCounter,
outboxDispatcher,
outboxCounter,
articleDispatcher,
Expand Down Expand Up @@ -109,6 +111,13 @@ fedify
)
.setCounter(followingCounter);

fedify
.setInboxDispatcher(
'/.ghost/activitypub/inbox/{handle}',
inboxDispatcher,
)
.setCounter(inboxCounter);

fedify
.setOutboxDispatcher(
'/.ghost/activitypub/outbox/{handle}',
Expand Down
43 changes: 43 additions & 0 deletions src/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,49 @@ export async function followingCounter(
return results.length;
}

type StoredThing = {
object: object | string;
}

export async function inboxDispatcher(
ctx: RequestContext<ContextData>,
handle: string,
) {
console.log('Inbox Dispatcher');
const results = (await ctx.data.db.get<string[]>(['inbox'])) || [];
console.log(results);
let items: Activity[] = [];
for (const result of results) {
try {
const thing = await ctx.data.globaldb.get<StoredThing>([result]);

// If the object is a string, it's a URI, so we should to look it up
// in the globalDb. If it's not in the globalDb, we should just
// leave it as a string
if (thing && typeof thing.object === 'string') {
thing.object = await ctx.data.globaldb.get([thing.object]) ?? thing.object;
}

const activity = await Activity.fromJsonLd(thing);

items.push(activity);
} catch (err) {
console.log(err);
}
}
return {
items,
};
}

export async function inboxCounter(
ctx: RequestContext<ContextData>,
handle: string,
) {
const results = (await ctx.data.db.get<string[]>(['inbox'])) || [];
return results.length;
}

export async function outboxDispatcher(
ctx: RequestContext<ContextData>,
handle: string,
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/[email protected].0-dev.223":
version "0.11.0-dev.223"
resolved "https://registry.yarnpkg.com/@fedify/fedify/-/fedify-0.11.0-dev.223.tgz#a9796a2ecd1e1517af67c32d87252672eb0b31de"
integrity sha512-p9WqdgPvKmzX1rsmQhNUZv+Z9BNs5T9DZbCJ55XfS0N0T6xOUTHeMty6B8hWOYNdtatFIaWAaTIcwOIFBlqm1Q==
"@fedify/[email protected].3":
version "0.11.3"
resolved "https://registry.yarnpkg.com/@fedify/fedify/-/fedify-0.11.3.tgz#c45c38f6488eeaa657c4857bbdf205ef33c02a35"
integrity sha512-h6eO9COZ6C5gGXPXKzowgSU5aGrovn2ZLaSrglMNsComDyqRU4Ln+c7ey8p/g37EqHbmoD/Tp6RrnpTuFlpjKg==
dependencies:
"@deno/shim-crypto" "~0.3.1"
"@deno/shim-deno" "~0.18.0"
Expand Down

0 comments on commit 5dcbce2

Please sign in to comment.