From 312fc55f14e1b7f88ec43b72c577bf5df595beac Mon Sep 17 00:00:00 2001
From: "Haelwenn (lanodan) Monnier" <contact@hacktivis.me>
Date: Wed, 8 Jul 2020 05:56:24 +0200
Subject: [PATCH] Add [:instances_favicons, :enabled] setting, defaults to
 false

---
 config/config.exs                             |  2 ++
 config/description.exs                        | 13 ++++++++++++
 config/test.exs                               |  2 ++
 docs/configuration/cheatsheet.md              |  6 ++++++
 .../web/mastodon_api/views/account_view.ex    | 16 +++++++++------
 .../mastodon_api/views/account_view_test.exs  | 20 +++++++++++++++++++
 6 files changed, 53 insertions(+), 6 deletions(-)

diff --git a/config/config.exs b/config/config.exs
index 458d3a99a..3577cd101 100644
--- a/config/config.exs
+++ b/config/config.exs
@@ -706,6 +706,8 @@
 
 config :ex_aws, http_client: Pleroma.HTTP.ExAws
 
+config :pleroma, :instances_favicons, enabled: false
+
 # Import environment specific config. This must remain at the bottom
 # of this file so it overrides the configuration defined above.
 import_config "#{Mix.env()}.exs"
diff --git a/config/description.exs b/config/description.exs
index 650610fbe..7c432d67d 100644
--- a/config/description.exs
+++ b/config/description.exs
@@ -3447,5 +3447,18 @@
         suggestions: [false]
       }
     ]
+  },
+  %{
+    group: :pleroma,
+    key: :instances_favicons,
+    type: :group,
+    description: "Control favicons for instances",
+    children: [
+      %{
+        key: :enabled,
+        type: :boolean,
+        description: "Allow/disallow displaying and getting instances favicons"
+      }
+    ]
   }
 ]
diff --git a/config/test.exs b/config/test.exs
index e38b9967d..e6596e0bc 100644
--- a/config/test.exs
+++ b/config/test.exs
@@ -111,6 +111,8 @@
 
 config :pleroma, Pleroma.Web.ApiSpec.CastAndValidate, strict: true
 
+config :pleroma, :instances_favicons, enabled: true
+
 if File.exists?("./config/test.secret.exs") do
   import_config "test.secret.exs"
 else
diff --git a/docs/configuration/cheatsheet.md b/docs/configuration/cheatsheet.md
index 6b640cebc..7a3200e01 100644
--- a/docs/configuration/cheatsheet.md
+++ b/docs/configuration/cheatsheet.md
@@ -987,3 +987,9 @@ Restrict access for unauthenticated users to timelines (public and federate), us
 ## Pleroma.Web.ApiSpec.CastAndValidate
 
 * `:strict` a boolean, enables strict input validation (useful in development, not recommended in production). Defaults to `false`.
+
+## :instances_favicons
+
+Control favicons for instances.
+
+* `enabled`: Allow/disallow displaying and getting instances favicons
diff --git a/lib/pleroma/web/mastodon_api/views/account_view.ex b/lib/pleroma/web/mastodon_api/views/account_view.ex
index db5739254..2feba4778 100644
--- a/lib/pleroma/web/mastodon_api/views/account_view.ex
+++ b/lib/pleroma/web/mastodon_api/views/account_view.ex
@@ -205,12 +205,16 @@ defp do_render("show.json", %{user: user} = opts) do
       end
 
     favicon =
-      user
-      |> Map.get(:ap_id, "")
-      |> URI.parse()
-      |> URI.merge("/")
-      |> Pleroma.Instances.Instance.get_or_update_favicon()
-      |> MediaProxy.url()
+      if Pleroma.Config.get([:instances_favicons, :enabled]) do
+        user
+        |> Map.get(:ap_id, "")
+        |> URI.parse()
+        |> URI.merge("/")
+        |> Pleroma.Instances.Instance.get_or_update_favicon()
+        |> MediaProxy.url()
+      else
+        nil
+      end
 
     %{
       id: to_string(user.id),
diff --git a/test/web/mastodon_api/views/account_view_test.exs b/test/web/mastodon_api/views/account_view_test.exs
index c4341cb28..ac6d50e3a 100644
--- a/test/web/mastodon_api/views/account_view_test.exs
+++ b/test/web/mastodon_api/views/account_view_test.exs
@@ -5,6 +5,7 @@
 defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
   use Pleroma.DataCase
 
+  alias Pleroma.Config
   alias Pleroma.User
   alias Pleroma.UserRelationship
   alias Pleroma.Web.CommonAPI
@@ -18,6 +19,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
     :ok
   end
 
+  setup do: clear_config([:instances_favicons, :enabled])
+
   test "Represent a user account" do
     background_image = %{
       "url" => [%{"href" => "https://example.com/images/asuka_hospital.png"}]
@@ -94,6 +97,23 @@ test "Represent a user account" do
     assert expected == AccountView.render("show.json", %{user: user})
   end
 
+  test "Favicon is nil when :instances_favicons is disabled" do
+    user = insert(:user)
+
+    Config.put([:instances_favicons, :enabled], true)
+
+    assert %{
+             pleroma: %{
+               favicon:
+                 "https://shitposter.club/plugins/Qvitter/img/gnusocial-favicons/favicon-16x16.png"
+             }
+           } = AccountView.render("show.json", %{user: user})
+
+    Config.put([:instances_favicons, :enabled], false)
+
+    assert %{pleroma: %{favicon: nil}} = AccountView.render("show.json", %{user: user})
+  end
+
   test "Represent the user account for the account owner" do
     user = insert(:user)