Fix depreciated calls of fetch_env!/2 and get_env/2

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2022-09-21 08:53:50 +02:00
parent 655ae4f9a1
commit bbbaa81a6e
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
10 changed files with 45 additions and 27 deletions

View File

@ -83,7 +83,7 @@ defmodule Mobilizon.Federation.ActivityPub.Federator do
def enqueue(type, payload, priority \\ 1) do def enqueue(type, payload, priority \\ 1) do
Logger.debug("enqueue something with type #{inspect(type)}") Logger.debug("enqueue something with type #{inspect(type)}")
if Application.fetch_env!(:mobilizon, :env) == :test do if Application.compile_env(:mobilizon, :env) == :test do
handle(type, payload) handle(type, payload)
else else
GenServer.cast(__MODULE__, {:enqueue, type, payload, priority}) GenServer.cast(__MODULE__, {:enqueue, type, payload, priority})

View File

@ -205,7 +205,7 @@ defmodule Mobilizon.Federation.WebFinger do
{:ok, String.t()} | {:error, :link_not_found} | {:error, any()} {:ok, String.t()} | {:error, :link_not_found} | {:error, any()}
defp find_webfinger_endpoint(domain) when is_binary(domain) do defp find_webfinger_endpoint(domain) when is_binary(domain) do
Logger.debug("Calling HostMetaClient for #{domain}") Logger.debug("Calling HostMetaClient for #{domain}")
prefix = if Application.fetch_env!(:mobilizon, :env) !== :dev, do: "https", else: "http" prefix = if Application.compile_env(:mobilizon, :env) !== :dev, do: "https", else: "http"
with {:ok, %Tesla.Env{status: 200, body: body}} <- with {:ok, %Tesla.Env{status: 200, body: body}} <-
HostMetaClient.get("#{prefix}://#{domain}/.well-known/host-meta"), HostMetaClient.get("#{prefix}://#{domain}/.well-known/host-meta"),
@ -229,7 +229,10 @@ defmodule Mobilizon.Federation.WebFinger do
_ -> _ ->
Logger.debug("Using default webfinger location") Logger.debug("Using default webfinger location")
prefix = if Application.fetch_env!(:mobilizon, :env) !== :dev, do: "https", else: "http"
prefix =
if Application.compile_env(:mobilizon, :env) !== :dev, do: "https", else: "http"
"#{prefix}://#{domain}/.well-known/webfinger?resource=acct:#{actor}" "#{prefix}://#{domain}/.well-known/webfinger?resource=acct:#{actor}"
end end
end end

View File

@ -13,7 +13,7 @@ defmodule Mix.Tasks.Mobilizon.Common do
def start_mobilizon do def start_mobilizon do
if mix_task?(), do: Mix.Task.run("app.config") if mix_task?(), do: Mix.Task.run("app.config")
unless System.get_env("DEBUG") || Application.fetch_env!(:mobilizon, :env) == :test do unless System.get_env("DEBUG") || Application.compile_env(:mobilizon, :env) == :test do
Logger.configure(level: :error) Logger.configure(level: :error)
end end
@ -73,7 +73,7 @@ defmodule Mix.Tasks.Mobilizon.Common do
IO.puts(:stderr, message) IO.puts(:stderr, message)
end end
if Application.fetch_env!(:mobilizon, :env) != :test do if Application.compile_env(:mobilizon, :env) != :test do
exit({:shutdown, Keyword.get(options, :error_code, 1)}) exit({:shutdown, Keyword.get(options, :error_code, 1)})
end end
end end

View File

@ -21,7 +21,7 @@ defmodule Mobilizon do
@name Mix.Project.config()[:name] @name Mix.Project.config()[:name]
@version Mix.Project.config()[:version] @version Mix.Project.config()[:version]
@env Application.fetch_env!(:mobilizon, :env) @env Application.compile_env(:mobilizon, :env)
@spec named_version :: String.t() @spec named_version :: String.t()
def named_version, do: "#{@name} #{@version}" def named_version, do: "#{@name} #{@version}"

