+ uri =
+ URI.parse(key_id)
+ |> Map.put(:fragment, nil)
+
+ uri =
+ if not is_nil(uri.path) and String.ends_with?(uri.path, "/publickey") do
+ Map.put(uri, :path, String.replace(uri.path, "/publickey", ""))
+ else
+ uri
+ end
+
+ case uri do
+ %URI{scheme: scheme} when scheme in ["https", "http"] ->
+ {:ok, URI.to_string(uri)}
+
+ _ ->
+ case Pleroma.Web.WebFinger.finger(URI.to_string(uri)) do
+ %{"ap_id" => ap_id} -> {:ok, ap_id}
+ _ -> {:error, URI.to_string(uri)}
+ end
+ end