summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Gleason <alex@alexgleason.me>2021-06-08 14:04:46 -0500
committerAlex Gleason <alex@alexgleason.me>2021-06-08 14:17:57 -0500
commitd369142a8e11b02066b734ef8900b27d7cd52bad (patch)
treee3224d2e4e4c6969a4c9e7046a6397553791a5ad
parentbed10ab2c70848756740f251c1d54bdca02dd093 (diff)
Introduce Pleroma.Config.url/0 to bypass compile-time deps on Endpointcycles-context
-rw-r--r--lib/pleroma/config.ex21
-rw-r--r--lib/pleroma/ecto_type/activity_pub/object_validators/context_id.ex4
-rw-r--r--test/pleroma/config_test.exs22
3 files changed, 45 insertions, 2 deletions
diff --git a/lib/pleroma/config.ex b/lib/pleroma/config.ex
index 54e332595..2709f39bf 100644
--- a/lib/pleroma/config.ex
+++ b/lib/pleroma/config.ex
@@ -103,4 +103,25 @@ defmodule Pleroma.Config do
def feature_enabled?(feature_name) do
get([:features, feature_name]) not in [nil, false, :disabled, :auto]
end
+
+ @doc """
+ Get the URI directly from application config, bypassing the Endpoint module.
+ """
+ def uri do
+ # `Pleroma.Web.Endpoint` is only being used as a key here (for equality check),
+ # so it's okay to use `Module.concat/1` to have the compiler ignore it.
+ endpoint_key = Module.concat(["Pleroma.Web.Endpoint"])
+
+ url =
+ get([endpoint_key, :url])
+ |> Map.new()
+
+ struct(URI, url)
+ end
+
+ @doc """
+ Similar to `Pleroma.Web.Endpoint.url/0`.
+ May be used where needed to avoid a compile-time dep on Endpoint.
+ """
+ def url, do: URI.to_string(uri())
end
diff --git a/lib/pleroma/ecto_type/activity_pub/object_validators/context_id.ex b/lib/pleroma/ecto_type/activity_pub/object_validators/context_id.ex
index 15f47d45f..880749266 100644
--- a/lib/pleroma/ecto_type/activity_pub/object_validators/context_id.ex
+++ b/lib/pleroma/ecto_type/activity_pub/object_validators/context_id.ex
@@ -6,7 +6,7 @@ defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.ContextID do
use Ecto.Type
alias Ecto.UUID
- alias Pleroma.Web.Endpoint
+ alias Pleroma.Config
def type, do: :string
@@ -19,6 +19,6 @@ defmodule Pleroma.EctoType.ActivityPub.ObjectValidators.ContextID do
def load(data), do: {:ok, data}
def autogenerate do
- "#{Endpoint.url()}/contexts/#{UUID.generate()}"
+ "#{Config.url()}/contexts/#{UUID.generate()}"
end
end
diff --git a/test/pleroma/config_test.exs b/test/pleroma/config_test.exs
index 3158a2ec8..2a1bad84d 100644
--- a/test/pleroma/config_test.exs
+++ b/test/pleroma/config_test.exs
@@ -136,4 +136,26 @@ defmodule Pleroma.ConfigTest do
Pleroma.Config.delete([:lorem])
Pleroma.Config.delete([:ipsum])
end
+
+ describe "URI functions" do
+ setup do
+ url = [host: "lain.com", scheme: "https", port: 443]
+ clear_config([Pleroma.Web.Endpoint, :url], url)
+ end
+
+ test "uri/0" do
+ expected = %URI{
+ scheme: "https",
+ host: "lain.com",
+ port: 443
+ }
+
+ assert Pleroma.Config.uri() == expected
+ end
+
+ test "url/0" do
+ expected = "https://lain.com"
+ assert Pleroma.Config.url() == expected
+ end
+ end
end