View File

@ -4,10 +4,10 @@ defmodule Mobilizon.Cldr do
""" """
use Cldr, use Cldr,
locales: Application.get_env(:mobilizon, :cldr)[:locales], locales: locales(),
add_fallback_locales: true, add_fallback_locales: true,
gettext: gettext:
if(Application.fetch_env!(:mobilizon, :env) == :prod, if(Application.compile_env(:mobilizon, :env) == :prod,
do: Mobilizon.Web.Gettext, do: Mobilizon.Web.Gettext,
else: nil else: nil
), ),
@ -27,4 +27,8 @@ defmodule Mobilizon.Cldr do
default default
end end
end end
defp locales do
Application.get_env(:mobilizon, :cldr)[:locales]
end
end end

View File

@ -30,7 +30,7 @@ defmodule Mobilizon.Service.Activity.Utils do
@spec maybe_inserted_at :: map() @spec maybe_inserted_at :: map()
def maybe_inserted_at do def maybe_inserted_at do
if Application.fetch_env!(:mobilizon, :env) == :test do if Application.compile_env(:mobilizon, :env) == :test do
%{} %{}
else else
%{"inserted_at" => DateTime.utc_now()} %{"inserted_at" => DateTime.utc_now()}

View File

@ -8,12 +8,10 @@ defmodule Mobilizon.Web.AuthController do
require Logger require Logger
plug(:put_layout, false) plug(:put_layout, false)
config = Application.get_env(:mobilizon, Mobilizon.Web.Endpoint, [])
plug(Plug.Session, plug(Plug.Session,
store: :cookie, store: :cookie,
key: "_auth_callback", key: "_auth_callback",
signing_salt: Keyword.get(config, :secret_key_base) signing_salt: Keyword.get(endpoint_config, :secret_key_base)
) )
plug(Ueberauth) plug(Ueberauth)
@ -142,4 +140,8 @@ defmodule Mobilizon.Web.AuthController do
defp redirect_to_error(conn, :unknown_error, provider_name) do defp redirect_to_error(conn, :unknown_error, provider_name) do
redirect(conn, to: "/login?code=Error with Login Provider&provider=#{provider_name}") redirect(conn, to: "/login?code=Error with Login Provider&provider=#{provider_name}")
end end
defp endpoint_config do
Application.get_env(:mobilizon, Mobilizon.Web.Endpoint, [])
end
end end

View File

