Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286defmodule AnnotAt.Atproto.StandardSite do @moduledoc """ Writes standard.site records (publication, documents) to a user's repo via Latch, uploading any images as blobs first. """
alias AnnotAt.Accounts alias AnnotAt.Atproto.HTTP alias AnnotAt.Atproto.Slingshot alias AnnotAt.Atproto.StandardSite.Document alias AnnotAt.Atproto.StandardSite.Publication
@publication "site.standard.publication" @document "site.standard.document" @wellknown_path "/.well-known/site.standard.publication" @html_content "org.wordpress.html" @html_max_graphemes 100_000
@doc """ Creates or updates the user's publication record. """ @spec put_publication(integer(), String.t(), Publication.t()) :: {:ok, map()} | {:error, term()} def put_publication(user_id, rkey, %Publication{} = publication) do with {:ok, user} <- fetch_user(user_id), {:ok, icon} <- upload_image(user, publication.icon) do put_record(user, @publication, rkey, publication_record(publication, icon)) end end
def get_document(user_id, rkey) do with {:ok, user} <- fetch_user(user_id), {:ok, %{"value" => value, "uri" => uri}} <- Latch.query(AnnotAt.Latch, user.did, "com.atproto.repo.getRecord", params: [repo: user.did, collection: @document, rkey: rkey] ) do {:ok, Document.from_record(uri, value)} end end
def list_documents(user_id) do with {:ok, user} <- fetch_user(user_id), {:ok, %{"records" => records}} <- Latch.query(AnnotAt.Latch, user.did, "com.atproto.repo.listRecords", params: [repo: user.did, collection: @document, limit: 100] ) do {:ok, Enum.map(records, fn %{"uri" => uri, "value" => value} -> Document.from_record(uri, value) end)} end end
def put_document(user_id, %Document{} = document, cover_upload \\ nil) do with {:ok, user} <- fetch_user(user_id), {:ok, cover} <- upload_image(user, cover_upload) do put_record(user, @document, document.rkey, document_record(document, cover)) end end
def delete_document(user_id, rkey) do with {:ok, user} <- fetch_user(user_id) do Latch.procedure(AnnotAt.Latch, user.did, "com.atproto.repo.deleteRecord", %{ repo: user.did, collection: @document, rkey: rkey }) end end
@doc """ The AT-URI of the user's publication record, used as a document's `site`. """ @spec publication_uri(String.t(), String.t()) :: String.t() def publication_uri(did, rkey), do: "at://#{did}/#{@publication}/#{rkey}"
@doc """ Verifies the website hosts the publication's AT-URI at its well-known path, proving control of the domain. """ @spec verify_ownership(String.t(), String.t()) :: :ok | {:error, :mismatch | :not_found | {:http_status, pos_integer()} | {:transport, term()}} def verify_ownership(url, at_uri) do url = wellknown_url(url)
case HTTP.get_text(url) do {:ok, body} -> if String.trim(body) == at_uri do :ok else {:error, :mismatch} end
{:error, {:http_status, 404}} -> {:error, :not_found}
{:error, _reason} = error -> error end end
@spec list_publications(integer()) :: {:ok, [%{rkey: String.t(), url: String.t() | nil, name: String.t() | nil}]} | {:error, :no_session} def list_publications(user_id) do with {:ok, user} <- fetch_user(user_id), {:ok, %{"records" => records}} <- Latch.query(AnnotAt.Latch, user.did, "com.atproto.repo.listRecords", params: [repo: user.did, collection: @publication] ) do {:ok, Enum.map(records, &to_existing/1)} end end
@doc """ Reads the user's publication document. """ @spec get_publication(integer(), String.t()) :: {:ok, map()} | {:error, term()} def get_publication(user_id, rkey) do with {:ok, user} <- fetch_user(user_id), {:ok, %{"value" => value}} <- Latch.query(AnnotAt.Latch, user.did, "com.atproto.repo.getRecord", params: [repo: user.did, collection: @publication, rkey: rkey] ) do {:ok, value} end end
@doc """ Builds the publication document we create, from page metadata. """ @spec draft_publication(String.t(), %{title: String.t() | nil, description: String.t() | nil}) :: map() def draft_publication(url, %{title: title, description: description}) do %{ "$type" => @publication, "name" => title, "url" => url, "description" => description, "preferences" => %{"showInDiscover" => true} } end
@doc """ Construct an at-uri for a document, useful when referencing documents. """ def document_uri(did, rkey) do "at://#{did}/site.standard.document/#{rkey}" end
def get_public_document(did, rkey) do with {:ok, value, did_doc} <- get_public_record(did, @document, rkey) do {:ok, Document.from_record(document_uri(did, rkey), value), did_doc} end end
def get_public_publication("at://" <> _ = site_uri) do with {:ok, did, rkey} <- parse_aturi(site_uri, @publication) do get_public_record(did, @publication, rkey) end end
def get_public_publication(_site), do: {:error, :no_publication}
def get_public_record(did, collection, rkey) do with {:ok, %{did: did, handle: handle, pds: pds}} <- Slingshot.resolve_identity(did), {:ok, %{"value" => value}} <- Slingshot.get_record(did, collection, rkey) do {:ok, value, %{did: did, handle: handle, pds_endpoint: pds}} end end
def wellknown_path(%URI{} = url), do: do_wellknown_path(url)
def wellknown_path(url) when is_binary(url) do uri = URI.parse(url)
do_wellknown_path(uri) end
defp do_wellknown_path(uri) do if uri.path in [nil, "/"] do @wellknown_path else @wellknown_path <> uri.path end end
defp fetch_user(user_id) do case Accounts.get_user(user_id) do nil -> {:error, :no_session} user -> {:ok, user} end end
defp upload_image(_user, nil), do: {:ok, nil}
defp upload_image(user, {bytes, content_type}) do with {:ok, %{"blob" => blob}} <- Latch.upload_blob(AnnotAt.Latch, user.did, bytes, content_type) do {:ok, blob} end end
defp put_record(user, collection, rkey, record) do body = %{repo: user.did, collection: collection, rkey: rkey, record: record} Latch.procedure(AnnotAt.Latch, user.did, "com.atproto.repo.putRecord", body) end
defp publication_record(p, icon) do %{"$type" => @publication, "name" => p.name, "url" => p.url} |> put_optional("description", p.description) |> put_optional("icon", icon) end
defp document_record(d, cover_image) do %{ "$type" => @document, "site" => d.site, "title" => d.title, "publishedAt" => DateTime.to_iso8601(d.published_at) } |> put_optional("path", d.path) |> put_optional("updatedAt", iso8601(d.updated_at)) |> put_optional("description", d.description) |> put_optional("textContent", d.text_content) |> put_optional("content", to_content(d.content)) |> put_optional("tags", d.tags) |> put_optional("coverImage", cover_image) end
defp iso8601(nil), do: nil defp iso8601(%DateTime{} = dt), do: DateTime.to_iso8601(dt)
defp put_optional(map, _key, nil), do: map defp put_optional(map, key, value), do: Map.put(map, key, value)
defp wellknown_url(url) do uri = URI.parse(url)
URI.to_string(%{ uri | path: wellknown_path(uri), query: nil, fragment: nil }) end
defp to_existing(%{"uri" => uri, "value" => value}) do rkey = uri |> String.split("/") |> List.last()
%{rkey: rkey, url: value["url"], name: value["name"]} end
defp to_content(nil), do: nil
defp to_content({:html, html}) do %{ "$type" => @html_content, "html" => truncate_graphemes(html, @html_max_graphemes) } end
defp to_content({:unknown, content}), do: content
defp truncate_graphemes(text, max) do if String.length(text) <= max do text else String.slice(text, 0, max) end end
defp parse_aturi("at://" <> rest, collection) do case String.split(rest, "/") do [did, ^collection, rkey] -> {:ok, did, rkey} _ -> {:error, :invalid} end end
defp parse_aturi(_uri, _collection), do: {:error, :invalid}end