Skip to content

Commit

Permalink
Used cached Actor data for Followers & Following
Browse files Browse the repository at this point in the history
The dispatcher for the followers collection is making network calls for each
follower which is a waste of resources and time. Instead we will check the
local db first for the actor data and if it's not present, we'll fetch over the
network and store it for next time.
  • Loading branch information
allouis committed Aug 5, 2024
1 parent 5689ed8 commit 2433505
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,20 @@ export async function inboxErrorHandler(
console.error(error);
}

async function lookupPerson(ctx: RequestContext<ContextData>, url: string) {
try {
const local = await ctx.data.globaldb.get([url]);
return await Person.fromJsonLd(local);
} catch (err) {
const remote = await lookupObject(url);
if (remote instanceof Person) {
await ctx.data.globaldb.set([url], await remote.toJsonLd());
return remote;
}
}
return null;
}

export async function followersDispatcher(
ctx: RequestContext<ContextData>,
handle: string,
Expand All @@ -226,7 +240,7 @@ export async function followersDispatcher(
let items: Person[] = [];
for (const result of results) {
try {
const thing = await lookupObject(result);
const thing = await lookupPerson(ctx, result);
if (thing instanceof Person) {
items.push(thing);
}
Expand Down Expand Up @@ -257,7 +271,7 @@ export async function followingDispatcher(
let items: Person[] = [];
for (const result of results) {
try {
const thing = await lookupObject(result);
const thing = await lookupPerson(ctx, result);
if (thing instanceof Person) {
items.push(thing);
}
Expand Down

0 comments on commit 2433505

Please sign in to comment.