Skip to content

Commit

Permalink
Added initital handling for Announce activities
Browse files Browse the repository at this point in the history
  • Loading branch information
allouis committed Jul 29, 2024
1 parent 7faf27d commit 8c3b628
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Organization,
Service,
Update,
Announce,
} from '@fedify/fedify';
import { federation } from '@fedify/fedify/x/hono';
import { Hono, Context } from 'hono';
Expand Down Expand Up @@ -44,6 +45,7 @@ import {
acceptDispatcher,
createDispatcher,
updateDispatcher,
handleAnnounce,
} from './dispatchers';

import { followAction, inboxHandler, postPublishedWebhook, siteChangedWebhook } from './handlers';
Expand Down Expand Up @@ -89,6 +91,8 @@ inboxListener
.on(Accept, handleAccept)
.onError(inboxErrorHandler)
.on(Create, handleCreate)
.onError(inboxErrorHandler)
.on(Announce, handleAnnounce)
.onError(inboxErrorHandler);

fedify
Expand Down
42 changes: 42 additions & 0 deletions src/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Activity,
Update,
Context,
Announce,
} from '@fedify/fedify';
import { v4 as uuidv4 } from 'uuid';
import { addToList } from './kv-helpers';
Expand Down Expand Up @@ -136,6 +137,47 @@ export async function handleCreate(
await addToList(ctx.data.db, ['inbox'], create.id.href);
}

export async function handleAnnounce(
ctx: Context<ContextData>,
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<ContextData>,
error: unknown,
Expand Down

0 comments on commit 8c3b628

Please sign in to comment.