Skip to content

Commit

Permalink
Augment inbox endpoint rather than utilising inbox dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
mike182uk committed Jul 31, 2024
1 parent b9b9281 commit 8f9381b
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 51 deletions.
9 changes: 0 additions & 9 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@ import {
followersCounter,
followingDispatcher,
followingCounter,
inboxDispatcher,
inboxCounter,
outboxDispatcher,
outboxCounter,
articleDispatcher,
Expand Down Expand Up @@ -132,13 +130,6 @@ fedify
)
.setCounter(followingCounter);

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

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

// TODO: Check if sender is in our following and if so, bail out early

// Persist announce
const announceJson = await announce.toJsonLd();
ctx.data.globaldb.set([announce.id.href], announceJson);
Expand Down Expand Up @@ -276,45 +274,6 @@ 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
15 changes: 14 additions & 1 deletion src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ type GhostSiteSettings = {
}
}

type StoredThing = {
object: object | string;
}

async function getGhostSiteSettings(host: string): Promise<GhostSiteSettings> {
const settings = await ky
.get(`https://${host}/ghost/api/admin/site/`)
Expand Down Expand Up @@ -222,7 +226,16 @@ export async function inboxHandler(
let items: unknown[] = [];
for (const result of results) {
try {
const thing = await ctx.get('globaldb').get([result]);
const db = ctx.get('globaldb');
const thing = await db.get<StoredThing>([result]);

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

items.push(thing);
} catch (err) {
console.log(err);
Expand Down

0 comments on commit 8f9381b

Please sign in to comment.