-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
We want to use this in multiple places!
- Loading branch information
Showing
2 changed files
with
38 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |