From 2bc924ba451b1a324663133632093914192cec2d Mon Sep 17 00:00:00 2001
From: href <href@random.sh>
Date: Tue, 6 Nov 2018 11:34:34 +0100
Subject: [PATCH 1/3] Get rid of Pleroma.Config in favor of Application

Discussed in https://git.pleroma.social/pleroma/pleroma/merge_requests/426#note_7232
---
 lib/pleroma/application.ex                       |  1 -
 lib/pleroma/config.ex                            | 15 ---------------
 .../web/activity_pub/activity_pub_controller.ex  |  3 +--
 lib/pleroma/web/federator/federator.ex           |  3 +--
 test/config_test.exs                             | 10 ----------
 .../activity_pub_controller_test.exs             | 16 ++++++++++++----
 test/web/federator_test.exs                      | 15 +++++++++++----
 7 files changed, 25 insertions(+), 38 deletions(-)
 delete mode 100644 lib/pleroma/config.ex
 delete mode 100644 test/config_test.exs

diff --git a/lib/pleroma/application.ex b/lib/pleroma/application.ex
index d4bc8f63d..eedad7675 100644
--- a/lib/pleroma/application.ex
+++ b/lib/pleroma/application.ex
@@ -10,7 +10,6 @@ def start(_type, _args) do
     # Define workers and child supervisors to be supervised
     children =
       [
-        worker(Pleroma.Config, [Application.get_all_env(:pleroma)]),
         # Start the Ecto repository
         supervisor(Pleroma.Repo, []),
         worker(Pleroma.Emoji, []),
diff --git a/lib/pleroma/config.ex b/lib/pleroma/config.ex
deleted file mode 100644
index 510d8d498..000000000
--- a/lib/pleroma/config.ex
+++ /dev/null
@@ -1,15 +0,0 @@
-defmodule Pleroma.Config do
-  use Agent
-
-  def start_link(initial) do
-    Agent.start_link(fn -> initial end, name: __MODULE__)
-  end
-
-  def get(path) do
-    Agent.get(__MODULE__, Kernel, :get_in, [path])
-  end
-
-  def put(path, value) do
-    Agent.update(__MODULE__, Kernel, :put_in, [path, value])
-  end
-end
diff --git a/lib/pleroma/web/activity_pub/activity_pub_controller.ex b/lib/pleroma/web/activity_pub/activity_pub_controller.ex
index 531e98237..47937beef 100644
--- a/lib/pleroma/web/activity_pub/activity_pub_controller.ex
+++ b/lib/pleroma/web/activity_pub/activity_pub_controller.ex
@@ -6,7 +6,6 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
   alias Pleroma.Web.ActivityPub.Relay
   alias Pleroma.Web.ActivityPub.Utils
   alias Pleroma.Web.Federator
-  alias Pleroma.Config
 
   require Logger
 
@@ -15,7 +14,7 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubController do
   plug(:relay_active? when action in [:relay])
 
   def relay_active?(conn, _) do
-    if Config.get([:instance, :allow_relay]) do
+    if Keyword.get(Application.get_env(:pleroma, :instance), :allow_relay) do
       conn
     else
       conn
diff --git a/lib/pleroma/web/federator/federator.ex b/lib/pleroma/web/federator/federator.ex
index 9ea2507a1..01c2c89c3 100644
--- a/lib/pleroma/web/federator/federator.ex
+++ b/lib/pleroma/web/federator/federator.ex
@@ -7,7 +7,6 @@ defmodule Pleroma.Web.Federator do
   alias Pleroma.Web.ActivityPub.Relay
   alias Pleroma.Web.ActivityPub.Transmogrifier
   alias Pleroma.Web.ActivityPub.Utils
-  alias Pleroma.Config
   require Logger
 
   @websub Application.get_env(:pleroma, :websub)
@@ -72,7 +71,7 @@ def handle(:publish, activity) do
         Logger.info(fn -> "Sending #{activity.data["id"]} out via Salmon" end)
         Pleroma.Web.Salmon.publish(actor, activity)
 
-        if Config.get([:instance, :allow_relay]) do
+        if Keyword.get(Application.get_env(:pleroma, :instance), :allow_relay) do
           Logger.info(fn -> "Relaying #{activity.data["id"]} out" end)
           Relay.publish(activity)
         end
diff --git a/test/config_test.exs b/test/config_test.exs
deleted file mode 100644
index 6d0f0a2d4..000000000
--- a/test/config_test.exs
+++ /dev/null
@@ -1,10 +0,0 @@
-defmodule Pleroma.ConfigTest do
-  use Pleroma.DataCase
-  alias Pleroma.Config
-
-  test "get returns the item at the path if there is one" do
-    Config.put([:instance, :name], "Plemora")
-    assert Config.get([:instance, :name]) == "Plemora"
-    assert Config.get([:unknown]) == nil
-  end
-end
diff --git a/test/web/activity_pub/activity_pub_controller_test.exs b/test/web/activity_pub/activity_pub_controller_test.exs
index 5b46bbe76..524ed9eaa 100644
--- a/test/web/activity_pub/activity_pub_controller_test.exs
+++ b/test/web/activity_pub/activity_pub_controller_test.exs
@@ -4,12 +4,9 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
   alias Pleroma.Web.ActivityPub.{UserView, ObjectView}
   alias Pleroma.{Repo, User}
   alias Pleroma.Activity
-  alias Pleroma.Config
 
   describe "/relay" do
     test "with the relay active, it returns the relay user", %{conn: conn} do
-      Config.put([:instance, :allow_relay], true)
-
       res =
         conn
         |> get(activity_pub_path(conn, :relay))
@@ -19,12 +16,23 @@ test "with the relay active, it returns the relay user", %{conn: conn} do
     end
 
     test "with the relay disabled, it returns 404", %{conn: conn} do
-      Config.put([:instance, :allow_relay], false)
+      instance =
+        Application.get_env(:pleroma, :instance)
+        |> Keyword.put(:allow_relay, false)
+
+      Application.put_env(:pleroma, :instance, instance)
 
       res =
         conn
         |> get(activity_pub_path(conn, :relay))
         |> json_response(404)
+
+      instance =
+        Application.get_env(:pleroma, :instance)
+        |> Keyword.put(:allow_relay, true)
+
+      Application.put_env(:pleroma, :instance, instance)
+
     end
   end
 
diff --git a/test/web/federator_test.exs b/test/web/federator_test.exs
index 966702935..88aef0d0f 100644
--- a/test/web/federator_test.exs
+++ b/test/web/federator_test.exs
@@ -1,7 +1,6 @@
 defmodule Pleroma.Web.FederatorTest do
   alias Pleroma.Web.Federator
   alias Pleroma.Web.CommonAPI
-  alias Pleroma.Config
   use Pleroma.DataCase
   import Pleroma.Factory
   import Mock
@@ -40,8 +39,6 @@ test "with relays active, it publishes to the relay", %{
       activity: activity,
       relay_mock: relay_mock
     } do
-      Config.put([:instance, :allow_relay], true)
-
       with_mocks([relay_mock]) do
         Federator.handle(:publish, activity)
       end
@@ -53,13 +50,23 @@ test "with relays deactivated, it does not publish to the relay", %{
       activity: activity,
       relay_mock: relay_mock
     } do
-      Config.put([:instance, :allow_relay], false)
+      instance =
+        Application.get_env(:pleroma, :instance)
+        |> Keyword.put(:allow_relay, false)
+
+      Application.put_env(:pleroma, :instance, instance)
 
       with_mocks([relay_mock]) do
         Federator.handle(:publish, activity)
       end
 
       refute_received :relay_publish
+
+      instance =
+        Application.get_env(:pleroma, :instance)
+        |> Keyword.put(:allow_relay, true)
+
+      Application.put_env(:pleroma, :instance, instance)
     end
   end
 end

From 36ca3c1b3ec814609d14815a672239a31b1e0ec5 Mon Sep 17 00:00:00 2001
From: href <href@random.sh>
Date: Tue, 6 Nov 2018 15:17:13 +0100
Subject: [PATCH 2/3] format

---
 test/web/activity_pub/activity_pub_controller_test.exs | 1 -
 1 file changed, 1 deletion(-)

diff --git a/test/web/activity_pub/activity_pub_controller_test.exs b/test/web/activity_pub/activity_pub_controller_test.exs
index 524ed9eaa..f22975f86 100644
--- a/test/web/activity_pub/activity_pub_controller_test.exs
+++ b/test/web/activity_pub/activity_pub_controller_test.exs
@@ -32,7 +32,6 @@ test "with the relay disabled, it returns 404", %{conn: conn} do
         |> Keyword.put(:allow_relay, true)
 
       Application.put_env(:pleroma, :instance, instance)
-
     end
   end
 

From 7d328c658da69ec236d10fa89d23f2a6886b3205 Mon Sep 17 00:00:00 2001
From: href <href@random.sh>
Date: Tue, 6 Nov 2018 16:00:48 +0100
Subject: [PATCH 3/3] Small wrapper module around Application.get_env/put_env

Same API as the old Pleroma.Config
---
 lib/pleroma/config.ex                         | 26 +++++++++++++
 test/config_test.exs                          | 39 +++++++++++++++++++
 .../activity_pub_controller_test.exs          | 12 +-----
 test/web/federator_test.exs                   | 12 +-----
 4 files changed, 69 insertions(+), 20 deletions(-)
 create mode 100644 lib/pleroma/config.ex
 create mode 100644 test/config_test.exs

diff --git a/lib/pleroma/config.ex b/lib/pleroma/config.ex
new file mode 100644
index 000000000..fc5338591
--- /dev/null
+++ b/lib/pleroma/config.ex
@@ -0,0 +1,26 @@
+defmodule Pleroma.Config do
+  def get([key]), do: get(key)
+
+  def get([parent_key | keys]) do
+    Application.get_env(:pleroma, parent_key)
+    |> get_in(keys)
+  end
+
+  def get(key) do
+    Application.get_env(:pleroma, key)
+  end
+
+  def put([key], value), do: put(key, value)
+
+  def put([parent_key | keys], value) do
+    parent =
+      Application.get_env(:pleroma, parent_key)
+      |> put_in(keys, value)
+
+    Application.put_env(:pleroma, parent_key, parent)
+  end
+
+  def put(key, value) do
+    Application.put_env(:pleroma, key, value)
+  end
+end
diff --git a/test/config_test.exs b/test/config_test.exs
new file mode 100644
index 000000000..32d5cc90c
--- /dev/null
+++ b/test/config_test.exs
@@ -0,0 +1,39 @@
+defmodule Pleroma.ConfigTest do
+  use ExUnit.Case
+
+  test "get/1 with an atom" do
+    assert Pleroma.Config.get(:instance) == Application.get_env(:pleroma, :instance)
+    assert Pleroma.Config.get(:azertyuiop) == nil
+  end
+
+  test "get/1 with a list of keys" do
+    assert Pleroma.Config.get([:instance, :public]) ==
+             Keyword.get(Application.get_env(:pleroma, :instance), :public)
+
+    assert Pleroma.Config.get([Pleroma.Web.Endpoint, :render_errors, :view]) ==
+             get_in(
+               Application.get_env(
+                 :pleroma,
+                 Pleroma.Web.Endpoint
+               ),
+               [:render_errors, :view]
+             )
+
+    assert Pleroma.Config.get([:azerty, :uiop]) == nil
+  end
+
+  test "put/2 with a key" do
+    Pleroma.Config.put(:config_test, true)
+
+    assert Pleroma.Config.get(:config_test) == true
+  end
+
+  test "put/2 with a list of keys" do
+    Pleroma.Config.put([:instance, :config_test], true)
+    Pleroma.Config.put([:instance, :config_nested_test], [])
+    Pleroma.Config.put([:instance, :config_nested_test, :x], true)
+
+    assert Pleroma.Config.get([:instance, :config_test]) == true
+    assert Pleroma.Config.get([:instance, :config_nested_test, :x]) == true
+  end
+end
diff --git a/test/web/activity_pub/activity_pub_controller_test.exs b/test/web/activity_pub/activity_pub_controller_test.exs
index f22975f86..1c24b348c 100644
--- a/test/web/activity_pub/activity_pub_controller_test.exs
+++ b/test/web/activity_pub/activity_pub_controller_test.exs
@@ -16,22 +16,14 @@ test "with the relay active, it returns the relay user", %{conn: conn} do
     end
 
     test "with the relay disabled, it returns 404", %{conn: conn} do
-      instance =
-        Application.get_env(:pleroma, :instance)
-        |> Keyword.put(:allow_relay, false)
-
-      Application.put_env(:pleroma, :instance, instance)
+      Pleroma.Config.put([:instance, :allow_relay], false)
 
       res =
         conn
         |> get(activity_pub_path(conn, :relay))
         |> json_response(404)
 
-      instance =
-        Application.get_env(:pleroma, :instance)
-        |> Keyword.put(:allow_relay, true)
-
-      Application.put_env(:pleroma, :instance, instance)
+      Pleroma.Config.put([:instance, :allow_relay], true)
     end
   end
 
diff --git a/test/web/federator_test.exs b/test/web/federator_test.exs
index 88aef0d0f..c709d1181 100644
--- a/test/web/federator_test.exs
+++ b/test/web/federator_test.exs
@@ -50,11 +50,7 @@ test "with relays deactivated, it does not publish to the relay", %{
       activity: activity,
       relay_mock: relay_mock
     } do
-      instance =
-        Application.get_env(:pleroma, :instance)
-        |> Keyword.put(:allow_relay, false)
-
-      Application.put_env(:pleroma, :instance, instance)
+      Pleroma.Config.put([:instance, :allow_relay], false)
 
       with_mocks([relay_mock]) do
         Federator.handle(:publish, activity)
@@ -62,11 +58,7 @@ test "with relays deactivated, it does not publish to the relay", %{
 
       refute_received :relay_publish
 
-      instance =
-        Application.get_env(:pleroma, :instance)
-        |> Keyword.put(:allow_relay, true)
-
-      Application.put_env(:pleroma, :instance, instance)
+      Pleroma.Config.put([:instance, :allow_relay], true)
     end
   end
 end