Merge branch 'remove-config-agent' into 'develop'

Get rid of Pleroma.Config in favor of Application

See merge request pleroma/pleroma!428
This commit is contained in:
lambda 2018-11-06 15:09:52 +00:00
commit 25512aa29c
7 changed files with 61 additions and 26 deletions

View File

@ -10,7 +10,6 @@ defmodule Pleroma.Application 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, []),

View File

@ -1,15 +1,26 @@
defmodule Pleroma.Config do
use Agent
def get([key]), do: get(key)
def start_link(initial) do
Agent.start_link(fn -> initial end, name: __MODULE__)
def get([parent_key | keys]) do
Application.get_env(:pleroma, parent_key)
|> get_in(keys)
end
def get(path) do
Agent.get(__MODULE__, Kernel, :get_in, [path])
def get(key) do
Application.get_env(:pleroma, key)
end
def put(path, value) do
Agent.update(__MODULE__, Kernel, :put_in, [path, value])
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

View File

@ -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

View File

@ -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 @@ defmodule Pleroma.Web.Federator 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

View File

@ -1,10 +1,39 @@
defmodule Pleroma.ConfigTest do
use Pleroma.DataCase
alias Pleroma.Config
use ExUnit.Case
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
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

View File

@ -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,14 @@ defmodule Pleroma.Web.ActivityPub.ActivityPubControllerTest do
end
test "with the relay disabled, it returns 404", %{conn: conn} do
Config.put([:instance, :allow_relay], false)
Pleroma.Config.put([:instance, :allow_relay], false)
res =
conn
|> get(activity_pub_path(conn, :relay))
|> json_response(404)
Pleroma.Config.put([:instance, :allow_relay], true)
end
end

View File

@ -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 @@ defmodule Pleroma.Web.FederatorTest do
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,15 @@ defmodule Pleroma.Web.FederatorTest do
activity: activity,
relay_mock: relay_mock
} do
Config.put([:instance, :allow_relay], false)
Pleroma.Config.put([:instance, :allow_relay], false)
with_mocks([relay_mock]) do
Federator.handle(:publish, activity)
end
refute_received :relay_publish
Pleroma.Config.put([:instance, :allow_relay], true)
end
end
end