- defp scrape_favicon(%URI{} = instance_uri) do
- try do
- with {_, true} <- {:reachable, reachable?(instance_uri.host)},
- {:ok, %Tesla.Env{body: html}} <-
- Pleroma.HTTP.get(to_string(instance_uri), [{"accept", "text/html"}], pool: :media),
- {_, [favicon_rel | _]} when is_binary(favicon_rel) <-
- {:parse,
- html |> Floki.parse_document!() |> Floki.attribute("link[rel=icon]", "href")},
- {_, favicon} when is_binary(favicon) <-
- {:merge, URI.merge(instance_uri, favicon_rel) |> to_string()} do
- favicon
- else
- {:reachable, false} ->
- Logger.debug(
- "Instance.scrape_favicon(\"#{to_string(instance_uri)}\") ignored unreachable host"
- )
+ defp scrape_nodeinfo(%URI{} = instance_uri) do
+ with true <- Pleroma.Config.get([:instances_nodeinfo, :enabled]),
+ {_, true} <- {:reachable, reachable?(instance_uri.host)},
+ {:ok, %Tesla.Env{status: 200, body: body}} <-
+ Tesla.get(
+ "https://#{instance_uri.host}/.well-known/nodeinfo",
+ headers: [{"Accept", "application/json"}]
+ ),
+ {:ok, json} <- Jason.decode(body),
+ {:ok, %{"links" => links}} <- {:ok, json},
+ {:ok, %{"href" => href}} <-
+ {:ok,
+ Enum.find(links, &(&1["rel"] == "http://nodeinfo.diaspora.software/ns/schema/2.0"))},
+ {:ok, %Tesla.Env{body: data}} <-
+ Pleroma.HTTP.get(href, [{"accept", "application/json"}], []),
+ {:length, true} <- {:length, String.length(data) < 50_000},
+ {:ok, nodeinfo} <- Jason.decode(data) do
+ nodeinfo
+ else
+ {:reachable, false} ->
+ Logger.debug(
+ "Instance.scrape_nodeinfo(\"#{to_string(instance_uri)}\") ignored unreachable host"
+ )