Exosphere.Bsky.Records (Exosphere v0.3.0)

Copy Markdown View Source

Typed repository operations: create, fetch, list, and delete records through their generated struct modules.

The collection NSID and wire encoding come from each struct's generated module (type_id/0 and to_map/1), so a %Exosphere.Bsky.Feed.Post{} knows it belongs in app.bsky.feed.post.

Writes go to the user's PDS (authenticated); reads may go to any server hosting the record (e.g. the App View or the PDS itself).

Examples

{:ok, post} = Exosphere.Bsky.Feed.Post.new(%{text: "Hello!", created_at: DateTime.utc_now() |> DateTime.to_iso8601()})

{:ok, %{uri: uri, cid: cid}} =
  Exosphere.Bsky.Records.create(session, pds_url, did, post)

{:ok, %Exosphere.Bsky.Feed.Post{} = post} =
  Exosphere.Bsky.Records.get(pds_url, did, Exosphere.Bsky.Feed.Post, rkey)

Summary

Functions

Create a record from a generated struct.

Delete a record by collection module and record key.

Fetch and decode a record by collection module and record key.

List records in a collection, decoding each into module.

Functions

create(session, pds_url, did, record, opts \\ [])

@spec create(map(), String.t(), String.t(), struct(), keyword()) ::
  {:ok, %{uri: String.t(), cid: String.t()}} | {:error, term()}

Create a record from a generated struct.

The collection is taken from the struct's lexicon type ID. An optional :rkey overrides the generated one (record schemas with tid keys get a fresh TID; self-keyed records should pass rkey: "self").

delete(session, pds_url, did, module, rkey)

@spec delete(map(), String.t(), String.t(), module(), String.t()) ::
  {:ok, map()} | {:error, term()}

Delete a record by collection module and record key.

get(server, did, module, rkey)

@spec get(
  String.t() | Exosphere.ATProto.XRPC.Client.t(),
  String.t(),
  module(),
  String.t()
) ::
  {:ok, struct()} | {:error, term()}

Fetch and decode a record by collection module and record key.

Accepts a server URL or a pre-built Client (inject http: for testing). The module must be a generated schema module (Exosphere.Bsky.Feed.Post, Exosphere.Community.Interaction.Like, ...).

list(server, did, module, opts \\ [])

@spec list(
  String.t() | Exosphere.ATProto.XRPC.Client.t(),
  String.t(),
  module(),
  keyword()
) ::
  {:ok, [struct()], cursor :: String.t() | nil} | {:error, term()}

List records in a collection, decoding each into module.

Returns {:ok, records, cursor} where cursor is nil or the next-page cursor from the server.