Skip to content

Commit

Permalink
Updated announce handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mike182uk committed Jul 30, 2024
1 parent b1748e8 commit 5157b8e
Showing 1 changed file with 42 additions and 18 deletions.
60 changes: 42 additions & 18 deletions src/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,39 +142,63 @@ export async function handleAnnounce(
announce: Announce,
) {
console.log('Handling Announce');
const announceJson = await announce.toJsonLd();
console.log(announceJson);

// Validate announce
if (!announce.id) {
console.log('Invalid Announce - no id');
return;
}

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');

// Validate sender
const sender = await announce.getActor(ctx);

if (sender === null || sender.id === null) {
console.log('Sender missing, exit early');
return;
}
if (!object.id) {
console.log('Invalid Announce - could not find object id');
return;

// Lookup announced object - If not found in globalDb, perform network lookup
let object = null;
let existing = await ctx.data.globaldb.get([announce.objectId.href]) ?? null;

if (!existing) {
console.log('Object not found in globalDb, performing network lookup');

object = await lookupObject(announce.objectId);
}
if (!announce.id) {
console.log('Invalid Announce - no id');

// Validate object
if (!existing && !object) {
console.log('Invalid Announce - could not find object');
return;
}

const sender = await announce.getActor(ctx);
if (sender === null || sender.id === null) {
console.log('Sender missing, exit early');
if (object && !object.id) {
console.log('Invalid Announce - could not find object id');
return;
}

// TODO Check Sender is in our following
// 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);
try {
ctx.data.globaldb.set([object.id.href], object.toJsonLd());
} catch (err) {
console.error('Could not store announced object');

// Persist object if not already persisted
if (!existing && object && object.id) {
console.log('Storing object in globalDb');

const objectJson = await object.toJsonLd();

ctx.data.globaldb.set([object.id.href], objectJson);
}

// Add announce to inbox
await addToList(ctx.data.db, ['inbox'], announce.id.href);
}

Expand Down

0 comments on commit 5157b8e

Please sign in to comment.