return db key on update requests
authorAlexander Strizhakov <alex.strizhakov@gmail.com>
Fri, 17 Jan 2020 15:08:45 +0000 (18:08 +0300)
committerAlexander Strizhakov <alex.strizhakov@gmail.com>
Fri, 17 Jan 2020 15:15:41 +0000 (18:15 +0300)
lib/pleroma/config/config_db.ex
lib/pleroma/config/holder.ex
lib/pleroma/config/transfer_task.ex
lib/pleroma/web/admin_api/admin_api_controller.ex
lib/pleroma/web/admin_api/views/config_view.ex
test/config/transfer_task_test.exs
test/web/admin_api/admin_api_controller_test.exs

index 6d48e030000995dd28c13a8fd4e2a5e068f787ec..93b02370caa42e57cb647eb9d233f2b24b8deee1 100644 (file)
@@ -16,6 +16,7 @@ defmodule Pleroma.ConfigDB do
     field(:key, :string)
     field(:group, :string)
     field(:value, :binary)
+    field(:db, {:array, :string}, virtual: true, default: [])
 
     timestamps()
   end
@@ -61,6 +62,22 @@ defmodule Pleroma.ConfigDB do
     |> Repo.update()
   end
 
+  @spec get_db_keys(ConfigDB.t()) :: [String.t()]
+  def get_db_keys(%ConfigDB{} = config) do
+    config.value
+    |> ConfigDB.from_binary()
+    |> get_db_keys(config.key)
+  end
+
+  @spec get_db_keys(keyword() | any()) :: [String.t()]
+  def get_db_keys(value, key) do
+    if Keyword.keyword?(value) do
+      value |> Keyword.keys() |> Enum.map(&convert(&1))
+    else
+      [convert(key)]
+    end
+  end
+
   @full_key_update [
     {:pleroma, :ecto_repos},
     {:pleroma, :assets},
@@ -317,7 +334,7 @@ defmodule Pleroma.ConfigDB do
 
   @spec is_module_name?(String.t()) :: boolean()
   def is_module_name?(string) do
-    Regex.match?(~r/^(Pleroma|Phoenix|Tesla|Quack)\./, string) or
+    Regex.match?(~r/^(Pleroma|Phoenix|Tesla|Quack|Ueberauth)\./, string) or
       string in ["Oban", "Ueberauth", "ExSyslogger"]
   end
 end
index 0d9cfef66bea4cbe193fbcfff0703e4aabaeed60..7436df1a720c0297a72e5c5aa49f1003ad5c416b 100644 (file)
@@ -15,8 +15,8 @@ defmodule Pleroma.Config.Loader do
     # support for Elixir less than 1.9
     @spec load() :: map()
     def load do
-      {config, _paths} = load("config/config.exs")
-      {env_config, _paths} = load("config/#{Mix.env()}.exs")
+      config = load("config/config.exs")
+      env_config = load("config/#{Mix.env()}.exs")
 
       Mix.Config.merge(config, env_config)
     end
index 70b51cb4d0d4ecede601ea61a4329f0884499dc1..6e651c48ba0e72293f9cf8349205f95840c19421 100644 (file)
@@ -66,7 +66,9 @@ defmodule Pleroma.Config.TransferTask do
     rescue
       e ->
         Logger.warn(
-          "updating env causes error, key: #{inspect(setting.key)}, error: #{inspect(e)}"
+          "updating env causes error, group: #{inspect(setting.group)}, key: #{
+            inspect(setting.key)
+          }, value: #{inspect(ConfigDB.from_binary(setting.value))}, error: #{inspect(e)}"
         )
 
         nil
index cc93c2309beb6966eae01ccd805ab5f9e0652b1c..7572a6b658a1c6c3314fc403f777e15aee7f4821 100644 (file)
@@ -827,15 +827,11 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
         merged =
           Pleroma.Config.Holder.config()
           |> DeepMerge.deep_merge(configs)
