Merge branch 'release/2.1.0' into 'stable'
[akkoma] / lib / pleroma / web / web_finger / web_finger.ex
index b264538282cbb200cf0bb87b337abbacab5c8673..c4051e63e40979321304eef1b13614ffcae87f6e 100644 (file)
@@ -87,19 +87,23 @@ defmodule Pleroma.Web.WebFinger do
   end
 
   defp webfinger_from_xml(doc) do
-    with subject <- XML.string_from_xpath("//Subject", doc),
-         ap_id <-
-           XML.string_from_xpath(
-             ~s{//Link[@rel="self" and @type="application/activity+json"]/@href},
-             doc
-           ) do
-      data = %{
-        "subject" => subject,
-        "ap_id" => ap_id
-      }
+    subject = XML.string_from_xpath("//Subject", doc)
 
-      {:ok, data}
-    end
+    subscribe_address =
+      ~s{//Link[@rel="http://ostatus.org/schema/1.0/subscribe"]/@template}
+      |> XML.string_from_xpath(doc)
+
+    ap_id =
+      ~s{//Link[@rel="self" and @type="application/activity+json"]/@href}
+      |> XML.string_from_xpath(doc)
+
+    data = %{
+      "subject" => subject,
+      "subscribe_address" => subscribe_address,
+      "ap_id" => ap_id
+    }
+
+    {:ok, data}
   end
 
   defp webfinger_from_json(doc) do
@@ -145,6 +149,18 @@ defmodule Pleroma.Web.WebFinger do
     end
   end
 
+  defp get_address_from_domain(domain, encoded_account) when is_binary(domain) do
+    case find_lrdd_template(domain) do
+      {:ok, template} ->
+        String.replace(template, "{uri}", encoded_account)
+
+      _ ->
+        "https://#{domain}/.well-known/webfinger?resource=#{encoded_account}"
+    end
+  end
+
+  defp get_address_from_domain(_, _), do: nil
+
   @spec finger(String.t()) :: {:ok, map()} | {:error, any()}
   def finger(account) do
     account = String.trim_leading(account, "@")
@@ -157,16 +173,10 @@ defmodule Pleroma.Web.WebFinger do
           URI.parse(account).host
       end
 
-    address =
-      case find_lrdd_template(domain) do
-        {:ok, template} ->
-          String.replace(template, "{uri}", URI.encode(account))
-
-        _ ->
-          "https://#{domain}/.well-known/webfinger?resource=acct:#{account}"
-      end
+    encoded_account = URI.encode("acct:#{account}")
 
-    with response <-
+    with address when is_binary(address) <- get_address_from_domain(domain, encoded_account),
+         response <-
            HTTP.get(
              address,
              [{"accept", "application/xrd+xml,application/jrd+json"}]