diff --git a/src/handlers.ts b/src/handlers.ts index 0b20b6c..2533fba 100644 --- a/src/handlers.ts +++ b/src/handlers.ts @@ -570,7 +570,7 @@ export async function getSiteDataHandler( // This is to ensure that the actor exists - e.g. for a brand new a site await getUserData(apCtx, handle); - await updateSiteActor(apCtx, getSiteSettings, host); + await updateSiteActor(apCtx, getSiteSettings); return new Response(JSON.stringify(site), { status: 200, @@ -595,7 +595,7 @@ export async function siteChangedWebhook( logger, }); - await updateSiteActor(apCtx, getSiteSettings, host); + await updateSiteActor(apCtx, getSiteSettings); } catch (err) { ctx.get('logger').error('Site changed webhook failed: {error}', { error: err, diff --git a/src/helpers/activitypub/actor.ts b/src/helpers/activitypub/actor.ts index 5d014d3..f103a27 100644 --- a/src/helpers/activitypub/actor.ts +++ b/src/helpers/activitypub/actor.ts @@ -81,9 +81,8 @@ export async function updateSiteActor( getSiteSettings: (host: string) => Promise<{ site: { icon: string; title: string; description: string }; }>, - host: string, ) { - const settings = await getSiteSettings(host); + const settings = await getSiteSettings(apCtx.host); const handle = ACTOR_DEFAULT_HANDLE; const current = await getUserData(apCtx, handle); diff --git a/src/helpers/activitypub/actor.unit.test.ts b/src/helpers/activitypub/actor.unit.test.ts index 1cb4297..b3fc110 100644 --- a/src/helpers/activitypub/actor.unit.test.ts +++ b/src/helpers/activitypub/actor.unit.test.ts @@ -271,6 +271,7 @@ describe('updateSiteActor', () => { .fn() .mockReturnValue(new URL('https://example.com/followers')), sendActivity: vi.fn(), + host: 'example.com', } as unknown as RequestContext; } @@ -287,15 +288,17 @@ describe('updateSiteActor', () => { following: 'https://example.com/following', followers: 'https://example.com/followers', liked: 'https://example.com/liked', - url: 'https://example.com', + url: 'https://example.com/', }), set: vi.fn(), - } as unknown as KvStore; + delete: vi.fn(), + }; const globaldb = { get: vi.fn().mockResolvedValue(null), set: vi.fn(), - } as unknown as KvStore; + delete: vi.fn(), + }; const getSiteSettings = vi.fn().mockResolvedValue({ site: { @@ -314,6 +317,50 @@ describe('updateSiteActor', () => { expect(result).toBe(false); }); + it('should update the site actor if one does not exist', async () => { + const db = { + get: vi.fn().mockResolvedValue(undefined), + set: vi.fn(), + delete: vi.fn(), + }; + + const globaldb = { + get: vi.fn().mockResolvedValue(null), + set: vi.fn(), + delete: vi.fn(), + }; + + const getSiteSettings = vi.fn().mockResolvedValue({ + site: { + description: 'New Site Description', + title: 'New Site Title', + icon: 'https://example.com/icon.png', + }, + }); + + const host = 'example.com'; + + const apCtx = mockApContext(db, globaldb); + + const result = await updateSiteActor(apCtx, getSiteSettings, host); + + expect(result).toBe(true); + + expect(db.set.mock.lastCall?.[1]).toStrictEqual({ + id: 'https://example.com/user/1', + name: 'New Site Title', + summary: 'New Site Description', + preferredUsername: 'index', + icon: 'https://example.com/icon.png', + inbox: 'https://example.com/inbox', + outbox: 'https://example.com/outbox', + following: 'https://example.com/following', + followers: 'https://example.com/followers', + liked: 'https://example.com/liked', + url: 'https://example.com/', + }); + }); + it('should update the site actor if the site settings have changed', async () => { const db = { get: vi.fn().mockResolvedValue({ @@ -327,15 +374,17 @@ describe('updateSiteActor', () => { following: 'https://example.com/following', followers: 'https://example.com/followers', liked: 'https://example.com/liked', - url: 'https://example.com', + url: 'https://example.com/', }), set: vi.fn(), - } as unknown as KvStore; + delete: vi.fn(), + }; const globaldb = { get: vi.fn().mockResolvedValue(null), set: vi.fn(), - } as unknown as KvStore; + delete: vi.fn(), + }; const getSiteSettings = vi.fn().mockResolvedValue({ site: { @@ -352,5 +401,19 @@ describe('updateSiteActor', () => { const result = await updateSiteActor(apCtx, getSiteSettings, host); expect(result).toBe(true); + + expect(db.set.mock.calls[0][1]).toStrictEqual({ + id: 'https://example.com/user/1', + name: 'New Site Title', + summary: 'New Site Description', + preferredUsername: 'index', + icon: 'https://example.com/icon.png', + inbox: 'https://example.com/inbox', + outbox: 'https://example.com/outbox', + following: 'https://example.com/following', + followers: 'https://example.com/followers', + liked: 'https://example.com/liked', + url: 'https://example.com/', + }); }); });