-          |> Enum.map(fn {group, value} ->
-            Enum.map(value, fn {key, value} ->
+          |> Enum.map(fn {group, values} ->
+            Enum.map(values, fn {key, value} ->
               db =
                 if configs[group][key] do
-                  if Keyword.keyword?(value) do
-                    Keyword.keys(value) |> Enum.map(fn key -> ConfigDB.convert(key) end)
-                  else
-                    ConfigDB.convert(key)
-                  end
+                  ConfigDB.get_db_keys(value, key)
                 end
 
               setting = %{
@@ -871,6 +867,9 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIController do
             end
         end)
         |> Enum.reject(&is_nil(&1))
+        |> Enum.map(fn config ->
+          Map.put(config, :db, ConfigDB.get_db_keys(config))
+        end)
 
       Pleroma.Config.TransferTask.load_and_update_env()
 
index 72b042ab749313e4039d47d303302adb77b1ea74..23d97e847bb6f826104dd4a85c499bcbcc0c089a 100644 (file)
@@ -12,10 +12,16 @@ defmodule Pleroma.Web.AdminAPI.ConfigView do
   end
 
   def render("show.json", %{config: config}) do
-    %{
+    map = %{
       key: config.key,
       group: config.group,
       value: Pleroma.ConfigDB.from_binary_with_convert(config.value)
     }
+
+    if config.db != [] do
+      Map.put(map, :db, config.db)
+    else
+      map
+    end
   end
 end
index c3c4ef6740e1c8ab0403363c14368361e1f013bd..37bea20a3439f5d49f1cb1344889fa1125210ac7 100644 (file)
@@ -86,6 +86,6 @@ defmodule Pleroma.Config.TransferTaskTest do
     assert ExUnit.CaptureLog.capture_log(fn ->
              Pleroma.Config.TransferTask.start_link([])
            end) =~
-             "updating env causes error, key: \":undefined_atom_key\", error: %ArgumentError{message: \"argument error\"}"
+             "updating env causes error, group: \":pleroma\", key: \":undefined_atom_key\", value: [live: 2, com: 3], error: %ArgumentError{message: \"argument error\"}"
   end
 end
index 0206f23d6faa98133f268aee860e786ca72d32fd..2a0261b0e002547c614ee05cd684f51c8434f9c4 100644 (file)
@@ -1936,7 +1936,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
         |> ConfigDB.convert()
 
       Enum.each(received_configs, fn %{"value" => value, "db" => db} ->
-        assert db in [config1.key, config2.key, db_keys]
+        assert db in [[config1.key], [config2.key], db_keys]
 
         assert value in [
                  ConfigDB.from_binary_with_convert(config1.value),
@@ -1985,7 +1985,7 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
             %{group: ":pleroma", key: ":key1", value: "value1"},
             %{
               group: ":ueberauth",
-              key: "Ueberauth.Strategy.Twitter.OAuth",
+              key: "Ueberauth",
               value: [%{"tuple" => [":consumer_secret", "aaaa"]}]
             },
             %{
@@ -2025,12 +2025,14 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                  %{
                    "group" => ":pleroma",
                    "key" => ":key1",
-                   "value" => "value1"
+                   "value" => "value1",
+                   "db" => [":key1"]
                  },
                  %{
                    "group" => ":ueberauth",
-                   "key" => "Ueberauth.Strategy.Twitter.OAuth",
-                   "value" => [%{"tuple" => [":consumer_secret", "aaaa"]}]
+                   "key" => "Ueberauth",
+                   "value" => [%{"tuple" => [":consumer_secret", "aaaa"]}],
+                   "db" => [":consumer_secret"]
                  },
                  %{
                    "group" => ":pleroma",
@@ -2041,7 +2043,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                        %{":nested_22" => "nested_value222"},
                        %{":nested_33" => %{":nested_44" => "nested_444"}}
                      ]
-                   }
+                   },
+                   "db" => [":key2"]
                  },
                  %{
                    "group" => ":pleroma",
@@ -2049,17 +2052,20 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                    "value" => [
                      %{"nested_3" => ":nested_3", "nested_33" => "nested_33"},
                      %{"nested_4" => true}
-                   ]
+                   ],
+                   "db" => [":key3"]
                  },
                  %{
                    "group" => ":pleroma",
                    "key" => ":key4",
-                   "value" => %{"endpoint" => "https://example.com", ":nested_5" => ":upload"}
+                   "value" => %{"endpoint" => "https://example.com", ":nested_5" => ":upload"},
+                   "db" => [":key4"]
                  },
                  %{
                    "group" => ":idna",
                    "key" => ":key5",
-                   "value" => %{"tuple" => ["string", "Pleroma.Captcha.NotReal", []]}
+                   "value" => %{"tuple" => ["string", "Pleroma.Captcha.NotReal", []]},
+                   "db" => [":key5"]
                  }
                ]
              }
@@ -2121,12 +2127,23 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
 
       assert json_response(conn, 200) == %{
                "configs" => [
-                 %{"group" => ":quack", "key" => ":level", "value" => ":info"},
-                 %{"group" => ":quack", "key" => ":meta", "value" => [":none"]},
+                 %{
+                   "group" => ":quack",
+                   "key" => ":level",
+                   "value" => ":info",
+                   "db" => [":level"]
+                 },
+                 %{
+                   "group" => ":quack",
+                   "key" => ":meta",
+                   "value" => [":none"],
+                   "db" => [":meta"]
+                 },
                  %{
                    "group" => ":quack",
                    "key" => ":webhook_url",
-                   "value" => "https://hooks.slack.com/services/KEY"
+                   "value" => "https://hooks.slack.com/services/KEY",
+                   "db" => [":webhook_url"]
                  }
                ]
              }
@@ -2155,7 +2172,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                      %{"tuple" => [":key1", 1]},
                      %{"tuple" => [":key2", 2]},
                      %{"tuple" => [":key3", 3]}
-                   ]
+                   ],
+                   "db" => [":key1", ":key2", ":key3"]
                  }
                ]
              }
@@ -2205,7 +2223,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                          ]
                        ]
                      }
-                   ]
+                   ],
+                   "db" => [":key1", ":key3", ":key2"]
                  }
                ]
              }
