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

[WIP] Sending & Un-sending Likes #35

Merged
merged 6 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
26 changes: 25 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
Announce,
Context,
Like,
Undo,
} from '@fedify/fedify';
import { federation } from '@fedify/fedify/x/hono';
import { Hono, Context as HonoContext } from 'hono';
Expand All @@ -41,6 +42,10 @@ import {
followingCounter,
outboxDispatcher,
outboxCounter,
likedDispatcher,
likedCounter,
likeDispatcher,
undoDispatcher,
articleDispatcher,
noteDispatcher,
followDispatcher,
Expand All @@ -51,7 +56,7 @@ import {
handleLike
} from './dispatchers';

import { followAction, inboxHandler, postPublishedWebhook, siteChangedWebhook } from './handlers';
import { likeAction, unlikeAction, followAction, inboxHandler, postPublishedWebhook, siteChangedWebhook } from './handlers';

if (process.env.SENTRY_DSN) {
Sentry.init({ dsn: process.env.SENTRY_DSN });
Expand Down Expand Up @@ -142,6 +147,13 @@ fedify
)
.setCounter(outboxCounter);

fedify
.setLikedDispatcher(
'/.ghost/activitypub/liked/{handle}',
likedDispatcher,
)
.setCounter(likedCounter);

fedify.setObjectDispatcher(
Article,
`/.ghost/activitypub/article/{id}`,
Expand Down Expand Up @@ -172,6 +184,16 @@ fedify.setObjectDispatcher(
`/.ghost/activitypub/update/{id}`,
updateDispatcher,
);
fedify.setObjectDispatcher(
Like,
`/.ghost/activitypub/like/{id}`,
likeDispatcher,
);
fedify.setObjectDispatcher(
Undo,
`/.ghost/activitypub/undo/{id}`,
undoDispatcher,
);

/** Hono */

Expand Down Expand Up @@ -257,6 +279,8 @@ app.get('/.ghost/activitypub/inbox/:handle', inboxHandler);
app.post('/.ghost/activitypub/webhooks/post/published', postPublishedWebhook);
app.post('/.ghost/activitypub/webhooks/site/changed', siteChangedWebhook);
app.post('/.ghost/activitypub/actions/follow/:handle', followAction);
app.post('/.ghost/activitypub/actions/like/:id', likeAction);
app.post('/.ghost/activitypub/actions/unlike/:id', unlikeAction);

/** Federation wire up */

Expand Down
83 changes: 58 additions & 25 deletions src/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ import {
Object as APObject,
Recipient,
Like,
Undo,
} from '@fedify/fedify';
import { v4 as uuidv4 } from 'uuid';
import { addToList } from './kv-helpers';
import { ContextData } from './app';
import { ACTOR_DEFAULT_HANDLE } from './constants';
import { getUserData, getUserKeypair } from './user';
import { lookupActor } from './lookup-helpers';

export async function actorDispatcher(
ctx: RequestContext<ContextData>,
Expand Down Expand Up @@ -301,31 +303,6 @@ export async function inboxErrorHandler(
console.error(error);
}

async function lookupActor(ctx: RequestContext<ContextData>, url: string) {
try {
console.log('Looking up actor locally', url);
const local = await ctx.data.globaldb.get([url]);
return await APObject.fromJsonLd(local);
} catch (err) {
console.log('Error looking up actor locally', url);
console.log(err);
console.log('Looking up actor remotely', url);
const documentLoader = await ctx.getDocumentLoader({handle: 'index'});
try {
const remote = await lookupObject(url, {documentLoader});
if (isActor(remote)) {
await ctx.data.globaldb.set([url], await remote.toJsonLd());
return remote;
}
} catch (err) {
console.log('Error looking up actor remotely', url);
console.log(err)
return null;
}
}
return null;
}

function convertJsonLdToRecipient(result: any): Recipient {
return {
...result,
Expand Down Expand Up @@ -435,6 +412,38 @@ export async function outboxCounter(
return filterOutboxActivityUris(results).length;
}

export async function likedDispatcher(
ctx: RequestContext<ContextData>,
handle: string,
) {
console.log('Liked Dispatcher');
const results = (await ctx.data.db.get<string[]>(['liked'])) || [];
console.log(results);

let items: Like[] = [];
for (const result of results) {
try {
const thing = await ctx.data.globaldb.get([result]);
const activity = await Like.fromJsonLd(thing);
items.push(activity);
} catch (err) {
console.log(err);
}
}
return {
items: items.reverse(),
};
}

export async function likedCounter(
ctx: RequestContext<ContextData>,
handle: string,
) {
const results = (await ctx.data.db.get<string[]>(['liked'])) || [];

return results.length;
}

export async function articleDispatcher(
ctx: RequestContext<ContextData>,
data: Record<'id', string>,
Expand Down Expand Up @@ -506,3 +515,27 @@ export async function noteDispatcher(
}
return Note.fromJsonLd(exists);
}

export async function likeDispatcher(
ctx: RequestContext<ContextData>,
data: Record<'id', string>,
) {
const id = ctx.getObjectUri(Like, data);
const exists = await ctx.data.globaldb.get([id.href]);
if (!exists) {
return null;
}
return Like.fromJsonLd(exists);
}

export async function undoDispatcher(
ctx: RequestContext<ContextData>,
data: Record<'id', string>,
) {
const id = ctx.getObjectUri(Undo, data);
const exists = await ctx.data.globaldb.get([id.href]);
if (!exists) {
return null;
}
return Undo.fromJsonLd(exists);
}
Loading
Loading