Skip to content

Commit

Permalink
Implemented publishing update to actor
Browse files Browse the repository at this point in the history
  • Loading branch information
mike182uk committed Jul 25, 2024
1 parent f002146 commit bce8f19
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
7 changes: 7 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
Group,
Organization,
Service,
Update,
} from '@fedify/fedify';
import { federation } from '@fedify/fedify/x/hono';
import { Hono, Context } from 'hono';
Expand Down Expand Up @@ -42,6 +43,7 @@ import {
followDispatcher,
acceptDispatcher,
createDispatcher,
updateDispatcher,
} from './dispatchers';

import { followAction, inboxHandler, postPublishedWebhook, siteChangedWebhook } from './handlers';
Expand Down Expand Up @@ -135,6 +137,11 @@ fedify.setObjectDispatcher(
`/.ghost/activitypub/create/{id}`,
createDispatcher,
);
fedify.setObjectDispatcher(
Update,
`/.ghost/activitypub/update/{id}`,
updateDispatcher,
);

/** Hono */

Expand Down
13 changes: 13 additions & 0 deletions src/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
Create,
Note,
Activity,
Update,
} from '@fedify/fedify';
import { v4 as uuidv4 } from 'uuid';
import { addToList } from './kv-helpers';
Expand Down Expand Up @@ -282,6 +283,18 @@ export async function createDispatcher(
return Create.fromJsonLd(exists);
}

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

export async function noteDispatcher(
ctx: RequestContext<ContextData>,
data: Record<'id', string>,
Expand Down
24 changes: 22 additions & 2 deletions src/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
isActor,
Create,
Note,
Update,
} from '@fedify/fedify';
import { Context, Next } from 'hono';
import ky from 'ky';
Expand Down Expand Up @@ -148,13 +149,32 @@ export async function siteChangedWebhook(
const db = ctx.get('db');

const current = await db.get<PersonData>(['handle', handle]);

await db.set(['handle', handle], {
const updated = {
...current,
icon: settings.site.icon,
name: settings.site.title,
summary: settings.site.description,
}

await db.set(['handle', handle], updated);

// Publish activity
const apCtx = fedify.createContext(ctx.req.raw as Request, {
db,
globaldb: ctx.get('globaldb'),
});

const actor = await apCtx.getActor(handle);

const update = new Update({
id: apCtx.getObjectUri(Update, { id: uuidv4() }),
actor: actor?.id,
object: actor
});

await ctx.get('globaldb').set([update.id!.href], await update.toJsonLd());
await addToList(db, ['outbox'], update.id!.href);
await apCtx.sendActivity({ handle }, 'followers', update);
} catch (err) {
console.log(err);
}
Expand Down

0 comments on commit bce8f19

Please sign in to comment.