diff --git a/src/dispatchers.ts b/src/dispatchers.ts index 7919ef3b..f05256cb 100644 --- a/src/dispatchers.ts +++ b/src/dispatchers.ts @@ -11,6 +11,9 @@ import { Update, Context, Announce, + isActor, + Service, + Actor, } from '@fedify/fedify'; import { v4 as uuidv4 } from 'uuid'; import { addToList } from './kv-helpers'; @@ -217,24 +220,24 @@ export async function inboxErrorHandler( console.error(error); } -async function lookupPerson(ctx: RequestContext, url: string) { +async function lookupActor(ctx: RequestContext, url: string) { try { - console.log('Looking up person locally', url); + console.log('Looking up actor locally', url); const local = await ctx.data.globaldb.get([url]); return await Person.fromJsonLd(local); } catch (err) { - console.log('Error looking up person locally', url); + console.log('Error looking up actor locally', url); console.log(err); - console.log('Looking up person remotely', url); + console.log('Looking up actor remotely', url); const documentLoader = await ctx.getDocumentLoader({handle: 'index'}); try { const remote = await lookupObject(url, {documentLoader}); - if (remote instanceof Person) { + if (isActor(remote)) { await ctx.data.globaldb.set([url], await remote.toJsonLd()); return remote; } } catch (err) { - console.log('Error looking up person remotely', url); + console.log('Error looking up actor remotely', url); console.log(err) return null; } @@ -247,15 +250,25 @@ export async function followersDispatcher( handle: string, ) { console.log('Followers Dispatcher'); - let items: Person[] = []; + let items: Actor[] = []; const fullResults = await ctx.data.db.get(['followers', 'expanded']); if (fullResults) { - items = await Promise.all(fullResults.map(result => Person.fromJsonLd(result))); + items = (await Promise.all( + fullResults.map((result): Promise => { + if (result.type === 'Person') { + return Person.fromJsonLd(result) + } + if (result.type === 'Service') { + return Service.fromJsonLd(result); + } + return Promise.resolve(null); + }) + )).filter((item): item is Actor => item !== null); } else { const results = (await ctx.data.db.get(['followers'])) || []; - const items = (await Promise.all(results.map((result) => lookupPerson(ctx, result)))) - .filter((item): item is Person => item instanceof Person); - await ctx.data.db.set(['followers', 'expanded'], await Promise.all(items.map(person => person.toJsonLd()))); + const items = (await Promise.all(results.map((result) => lookupActor(ctx, result)))) + .filter((item): item is Actor => item !== null); + await ctx.data.db.set(['followers', 'expanded'], await Promise.all(items.map(actor => actor.toJsonLd()))); } return { items, @@ -280,7 +293,7 @@ export async function followingDispatcher( let items: Person[] = []; for (const result of results) { try { - const thing = await lookupPerson(ctx, result); + const thing = await lookupActor(ctx, result); if (thing instanceof Person) { items.push(thing); }