|
1 | 1 | defmodule Atex.IdentityResolver do
|
2 |
| - alias Atex.IdentityResolver.{DID, DIDDocument, Handle} |
| 2 | + alias Atex.IdentityResolver.{Cache, DID, DIDDocument, Handle, Identity} |
3 | 3 |
|
4 | 4 | @handle_strategy Application.compile_env(:atex, :handle_resolver_strategy, :dns_first)
|
5 | 5 |
|
6 | 6 | # TODO: simplify errors
|
7 | 7 |
|
8 |
| - @spec resolve(identity :: String.t()) :: |
9 |
| - {:ok, document :: DIDDocument.t(), did :: String.t(), handle :: String.t()} |
10 |
| - | {:ok, DIDDocument.t()} |
| 8 | + def resolve(identifier) do |
| 9 | + # If cache fetch succeeds, then the ok tuple will be retuned by the default `with` behaviour |
| 10 | + with {:error, :not_found} <- Cache.get(identifier), |
| 11 | + {:ok, identity} <- do_resolve(identifier), |
| 12 | + identity <- Cache.insert(identity) do |
| 13 | + {:ok, identity} |
| 14 | + end |
| 15 | + end |
| 16 | + |
| 17 | + @spec do_resolve(identity :: String.t()) :: |
| 18 | + {:ok, Identity.t()} |
11 | 19 | | {:error, :handle_mismatch}
|
12 | 20 | | {:error, any()}
|
13 |
| - def resolve("did:" <> _ = did) do |
| 21 | + defp do_resolve("did:" <> _ = did) do |
14 | 22 | with {:ok, document} <- DID.resolve(did),
|
15 | 23 | :ok <- DIDDocument.validate_for_atproto(document, did) do
|
16 | 24 | with handle when not is_nil(handle) <- DIDDocument.get_atproto_handle(document),
|
17 | 25 | {:ok, handle_did} <- Handle.resolve(handle, @handle_strategy),
|
18 | 26 | true <- handle_did == did do
|
19 |
| - {:ok, document, did, handle} |
| 27 | + {:ok, Identity.new(did, handle, document)} |
20 | 28 | else
|
21 | 29 | # Not having a handle, while a little un-ergonomic, is totally valid.
|
22 |
| - nil -> {:ok, document} |
| 30 | + nil -> {:ok, Identity.new(did, nil, document)} |
23 | 31 | false -> {:error, :handle_mismatch}
|
24 | 32 | e -> e
|
25 | 33 | end
|
26 | 34 | end
|
27 | 35 | end
|
28 | 36 |
|
29 |
| - def resolve(handle) do |
| 37 | + defp do_resolve(handle) do |
30 | 38 | with {:ok, did} <- Handle.resolve(handle, @handle_strategy),
|
31 | 39 | {:ok, document} <- DID.resolve(did),
|
32 | 40 | did_handle when not is_nil(handle) <- DIDDocument.get_atproto_handle(document),
|
33 | 41 | true <- did_handle == handle do
|
34 |
| - {:ok, document, did, handle} |
| 42 | + {:ok, Identity.new(did, handle, document)} |
35 | 43 | else
|
36 | 44 | nil -> {:error, :handle_mismatch}
|
37 | 45 | false -> {:error, :handle_mismatch}
|
|
0 commit comments