@ -3,8 +3,7 @@ defmodule Mobilizon.Web.Endpoint do
Endpoint for Mobilizon app Endpoint for Mobilizon app
""" """
if Application.fetch_env!(:mobilizon, :env) !== :test && if Application.compile_env(:mobilizon, :env) !== :test && sentry_dsn_config() != nil do
Application.get_env(:sentry, :dsn) != nil do
use Sentry.PlugCapture use Sentry.PlugCapture
end end
@ -13,7 +12,7 @@ defmodule Mobilizon.Web.Endpoint do
plug(Mobilizon.Web.Plugs.DetectLocalePlug) plug(Mobilizon.Web.Plugs.DetectLocalePlug)
if Application.fetch_env!(:mobilizon, :env) !== :dev do if Application.compile_env(:mobilizon, :env) !== :dev do
plug(Mobilizon.Web.Plugs.HTTPSecurityPlug) plug(Mobilizon.Web.Plugs.HTTPSecurityPlug)
end end
@ -31,9 +30,7 @@ defmodule Mobilizon.Web.Endpoint do
longpoll: false longpoll: false
) )
endpoint_config = Application.get_env(:mobilizon, Mobilizon.Web.Endpoint) if Keyword.get(endpoint_config(), :has_reverse_proxy, false) == true do
if Keyword.get(endpoint_config, :has_reverse_proxy, false) == true do
plug(RemoteIp) plug(RemoteIp)
end end
@ -64,8 +61,7 @@ defmodule Mobilizon.Web.Endpoint do
plug(Plug.RequestId) plug(Plug.RequestId)
plug(Plug.Logger) plug(Plug.Logger)
upload_limit = upload_limit = Keyword.get(instance_config(), :upload_limit, 10_485_760)
Keyword.get(Application.get_env(:mobilizon, :instance, []), :upload_limit, 10_485_760)
plug( plug(
Plug.Parsers, Plug.Parsers,
@ -83,8 +79,19 @@ defmodule Mobilizon.Web.Endpoint do
String.replace_leading(url(), "http", "ws") String.replace_leading(url(), "http", "ws")
end end
if Application.fetch_env!(:mobilizon, :env) !== :test && defp sentry_dsn_config do
Application.get_env(:sentry, :dsn) != nil do Application.get_env(:sentry, :dsn)
end
defp endpoint_config do
Application.get_env(:mobilizon, Mobilizon.Web.Endpoint)
end
defp instance_config do
Application.get_env(:mobilizon, :instance, [])
end
if Application.compile_env(:mobilizon, :env) !== :test && sentry_dsn_config() != nil do
plug(Sentry.PlugContext) plug(Sentry.PlugContext)
end end
end end

View File

@ -83,8 +83,6 @@ defmodule Mobilizon.Web.ReverseProxy do
| {:inline_content_types, boolean | [String.t()]} | {:inline_content_types, boolean | [String.t()]}
| {:redirect_on_failure, boolean} | {:redirect_on_failure, boolean}
@hackney Application.get_env(:mobilizon, :hackney, :hackney)
@default_hackney_options [] @default_hackney_options []
@inline_content_types [ @inline_content_types [
@ -171,7 +169,7 @@ defmodule Mobilizon.Web.ReverseProxy do
Logger.debug("#{__MODULE__} #{method} #{url} #{inspect(headers)}") Logger.debug("#{__MODULE__} #{method} #{url} #{inspect(headers)}")
method = method |> String.downcase() |> String.to_existing_atom() method = method |> String.downcase() |> String.to_existing_atom()
case @hackney.request(method, url, headers, "", hackney_opts) do case hackney().request(method, url, headers, "", hackney_opts) do
{:ok, code, headers, client} when code in @valid_resp_codes -> {:ok, code, headers, client} when code in @valid_resp_codes ->
{:ok, code, downcase_headers(headers), client} {:ok, code, downcase_headers(headers), client}
@ -231,7 +229,7 @@ defmodule Mobilizon.Web.ReverseProxy do
duration, duration,
Keyword.get(opts, :max_read_duration, @max_read_duration) Keyword.get(opts, :max_read_duration, @max_read_duration)
), ),
{:ok, data} <- @hackney.stream_body(client), {:ok, data} <- hackney().stream_body(client),
{:ok, duration} <- increase_read_duration({duration, now}), {:ok, duration} <- increase_read_duration({duration, now}),
sent_so_far = sent_so_far + byte_size(data), sent_so_far = sent_so_far + byte_size(data),
:ok <- body_size_constraint(sent_so_far, Keyword.get(opts, :max_body_size)), :ok <- body_size_constraint(sent_so_far, Keyword.get(opts, :max_body_size)),
@ -452,4 +450,8 @@ defmodule Mobilizon.Web.ReverseProxy do
uri = URI.parse(url) uri = URI.parse(url)
uri.scheme != nil && uri.host =~ "." uri.scheme != nil && uri.host =~ "."
end end
defp hackney do
Application.get_env(:mobilizon, :hackney, :hackney)
end
end end

View File

@ -212,7 +212,7 @@ defmodule Mobilizon.Web.Router do
get("/:sig/:url/:filename", MediaProxyController, :remote) get("/:sig/:url/:filename", MediaProxyController, :remote)
end end
if Application.fetch_env!(:mobilizon, :env) in [:dev, :e2e] do if Application.compile_env(:mobilizon, :env) in [:dev, :e2e] do
# If using Phoenix # If using Phoenix
forward("/sent_emails", Plug.Swoosh.MailboxPreview) forward("/sent_emails", Plug.Swoosh.MailboxPreview)
end end