Add user info gathering.
authorRoger Braun <roger@rogerbraun.net>
Sat, 29 Apr 2017 15:51:59 +0000 (17:51 +0200)
committerRoger Braun <roger@rogerbraun.net>
Sat, 29 Apr 2017 15:51:59 +0000 (17:51 +0200)
lib/pleroma/web/ostatus/ostatus.ex
lib/pleroma/web/web_finger/web_finger.ex
lib/pleroma/web/websub/websub.ex
test/web/ostatus/ostatus_test.exs
test/web/websub/websub_test.exs

index 89b4825928fdbd793c286c62a23a9327568f2366..90be86755d776996f6a01ea83ab78d2b474fdb6f 100644 (file)
@@ -5,6 +5,7 @@ defmodule Pleroma.Web.OStatus do
 
   alias Pleroma.{Repo, User, Web}
   alias Pleroma.Web.ActivityPub.ActivityPub
+  alias Pleroma.Web.{WebFinger, Websub}
 
   def feed_path(user) do
     "#{user.ap_id}/feed.atom"
@@ -134,4 +135,14 @@ defmodule Pleroma.Web.OStatus do
       nil
     end
   end
+
+  def gather_user_info(username) do
+    with {:ok, webfinger_data} <- WebFinger.finger(username),
+         {:ok, feed_data} <- Websub.gather_feed_data(webfinger_data.topic) do
+      {:ok, Map.merge(webfinger_data, feed_data) |> Map.put(:fqn, username)}
+    else e ->
+      Logger.debug("Couldn't gather info for #{username}")
+      {:error, e}
+    end
+  end
 end
index 08ff6d278b6017d617c2b1c7c455d422cbc5faa9..1d8c4d0c8c2e947167420e1079386dc592927c92 100644 (file)
@@ -18,7 +18,7 @@ defmodule Pleroma.Web.WebFinger do
 
   def webfinger(resource) do
     host = Pleroma.Web.host
-    regex = ~r/acct:(?<username>\w+)@#{host}/
+    regex = ~r/(acct:)?(?<username>\w+)@#{host}/
     case Regex.named_captures(regex, resource) do
       %{"username" => username} ->
         user = User.get_cached_by_nickname(username)
@@ -70,7 +70,7 @@ defmodule Pleroma.Web.WebFinger do
     else
       e ->
         Logger.debug("Couldn't finger #{account}.")
-        Logger.debug(e)
+        Logger.debug(inspect(e))
         {:error, e}
     end
   end
index ad9e47b467c3e9bc64e049aac9a0f70fac8b1c74..c1d48ad7a43c16aab0fb0856918b16c6af6ff60f 100644 (file)
@@ -121,14 +121,24 @@ defmodule Pleroma.Web.Websub do
     requester.(subscription)
   end
 
-  def discover(topic, getter \\ &HTTPoison.get/1) do
+  def gather_feed_data(topic, getter \\ &HTTPoison.get/1) do
     with {:ok, response} <- getter.(topic),
          status_code when status_code in 200..299 <- response.status_code,
          body <- response.body,
          doc <- XML.parse_document(body),
-         url when not is_nil(url) <- XML.string_from_xpath(~S{/feed/link[@rel="self"]/@href}, doc),
+         uri when not is_nil(uri) <- XML.string_from_xpath("/feed/author[1]/uri", doc),
          hub when not is_nil(hub) <- XML.string_from_xpath(~S{/feed/link[@rel="hub"]/@href}, doc) do
-      {:ok, %{url: url, hub: hub}}
+
+      name = XML.string_from_xpath("/feed/author[1]/name", doc)
+      preferredUsername = XML.string_from_xpath("/feed/author[1]/poco:preferredUsername", doc)
+      displayName = XML.string_from_xpath("/feed/author[1]/poco:displayName", doc)
+
+      {:ok, %{
+        uri: uri,
+        hub: hub,
+        nickname: preferredUsername || name,
+        name: displayName || name
+      }}
     else e ->
       {:error, e}
     end
index 140b32f367194eb1924374d229906b1e45ea05a2..2a5156b31b649329657899762f76602e1ff80c66 100644 (file)
@@ -63,4 +63,26 @@ defmodule Pleroma.Web.OStatusTest do
       assert user == user_again
     end
   end
+
+  describe "gathering user info from a user id" do
+    test "it returns user info in a hash" do
+      user = "shp@social.heldscal.la"
+
+      # TODO: make test local
+      {:ok, data} = OStatus.gather_user_info(user)
+
+      expected = %{
+        hub: "https://social.heldscal.la/main/push/hub",
+        magic_key: "RSA.wQ3i9UA0qmAxZ0WTIp4a-waZn_17Ez1pEEmqmqoooRsG1_BvpmOvLN0G2tEcWWxl2KOtdQMCiPptmQObeZeuj48mdsDZ4ArQinexY2hCCTcbV8Xpswpkb8K05RcKipdg07pnI7tAgQ0VWSZDImncL6YUGlG5YN8b5TjGOwk2VG8=.AQAB",
+        name: "shp",
+        nickname: "shp",
+        salmon: "https://social.heldscal.la/main/salmon/user/29191",
+        subject: "acct:shp@social.heldscal.la",
+        topic: "https://social.heldscal.la/api/statuses/user_timeline/29191.atom",
+        uri: "https://social.heldscal.la/user/29191",
+        fqn: user
+      }
+      assert data == expected
+    end
+  end
 end
index ca04a55cd08c145f55b8149e4ef27cccea2fa7f2..1b1ef3fa6082559ff86c3f98bae9e41b7261e9d2 100644 (file)
@@ -112,8 +112,15 @@ defmodule Pleroma.Web.WebsubTest do
       {:ok, %{status_code: 200, body: doc}}
     end
 
-    {:ok, discovered} = Websub.discover(topic, getter)
-    assert %{hub: "https://mastodon.social/api/push", url: topic} == discovered
+    {:ok, discovered} = Websub.gather_feed_data(topic, getter)
+    expected = %{
+      hub: "https://mastodon.social/api/push",
+      uri: "https://mastodon.social/users/lambadalambda",
+      nickname: "lambadalambda",
+      name: "Critical Value"
+    }
+
+    assert expected == discovered
   end
 
   test "calls the hub, requests topic" do