Unverified Commit 5e2224c3 authored by Hong Minhee's avatar Hong Minhee
Browse files

Object lookup function

parent 2a4aee50
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@
    "fedify",
    "fediverse",
    "halfyear",
    "hongminhee",
    "httpsig",
    "jsonld",
    "langstr",
+5 −0
Original line number Diff line number Diff line
@@ -25,11 +25,16 @@ To be released.

 -  Implemented [WebFinger] client.

     -  Added `lookupObject()` function.
     -  Added `lookupWebFinger()` function.

 -  `Federation.handle()` now responds with `Access-Control-Allow-Origin: *`
    header for WebFinger requests.

 -  `fetchDocumentLoader()`, the default document loader, now sends `Accept:
    application/activity+json, application/ld+json` header (was `Accept:
    application/ld+json` only).

[NodeInfo]: https://nodeinfo.diaspora.software/
[WebFinger]: https://datatracker.ietf.org/doc/html/rfc7033
[#1]: https://github.com/dahlia/fedify/issues/1
+5 −4
Original line number Diff line number Diff line
@@ -134,11 +134,12 @@ Getting a `DocumentLoader`
The `Context.documentLoader` property carries a `DocumentLoader` object that
is specified in the `Federation` constructor.  It is used to load remote
document in the JSON-LD format.  There are a few methods to take
a `DocumentLoader` as an option in [vocabulary objects](./vocab.md):
a `DocumentLoader` as an option in vocabulary API:

 -  `fromJsonLd()` static method
 -  `toJsonLd()` method
 -  `get*()` dereferencing accessors
 -  [`fromJsonLd()` static method](./vocab.md#json-ld)
 -  [`toJsonLd()` method](./vocab.md#json-ld)
 -  [`get*()` dereferencing accessors](./vocab.md#object-ids-and-remote-objects)
 -  [`lookupObject()` function](./vocab.md#looking-up-remote-objects)

All of those methods take options in the form of
`{ documentLoader: DocumentLoader }` which is compatible with `Context`.
+44 −0
Original line number Diff line number Diff line
@@ -169,6 +169,50 @@ Parameters of the `clone()` method share the same type with parameters of
the constructor.


Looking up remote objects
-------------------------

*This API is available since Fedify 0.2.0.*

Suppose your app has a search box that allows the user to look up a fediverse
user by the handle or a post by the URI.  In such cases, you need to look up
the object from a remote server that your app haven't interacted with yet.
The `lookupObject()` function plays a role in such cases.  The following shows
an example of looking up an actor object from the handle:

~~~~ typescript
import { lookupObject } from "jsr:@fedify/fedify";

const actor = await lookupObject("@hongminhee@todon.eu");
~~~~

In the above example, the `lookupObject()` function queries the remote server's
WebFinger endpoint to get the actor's URI from the handle, and then fetches the
actor object from the URI.

> [!TIP]
> The `lookupObject()` function accepts a fediverse handle without prefix `@`
> as well:
>
> ~~~~ typescript
> const actor = await lookupObject("hongminhee@todon.eu");
> ~~~~
>
> Also an `acct:` URI:
>
> ~~~~ typescript
> const actor = await lookupObject("acct:hongminhee@todon.eu");
> ~~~~

The `lookupObject()` function is not limited to the actor object.  It can look
up any object in the Activity Vocabulary.  For example, the following shows an
example of looking up a `Note` object from the URI:

~~~~ typescript
const note = await lookupObject("https://todon.eu/@hongminhee/112060633798771581");
~~~~


JSON-LD
-------

+1 −1
Original line number Diff line number Diff line
@@ -47,7 +47,7 @@ export async function fetchDocumentLoader(
): Promise<RemoteDocument> {
  const response = await fetch(url, {
    headers: {
      Accept: "application/ld+json",
      Accept: "application/activity+json, application/ld+json",
    },
    redirect: "follow",
  });
Loading