Skip to content

Commit

Permalink
Refactored lookupActor out
Browse files Browse the repository at this point in the history
We want to use this in multiple places!
  • Loading branch information
allouis committed Sep 9, 2024
1 parent 180d1e5 commit d38ddb8
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 25 deletions.
26 changes: 1 addition & 25 deletions src/dispatchers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { addToList } from './kv-helpers';
import { ContextData } from './app';
import { ACTOR_DEFAULT_HANDLE } from './constants';
import { getUserData, getUserKeypair } from './user';
import { lookupActor } from './lookup-helpers';

export async function actorDispatcher(
ctx: RequestContext<ContextData>,
Expand Down Expand Up @@ -302,31 +303,6 @@ export async function inboxErrorHandler(
console.error(error);
}

async function lookupActor(ctx: RequestContext<ContextData>, url: string) {
try {
console.log('Looking up actor locally', url);
const local = await ctx.data.globaldb.get([url]);
return await APObject.fromJsonLd(local);
} catch (err) {
console.log('Error looking up actor locally', url);
console.log(err);
console.log('Looking up actor remotely', url);
const documentLoader = await ctx.getDocumentLoader({handle: 'index'});
try {
const remote = await lookupObject(url, {documentLoader});
if (isActor(remote)) {
await ctx.data.globaldb.set([url], await remote.toJsonLd());
return remote;
}
} catch (err) {
console.log('Error looking up actor remotely', url);
console.log(err)
return null;
}
}
return null;
}

function convertJsonLdToRecipient(result: any): Recipient {
return {
...result,
Expand Down
37 changes: 37 additions & 0 deletions src/lookup-helpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {
isActor,
lookupObject,
Actor,
RequestContext,
Object as APObject,
} from '@fedify/fedify';
import { ContextData } from './app';

export async function lookupActor(ctx: RequestContext<ContextData>, url: string): Promise<Actor | null> {
try {
console.log('Looking up actor locally', url);
const local = await ctx.data.globaldb.get([url]);
const object = await APObject.fromJsonLd(local);
if (isActor(object)) {
return object;
}
return null;
} catch (err) {
console.log('Error looking up actor locally', url);
console.log(err);
console.log('Looking up actor remotely', url);
const documentLoader = await ctx.getDocumentLoader({handle: 'index'});
try {
const remote = await lookupObject(url, {documentLoader});
if (isActor(remote)) {
await ctx.data.globaldb.set([url], await remote.toJsonLd());
return remote;
}
} catch (err) {
console.log('Error looking up actor remotely', url);
console.log(err)
return null;
}
}
return null;
}

0 comments on commit d38ddb8

Please sign in to comment.