AP UserView: Refactor banner / avatar display code, add test.
authorlain <lain@soykaf.club>
Tue, 26 Mar 2019 15:40:09 +0000 (16:40 +0100)
committerlain <lain@soykaf.club>
Tue, 26 Mar 2019 15:59:28 +0000 (16:59 +0100)
lib/pleroma/user.ex
lib/pleroma/web/activity_pub/utils.ex
lib/pleroma/web/activity_pub/views/user_view.ex
test/web/activity_pub/views/user_view_test.exs

index ee5eb8efa85d15ddc78879c2539b1bf04e4f977c..efb4540b9c3f1e92267a784ec4d304b9872c4ccd 100644 (file)
@@ -83,25 +83,17 @@ defmodule Pleroma.User do
   def superuser?(%User{local: true, info: %User.Info{is_moderator: true}}), do: true
   def superuser?(_), do: false
 
-  def avatar_url(user) do
+  def avatar_url(user, options \\ []) do
     case user.avatar do
       %{"url" => [%{"href" => href} | _]} -> href
-      _ -> "#{Web.base_url()}/images/avi.png"
+      _ -> !options[:no_default] && "#{Web.base_url()}/images/avi.png"
     end
   end
 
-  # Do not return instance default avatar for federation
-  def avatar_url_ap(user) do
-    case user.avatar do
-      %{"url" => [%{"href" => href} | _]} -> href
-      _ -> nil
-    end
-  end
-
-  def banner_url(user) do
+  def banner_url(user, options \\ []) do
     case user.info.banner do
       %{"url" => [%{"href" => href} | _]} -> href
-      _ -> "#{Web.base_url()}/images/banner.png"
+      _ -> !options[:no_default] && "#{Web.base_url()}/images/banner.png"
     end
   end
 
index 86b6e702984a31a6101a3eeefe97b52aa506857b..2e9ffe41c40f5b2fa33049d94f7544cd450af5a0 100644 (file)
@@ -230,21 +230,6 @@ defmodule Pleroma.Web.ActivityPub.Utils do
     end)
   end
 
-  # Only federate user icon if not nil
-  # Prevents federating instance default avatars
-  def maybe_make_icon(user) do
-    if User.avatar_url_ap(user) do
-      %{
-        "icon" => %{
-          "type" => "Image",
-          "url" => User.avatar_url_ap(user)
-        }
-      }
-    else
-      %{}
-    end
-  end
-
   #### Like-related helpers
 
   @doc """
index f5c86d3609d71f5085d57d0abdbdaf8679f27b75..5926a3294e44faa8a569acbb06c0989cc06bbdeb 100644 (file)
@@ -87,13 +87,10 @@ defmodule Pleroma.Web.ActivityPub.UserView do
         "publicKeyPem" => public_key
       },
       "endpoints" => endpoints,
-      "image" => %{
-        "type" => "Image",
-        "url" => User.banner_url(user)
-      },
       "tag" => user.info.source_data["tag"] || []
     }
-    |> Map.merge(Utils.maybe_make_icon(user))
+    |> Map.merge(maybe_make_image(&User.avatar_url/2, "icon", user))
+    |> Map.merge(maybe_make_image(&User.banner_url/2, "image", user))
     |> Map.merge(Utils.make_json_ld_header())
   end
 
@@ -291,4 +288,17 @@ defmodule Pleroma.Web.ActivityPub.UserView do
       map
     end
   end
+
+  defp maybe_make_image(func, key, user) do
+    if image = func.(user, no_default: true) do
+      %{
+        key => %{
+          "type" => "Image",
+          "url" => image
+        }
+      }
+    else
+      %{}
+    end
+  end
 end
index 0bc1d4728f2296f29b5c52cae442beef1b139fa0..9fb9455d2c6693c95900d4c10ec869a3d4b6caa1 100644 (file)
@@ -16,6 +16,29 @@ defmodule Pleroma.Web.ActivityPub.UserViewTest do
     assert String.contains?(result["publicKey"]["publicKeyPem"], "BEGIN PUBLIC KEY")
   end
 
+  test "Does not add an avatar image if the user hasn't set one" do
+    user = insert(:user)
+    {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
+
+    result = UserView.render("user.json", %{user: user})
+    refute result["icon"]
+    refute result["image"]
+
+    user =
+      insert(:user,
+        avatar: %{"url" => [%{"href" => "https://someurl"}]},
+        info: %{
+          banner: %{"url" => [%{"href" => "https://somebanner"}]}
+        }
+      )
+
+    {:ok, user} = Pleroma.Web.WebFinger.ensure_keys_present(user)
+
+    result = UserView.render("user.json", %{user: user})
+    assert result["icon"]["url"] == "https://someurl"
+    assert result["image"]["url"] == "https://somebanner"
+  end
+
   describe "endpoints" do
     test "local users have a usable endpoints structure" do
       user = insert(:user)