diff --git a/src/app.ts b/src/app.ts index 497ed796..dca93a9b 100644 --- a/src/app.ts +++ b/src/app.ts @@ -14,6 +14,7 @@ import { Group, Organization, Service, + Update, } from '@fedify/fedify'; import { federation } from '@fedify/fedify/x/hono'; import { Hono, Context } from 'hono'; @@ -42,6 +43,7 @@ import { followDispatcher, acceptDispatcher, createDispatcher, + updateDispatcher, } from './dispatchers'; import { followAction, inboxHandler, postPublishedWebhook, siteChangedWebhook } from './handlers'; @@ -135,6 +137,11 @@ fedify.setObjectDispatcher( `/.ghost/activitypub/create/{id}`, createDispatcher, ); +fedify.setObjectDispatcher( + Update, + `/.ghost/activitypub/update/{id}`, + updateDispatcher, +); /** Hono */ diff --git a/src/dispatchers.ts b/src/dispatchers.ts index e374ef93..229299f8 100644 --- a/src/dispatchers.ts +++ b/src/dispatchers.ts @@ -8,6 +8,7 @@ import { Create, Note, Activity, + Update, } from '@fedify/fedify'; import { v4 as uuidv4 } from 'uuid'; import { addToList } from './kv-helpers'; @@ -282,6 +283,18 @@ export async function createDispatcher( return Create.fromJsonLd(exists); } +export async function updateDispatcher( + ctx: RequestContext, + 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, data: Record<'id', string>, diff --git a/src/handlers.ts b/src/handlers.ts index ff95eef3..41bd49f9 100644 --- a/src/handlers.ts +++ b/src/handlers.ts @@ -6,6 +6,7 @@ import { isActor, Create, Note, + Update, } from '@fedify/fedify'; import { Context, Next } from 'hono'; import ky from 'ky'; @@ -148,13 +149,32 @@ export async function siteChangedWebhook( const db = ctx.get('db'); const current = await db.get(['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); }