Add basic Ostatus user representer.
authorRoger Braun <roger@rogerbraun.net>
Mon, 17 Apr 2017 12:12:36 +0000 (14:12 +0200)
committerRoger Braun <roger@rogerbraun.net>
Mon, 17 Apr 2017 12:12:36 +0000 (14:12 +0200)
lib/pleroma/user.ex
lib/pleroma/web/ostatus/user_representer.ex [new file with mode: 0644]
lib/pleroma/web/twitter_api/representers/user_representer.ex
test/web/ostatus/user_representer_test.exs [new file with mode: 0644]

index 1ab4e40cbaf8840f1b37e90d623504d6a89bfbe0..f8e4d6d0b9b25aa5e5dd8ea6952f2c87a77cf683 100644 (file)
@@ -18,6 +18,13 @@ defmodule Pleroma.User do
     timestamps()
   end
 
+  def avatar_url(user) do
+    case user.avatar do
+      %{"url" => [%{"href" => href} | _]} -> href
+      _ -> "https://placehold.it/48x48"
+    end
+  end
+
   def ap_id(%User{nickname: nickname}) do
     "#{Pleroma.Web.base_url}/users/#{nickname}"
   end
diff --git a/lib/pleroma/web/ostatus/user_representer.ex b/lib/pleroma/web/ostatus/user_representer.ex
new file mode 100644 (file)
index 0000000..66fc6e0
--- /dev/null
@@ -0,0 +1,14 @@
+defmodule Pleroma.Web.OStatus.UserRepresenter do
+  alias Pleroma.User
+  def to_tuple(user, wrapper \\ :author) do
+    {
+      wrapper, [
+        { :id, user.ap_id },
+        { :"activity:object", "http://activitystrea.ms/schema/1.0/person" },
+        { :uri, user.ap_id },
+        { :name, user.nickname },
+        { :link, %{rel: "avatar", href: User.avatar_url(user)}}
+      ]
+    }
+  end
+end
index 2ee4ee25443a00bc2a9d9dd16b8c5d20f5f777e7..7582a0f224db9a588c8834aa8416af9b296f32ae 100644 (file)
@@ -4,11 +4,7 @@ defmodule Pleroma.Web.TwitterAPI.Representers.UserRepresenter do
   alias Pleroma.User
 
   def to_map(user, opts) do
-    image = case user.avatar do
-      %{"url" => [%{"href" => href} | _]} -> href
-      _ -> "https://placehold.it/48x48"
-    end
-
+    image = User.avatar_url(user)
     following = if opts[:for] do
       User.following?(opts[:for], user)
     else
diff --git a/test/web/ostatus/user_representer_test.exs b/test/web/ostatus/user_representer_test.exs
new file mode 100644 (file)
index 0000000..02a4b5b
--- /dev/null
@@ -0,0 +1,17 @@
+defmodule Pleroma.Web.OStatus.UserRepresenterTest do
+  use Pleroma.DataCase
+  alias Pleroma.Web.OStatus.UserRepresenter
+
+  import Pleroma.Factory
+
+  test "returns a user with id, uri, name and link" do
+    user = build(:user)
+    tuple = UserRepresenter.to_tuple(user)
+    {:author, author} = tuple
+
+    [:id, :uri, :name, :link]
+    |> Enum.each(fn (tag) ->
+      assert Enum.find(author, fn(e) -> tag == elem(e, 0) end)
+    end)
+  end
+end