diff --git a/src/app.ts b/src/app.ts index dca93a9b..a63d5ea2 100644 --- a/src/app.ts +++ b/src/app.ts @@ -15,6 +15,7 @@ import { Organization, Service, Update, + Announce, } from '@fedify/fedify'; import { federation } from '@fedify/fedify/x/hono'; import { Hono, Context } from 'hono'; @@ -44,6 +45,7 @@ import { acceptDispatcher, createDispatcher, updateDispatcher, + handleAnnounce, } from './dispatchers'; import { followAction, inboxHandler, postPublishedWebhook, siteChangedWebhook } from './handlers'; @@ -89,6 +91,8 @@ inboxListener .on(Accept, handleAccept) .onError(inboxErrorHandler) .on(Create, handleCreate) + .onError(inboxErrorHandler) + .on(Announce, handleAnnounce) .onError(inboxErrorHandler); fedify diff --git a/src/dispatchers.ts b/src/dispatchers.ts index 4b1ee1ae..f4dd2ec3 100644 --- a/src/dispatchers.ts +++ b/src/dispatchers.ts @@ -10,6 +10,7 @@ import { Activity, Update, Context, + Announce, } from '@fedify/fedify'; import { v4 as uuidv4 } from 'uuid'; import { addToList } from './kv-helpers'; @@ -136,6 +137,47 @@ export async function handleCreate( await addToList(ctx.data.db, ['inbox'], create.id.href); } +export async function handleAnnounce( + ctx: Context, + announce: Announce, +) { + console.log('Handling Announce'); + const announceJson = await announce.toJsonLd(); + console.log(announceJson); + if (!announce.objectId) { + console.log('Invalid Announce - no object id'); + return; + } + const object = await lookupObject(announce.objectId); + if (!object) { + console.log('Invalid Announce - could not find object'); + return; + } + if (!object.id) { + console.log('Invalid Announce - could not find object id'); + return; + } + if (!announce.id) { + console.log('Invalid Announce - no id'); + return; + } + + const sender = await announce.getActor(ctx); + if (sender === null || sender.id === null) { + console.log('Sender missing, exit early'); + return; + } + + // TODO Check Sender is in our following + ctx.data.globaldb.set([announce.id.href], announceJson); + try { + ctx.data.globaldb.set([object.id.href], object.toJsonLd()); + } catch (err) { + console.error('Could not store announced object'); + } + await addToList(ctx.data.db, ['inbox'], announce.id.href); +} + export async function inboxErrorHandler( ctx: Context, error: unknown,