Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added handler for incoming likes #33

Merged
merged 3 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
Update,
Announce,
Context,
Like,
} from '@fedify/fedify';
import { federation } from '@fedify/fedify/x/hono';
import { Hono, Context as HonoContext } from 'hono';
Expand Down Expand Up @@ -47,6 +48,7 @@ import {
createDispatcher,
updateDispatcher,
handleAnnounce,
handleLike
} from './dispatchers';

import { followAction, inboxHandler, postPublishedWebhook, siteChangedWebhook } from './handlers';
Expand Down Expand Up @@ -115,6 +117,8 @@ inboxListener
.on(Create, ensureCorrectContext(handleCreate))
.onError(inboxErrorHandler)
.on(Announce, ensureCorrectContext(handleAnnounce))
.onError(inboxErrorHandler)
.on(Like, ensureCorrectContext(handleLike))
.onError(inboxErrorHandler);

fedify
Expand Down
64 changes: 64 additions & 0 deletions src/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
Actor,
Object as APObject,
Recipient,
Like,
} from '@fedify/fedify';
import { v4 as uuidv4 } from 'uuid';
import { addToList } from './kv-helpers';
Expand Down Expand Up @@ -229,6 +230,69 @@ export async function handleAnnounce(
await addToList(ctx.data.db, ['inbox'], announce.id.href);
}

export async function handleLike(
ctx: Context<ContextData>,
like: Like,
) {
console.log('Handling Like');

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

if (!like.objectId) {
console.log('Invalid Like - no object id');
return;
}

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

if (sender === null || sender.id === null) {
console.log('Sender missing, exit early');
return;
}

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

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

object = await lookupObject(like.objectId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can be simplified:

Suggested change
object = await lookupObject(like.objectId);
object = await like.getObject();

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dahlia thats updated in 81816ea

}

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

if (object && !object.id) {
console.log('Invalid Like - could not find object id');
return;
}

// Persist like
const likeJson = await like.toJsonLd();
ctx.data.globaldb.set([like.id.href], likeJson);

// 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 to inbox
await addToList(ctx.data.db, ['inbox'], like.id.href);
}

export async function inboxErrorHandler(
ctx: Context<ContextData>,
error: unknown,
Expand Down
Loading