Skip to content

Commit

Permalink
fixup! Added expanded followers data to the db
Browse files Browse the repository at this point in the history
  • Loading branch information
allouis committed Aug 6, 2024
1 parent f68604f commit fc7f2cd
Showing 1 changed file with 25 additions and 12 deletions.
37 changes: 25 additions & 12 deletions src/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -217,24 +220,24 @@ export async function inboxErrorHandler(
console.error(error);
}

async function lookupPerson(ctx: RequestContext<ContextData>, url: string) {
async function lookupActor(ctx: RequestContext<ContextData>, 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;
}
Expand All @@ -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<any[]>(['followers', 'expanded']);
if (fullResults) {
items = await Promise.all(fullResults.map(result => Person.fromJsonLd(result)));
items = (await Promise.all(
fullResults.map((result): Promise<Actor | null> => {
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<string[]>(['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,
Expand All @@ -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);
}
Expand Down

0 comments on commit fc7f2cd

Please sign in to comment.