@@ -2242,7 +2261,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                          [%{"tuple" => [":versions", [":tlsv1", ":tlsv1.1", ":tlsv1.2"]]}]
                        ]
                      }
-                   ]
+                   ],
+                   "db" => [":ssl_options"]
                  }
                ]
              }
@@ -2282,7 +2302,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                    "value" => [
                      ":console",
                      %{"tuple" => ["ExSyslogger", ":ex_syslogger"]}
-                   ]
+                   ],
+                   "db" => [":backends"]
                  }
                ]
              }
@@ -2318,7 +2339,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                  %{
                    "group" => ":tesla",
                    "key" => ":adapter",
-                   "value" => "Tesla.Adapter.Httpc"
+                   "value" => "Tesla.Adapter.Httpc",
+                   "db" => [":adapter"]
                  }
                ]
              }
@@ -2351,7 +2373,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                  %{
                    "group" => ":pleroma",
                    "key" => config1.key,
-                   "value" => "another_value"
+                   "value" => "another_value",
+                   "db" => [":keyaa1"]
                  }
                ]
              }
@@ -2384,7 +2407,11 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                 %{"tuple" => [":name", "Pleroma"]}
               ]
             },
-            %{"group" => ":tesla", "key" => ":adapter", "value" => "Tesla.Adapter.Httpc"}
+            %{
+              "group" => ":tesla",
+              "key" => ":adapter",
+              "value" => "Tesla.Adapter.Httpc"
+            }
           ]
         })
 
@@ -2408,9 +2435,27 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                      %{"tuple" => [":regex3", "~r/https:\\/\\/example.com/i"]},
                      %{"tuple" => [":regex4", "~r/https:\\/\\/example.com/s"]},
                      %{"tuple" => [":name", "Pleroma"]}
+                   ],
+                   "db" => [
+                     ":enabled",
+                     ":method",
+                     ":seconds_valid",
+                     ":path",
+                     ":key1",
+                     ":partial_chain",
+                     ":regex1",
+                     ":regex2",
+                     ":regex3",
+                     ":regex4",
+                     ":name"
                    ]
                  },
-                 %{"group" => ":tesla", "key" => ":adapter", "value" => "Tesla.Adapter.Httpc"}
+                 %{
+                   "group" => ":tesla",
+                   "key" => ":adapter",
+                   "value" => "Tesla.Adapter.Httpc",
+                   "db" => [":adapter"]
+                 }
                ]
              }
     end
@@ -2540,7 +2585,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                          ]
                        ]
                      }
-                   ]
+                   ],
+                   "db" => [":http"]
                  }
                ]
              }
@@ -2602,7 +2648,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                            }
                          ]
                        }
-                     ]
+                     ],
+                     "db" => [":key2", ":key3"]
                    }
                  ]
                }
@@ -2626,7 +2673,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                    %{
                      "group" => ":pleroma",
                      "key" => ":key1",
-                     "value" => %{"key" => "some_val"}
+                     "value" => %{"key" => "some_val"},
+                     "db" => [":key1"]
                    }
                  ]
                }
@@ -2665,6 +2713,15 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                      %{"tuple" => [":transmogrifier", 20]},
                      %{"tuple" => [":scheduled_activities", 10]},
                      %{"tuple" => [":background", 5]}
+                   ],
+                   "db" => [
+                     ":federator_incoming",
+                     ":federator_outgoing",
+                     ":web_push",
+                     ":mailer",
+                     ":transmogrifier",
+                     ":scheduled_activities",
+                     ":background"
                    ]
                  }
                ]
@@ -2695,7 +2752,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                  %{
                    "group" => ":pleroma",
                    "key" => ":keyaa1",
-                   "value" => [%{"tuple" => [":subkey2", "val2"]}]
+                   "value" => [%{"tuple" => [":subkey2", "val2"]}],
+                   "db" => [":subkey2"]
                  }
                ]
              }
@@ -2724,7 +2782,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                    "value" => [
                      %{"tuple" => [":proxy_url", %{"tuple" => [":socks5", "localhost", 1234]}]},
                      %{"tuple" => [":send_user_agent", false]}
-                   ]
+                   ],
+                   "db" => [":proxy_url", ":send_user_agent"]
                  }
                ]
              }
@@ -2753,7 +2812,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                    "value" => [
                      %{"tuple" => [":proxy_url", %{"tuple" => [":socks5", "domain.com", 1234]}]},
                      %{"tuple" => [":send_user_agent", false]}
-                   ]
+                   ],
+                   "db" => [":proxy_url", ":send_user_agent"]
                  }
                ]
              }
@@ -2782,7 +2842,8 @@ defmodule Pleroma.Web.AdminAPI.AdminAPIControllerTest do
                    "value" => [
                      %{"tuple" => [":proxy_url", %{"tuple" => [":socks5", "127.0.0.1", 1234]}]},
                      %{"tuple" => [":send_user_agent", false]}
-                   ]
+                   ],
+                   "db" => [":proxy_url", ":send_user_agent"]
                  }
                ]
              }