Various refactoring and typespec improvements

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-09-24 16:46:42 +02:00
parent d235653876
commit 1893d9f55b
142 changed files with 1854 additions and 1297 deletions

View File

@@ -27,7 +27,7 @@ defmodule Mobilizon.Federation.WebFinger do
base_url = Endpoint.url()
%URI{host: host} = URI.parse(base_url)
{
XmlBuilder.to_doc({
:XRD,
%{
xmlns: "http://docs.oasis-open.org/ns/xri/xrd-1.0",
@@ -47,8 +47,7 @@ defmodule Mobilizon.Federation.WebFinger do
}
}
]
}
|> XmlBuilder.to_doc()
})
end
@doc """
@@ -150,7 +149,7 @@ defmodule Mobilizon.Federation.WebFinger do
{:error, err} ->
Logger.debug("Couldn't process webfinger data for #{actor}")
err
{:error, err}
end
{:error, err} ->
@@ -187,7 +186,8 @@ defmodule Mobilizon.Federation.WebFinger do
end
end
# Fetches the Extensible Resource Descriptor endpoint `/.well-known/host-meta` to find the Webfinger endpoint (usually `/.well-known/webfinger?resource=`)
# Fetches the Extensible Resource Descriptor endpoint `/.well-known/host-meta`
# to find the Webfinger endpoint (usually `/.well-known/webfinger?resource=`)
@spec find_webfinger_endpoint(String.t()) ::
{:ok, String.t()} | {:error, :link_not_found} | {:error, any()}
defp find_webfinger_endpoint(domain) when is_binary(domain) do

View File

@@ -5,42 +5,54 @@
defmodule Mobilizon.Federation.WebFinger.XmlBuilder do
@moduledoc """
Builds XRD for WebFinger host_meta.
Extremely basic XML encoder. Builds XRD for WebFinger host_meta.
"""
def to_xml({tag, attributes, content}) do
@typep content :: list({tag :: atom(), attributes :: map()}) | String.t()
@typep document :: {tag :: atom(), attributes :: map(), content :: content}
@doc """
Return the XML representation for a document.
"""
@spec to_doc(document :: document) :: String.t()
def to_doc(document), do: ~s(<?xml version="1.0" encoding="UTF-8"?>) <> to_xml(document)
@spec to_xml(document) :: String.t()
@spec to_xml({tag :: atom(), attributes :: map()}) :: String.t()
@spec to_xml({tag :: atom(), content :: content}) :: String.t()
@spec to_xml(content :: content) :: String.t()
defp to_xml({tag, attributes, content}) do
open_tag = make_open_tag(tag, attributes)
content_xml = to_xml(content)
"<#{open_tag}>#{content_xml}</#{tag}>"
end
def to_xml({tag, %{} = attributes}) do
defp to_xml({tag, %{} = attributes}) do
open_tag = make_open_tag(tag, attributes)
"<#{open_tag} />"
end
def to_xml({tag, content}), do: to_xml({tag, %{}, content})
defp to_xml({tag, content}), do: to_xml({tag, %{}, content})
def to_xml(content) when is_binary(content), do: to_string(content)
defp to_xml(content) when is_binary(content), do: to_string(content)
def to_xml(content) when is_list(content) do
defp to_xml(content) when is_list(content) do
content
|> Enum.map(&to_xml/1)
|> Enum.join()
end
def to_xml(%NaiveDateTime{} = time), do: NaiveDateTime.to_iso8601(time)
def to_doc(content), do: ~s(<?xml version="1.0" encoding="UTF-8"?>) <> to_xml(content)
defp to_xml(%NaiveDateTime{} = time), do: NaiveDateTime.to_iso8601(time)
@spec make_open_tag(tag :: atom, attributes :: map()) :: String.t()
defp make_open_tag(tag, attributes) do
attributes_string =
attributes
|> Enum.map(fn {attribute, value} -> "#{attribute}=\"#{value}\"" end)
|> Enum.join(" ")
[tag, attributes_string] |> Enum.join(" ") |> String.trim()
[to_string(tag), attributes_string] |> Enum.join(" ") |> String.trim()
end
end