Pass remote follow avatar into media proxy
authorTusooa Zhu <tusooa@kazv.moe>
Sun, 10 Jul 2022 04:10:35 +0000 (00:10 -0400)
committerNorm <normandy@biribiri.dev>
Tue, 12 Jul 2022 19:44:38 +0000 (15:44 -0400)
lib/pleroma/web/twitter_api/views/remote_follow_view.ex
test/pleroma/web/twitter_api/remote_follow_controller_test.exs

index 618ba2ba58fcf0e2cf34560fa367d13d082670fe..bb24902a46b19adfb2b7be7dee9a87e60d2df14e 100644 (file)
@@ -7,5 +7,9 @@ defmodule Pleroma.Web.TwitterAPI.RemoteFollowView do
   import Phoenix.HTML.Form
   alias Pleroma.Web.Gettext
 
-  defdelegate avatar_url(user), to: Pleroma.User
+  def avatar_url(user) do
+    user
+    |> Pleroma.User.avatar_url()
+    |> Pleroma.Web.MediaProxy.url()
+  end
 end
index fa3b290063ab2a5fceaefdd89ff74a28453f9404..97c9c6b1df1fae5304977374bba4f56a317e61cb 100644 (file)
@@ -410,4 +410,49 @@ defmodule Pleroma.Web.TwitterAPI.RemoteFollowControllerTest do
       assert response =~ "Error following account"
     end
   end
+
+  describe "avatar url" do
+    test "without media proxy" do
+      clear_config([:media_proxy, :enabled], false)
+
+      user =
+        insert(:user, %{
+          local: false,
+          avatar: %{"url" => [%{"href" => "https://remote.org/avatar.png"}]}
+        })
+
+      avatar_url = Pleroma.Web.TwitterAPI.RemoteFollowView.avatar_url(user)
+
+      assert avatar_url == "https://remote.org/avatar.png"
+    end
+
+    test "with media proxy" do
+      clear_config([:media_proxy, :enabled], true)
+
+      user =
+        insert(:user, %{
+          local: false,
+          avatar: %{"url" => [%{"href" => "https://remote.org/avatar.png"}]}
+        })
+
+      avatar_url = Pleroma.Web.TwitterAPI.RemoteFollowView.avatar_url(user)
+      url = Pleroma.Web.Endpoint.url()
+
+      assert String.starts_with?(avatar_url, url)
+    end
+
+    test "local avatar is not proxied" do
+      clear_config([:media_proxy, :enabled], true)
+
+      user =
+        insert(:user, %{
+          local: true,
+          avatar: %{"url" => [%{"href" => "#{Pleroma.Web.Endpoint.url()}/localuser/avatar.png"}]}
+        })
+
+      avatar_url = Pleroma.Web.TwitterAPI.RemoteFollowView.avatar_url(user)
+
+      assert avatar_url == "#{Pleroma.Web.Endpoint.url()}/localuser/avatar.png"
+    end
+  end
 end