refactor: fix review comments
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
93e1ec7414
commit
b1740e3435
10 changed files with 63 additions and 199 deletions
|
|
@ -470,11 +470,19 @@ defmodule Mv.Config do
|
|||
# ---------------------------------------------------------------------------
|
||||
|
||||
@doc """
|
||||
Returns SMTP host. ENV `SMTP_HOST` overrides Settings.
|
||||
Returns SMTP host.
|
||||
|
||||
Policy:
|
||||
- ENV-only mode (`SMTP_HOST` set): read from ENV `SMTP_HOST`
|
||||
- Settings mode: read from Settings only
|
||||
"""
|
||||
@spec smtp_host() :: String.t() | nil
|
||||
def smtp_host do
|
||||
smtp_env_or_setting("SMTP_HOST", :smtp_host)
|
||||
if smtp_env_mode?() do
|
||||
System.get_env("SMTP_HOST") |> trim_nil()
|
||||
else
|
||||
get_from_settings(:smtp_host)
|
||||
end
|
||||
end
|
||||
|
||||
@doc """
|
||||
|
|
@ -522,7 +530,7 @@ defmodule Mv.Config do
|
|||
def smtp_password do
|
||||
if smtp_env_mode?() do
|
||||
case System.get_env("SMTP_PASSWORD") do
|
||||
nil -> smtp_password_from_file_or_settings()
|
||||
nil -> smtp_password_from_file()
|
||||
value -> trim_nil(value)
|
||||
end
|
||||
else
|
||||
|
|
@ -530,7 +538,7 @@ defmodule Mv.Config do
|
|||
end
|
||||
end
|
||||
|
||||
defp smtp_password_from_file_or_settings do
|
||||
defp smtp_password_from_file do
|
||||
case System.get_env("SMTP_PASSWORD_FILE") do
|
||||
nil -> nil
|
||||
path -> read_smtp_password_file(path)
|
||||
|
|
@ -568,14 +576,6 @@ defmodule Mv.Config do
|
|||
present?(smtp_host())
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns true when SMTP ENV mode is active.
|
||||
"""
|
||||
@spec smtp_env_configured?() :: boolean()
|
||||
def smtp_env_configured? do
|
||||
smtp_env_mode?()
|
||||
end
|
||||
|
||||
@doc """
|
||||
Returns true when SMTP is managed by environment variables.
|
||||
|
||||
|
|
@ -599,6 +599,7 @@ defmodule Mv.Config do
|
|||
[]
|
||||
|> maybe_add_missing("SMTP_USERNAME", smtp_username_env_set?())
|
||||
|> maybe_add_missing("SMTP_PASSWORD/SMTP_PASSWORD_FILE", smtp_password_env_set?())
|
||||
|> Enum.reverse()
|
||||
else
|
||||
[]
|
||||
end
|
||||
|
|
@ -665,8 +666,6 @@ defmodule Mv.Config do
|
|||
@spec mail_from_email_env_set?() :: boolean()
|
||||
def mail_from_email_env_set?, do: env_set?("MAIL_FROM_EMAIL")
|
||||
|
||||
defp parse_smtp_port_env(nil), do: nil
|
||||
|
||||
defp parse_smtp_port_env(value) when is_binary(value) do
|
||||
case Integer.parse(String.trim(value)) do
|
||||
{port, _} when port > 0 -> port
|
||||
|
|
@ -676,16 +675,8 @@ defmodule Mv.Config do
|
|||
|
||||
defp parse_smtp_port_env(_), do: nil
|
||||
|
||||
# Reads a plain string SMTP setting: ENV first, then Settings.
|
||||
defp smtp_env_or_setting(env_key, setting_key) do
|
||||
case System.get_env(env_key) do
|
||||
nil -> get_from_settings(setting_key)
|
||||
value -> trim_nil(value)
|
||||
end
|
||||
end
|
||||
|
||||
defp maybe_add_missing(acc, _label, true), do: acc
|
||||
defp maybe_add_missing(acc, label, false), do: acc ++ [label]
|
||||
defp maybe_add_missing(acc, label, false), do: [label | acc]
|
||||
|
||||
# Reads an integer setting attribute from Settings.
|
||||
defp get_from_settings_integer(key) do
|
||||
|
|
|
|||
|
|
@ -1022,7 +1022,6 @@ defmodule MvWeb.CoreComponents do
|
|||
id={@row_id && @row_id.(row)}
|
||||
class={[
|
||||
table_row_tr_class(
|
||||
@row_click,
|
||||
table_row_selected?(assigns, row),
|
||||
@sticky_first_col
|
||||
)
|
||||
|
|
@ -1142,17 +1141,8 @@ defmodule MvWeb.CoreComponents do
|
|||
# Returns CSS classes for table row selection styles.
|
||||
# Hover/focus row highlighting is CSS-driven via [data-row-interactive] selectors in app.css.
|
||||
# Sticky-first-column zebra tables use CSS accents and omit selected row ring classes.
|
||||
defp table_row_tr_class(_row_click, selected?, sticky_first_col) do
|
||||
base = []
|
||||
|
||||
# Sticky-first-col tables: selection/hover accents are CSS-only (orange bar + fills).
|
||||
base =
|
||||
if selected? and not sticky_first_col,
|
||||
do: base ++ ["ring-2", "ring-inset", "ring-primary"],
|
||||
else: base
|
||||
|
||||
Enum.join(base, " ")
|
||||
end
|
||||
defp table_row_tr_class(true, false), do: "ring-2 ring-inset ring-primary"
|
||||
defp table_row_tr_class(_, _), do: ""
|
||||
|
||||
defp table_th_aria_sort(col, sort_field, sort_order) do
|
||||
col_sort = Map.get(col, :sort_field)
|
||||
|
|
|
|||
|
|
@ -85,16 +85,8 @@ defmodule MvWeb.GlobalSettingsLive do
|
|||
|> assign(:oidc_configured, Mv.Config.oidc_configured?())
|
||||
|> assign(:oidc_client_secret_set, Mv.Config.oidc_client_secret_set?())
|
||||
|> assign(:registration_enabled, settings.registration_enabled != false)
|
||||
|> assign(:smtp_env_configured, Mv.Config.smtp_env_configured?())
|
||||
|> assign(:smtp_env_mode, Mv.Config.smtp_env_mode?())
|
||||
|> assign(:smtp_missing_required_env_keys, Mv.Config.smtp_missing_required_env_keys())
|
||||
|> assign(:smtp_host_env_set, Mv.Config.smtp_host_env_set?())
|
||||
|> assign(:smtp_port_env_set, Mv.Config.smtp_port_env_set?())
|
||||
|> assign(:smtp_username_env_set, Mv.Config.smtp_username_env_set?())
|
||||
|> assign(:smtp_password_env_set, Mv.Config.smtp_password_env_set?())
|
||||
|> assign(:smtp_ssl_env_set, Mv.Config.smtp_ssl_env_set?())
|
||||
|> assign(:smtp_from_name_env_set, Mv.Config.mail_from_name_env_set?())
|
||||
|> assign(:smtp_from_email_env_set, Mv.Config.mail_from_email_env_set?())
|
||||
|> assign(:smtp_password_set, present?(Mv.Config.smtp_password()))
|
||||
|> assign(:smtp_configured, Mv.Config.smtp_configured?())
|
||||
|> assign(:smtp_test_result, nil)
|
||||
|
|
@ -361,19 +353,14 @@ defmodule MvWeb.GlobalSettingsLive do
|
|||
type="text"
|
||||
label={gettext("Host")}
|
||||
disabled={@smtp_env_mode}
|
||||
placeholder={
|
||||
if(@smtp_host_env_set,
|
||||
do: gettext("From SMTP_HOST"),
|
||||
else: "smtp.example.com"
|
||||
)
|
||||
}
|
||||
placeholder="smtp.example.com"
|
||||
/>
|
||||
<.input
|
||||
field={@form[:smtp_port]}
|
||||
type="number"
|
||||
label={gettext("Port")}
|
||||
disabled={@smtp_env_mode}
|
||||
placeholder={if(@smtp_port_env_set, do: gettext("From SMTP_PORT"), else: "587")}
|
||||
placeholder="587"
|
||||
/>
|
||||
<.input
|
||||
field={@form[:smtp_ssl]}
|
||||
|
|
@ -385,7 +372,6 @@ defmodule MvWeb.GlobalSettingsLive do
|
|||
{gettext("SSL (port 465)"), "ssl"},
|
||||
{gettext("None (port 25, insecure)"), "none"}
|
||||
]}
|
||||
placeholder={if(@smtp_ssl_env_set, do: gettext("From SMTP_SSL"), else: nil)}
|
||||
/>
|
||||
</div>
|
||||
|
||||
|
|
@ -395,12 +381,7 @@ defmodule MvWeb.GlobalSettingsLive do
|
|||
type="text"
|
||||
label={gettext("Username")}
|
||||
disabled={@smtp_env_mode}
|
||||
placeholder={
|
||||
if(@smtp_username_env_set,
|
||||
do: gettext("From SMTP_USERNAME"),
|
||||
else: "user@example.com"
|
||||
)
|
||||
}
|
||||
placeholder="user@example.com"
|
||||
/>
|
||||
<.input
|
||||
field={@form[:smtp_password]}
|
||||
|
|
@ -408,14 +389,11 @@ defmodule MvWeb.GlobalSettingsLive do
|
|||
label={gettext("Password")}
|
||||
disabled={@smtp_env_mode}
|
||||
placeholder={
|
||||
if(@smtp_password_env_set,
|
||||
do: gettext("From SMTP_PASSWORD"),
|
||||
else:
|
||||
if(@smtp_password_set,
|
||||
do: gettext("Leave blank to keep current"),
|
||||
else: nil
|
||||
)
|
||||
)
|
||||
if @smtp_env_mode do
|
||||
gettext("From SMTP_PASSWORD")
|
||||
else
|
||||
if @smtp_password_set, do: gettext("Leave blank to keep current"), else: nil
|
||||
end
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
|
|
@ -426,21 +404,14 @@ defmodule MvWeb.GlobalSettingsLive do
|
|||
type="email"
|
||||
label={gettext("Sender email (From)")}
|
||||
disabled={@smtp_env_mode}
|
||||
placeholder={
|
||||
if(@smtp_from_email_env_set,
|
||||
do: gettext("From MAIL_FROM_EMAIL"),
|
||||
else: "noreply@example.com"
|
||||
)
|
||||
}
|
||||
placeholder="noreply@example.com"
|
||||
/>
|
||||
<.input
|
||||
field={@form[:smtp_from_name]}
|
||||
type="text"
|
||||
label={gettext("Sender name (From)")}
|
||||
disabled={@smtp_env_mode}
|
||||
placeholder={
|
||||
if(@smtp_from_name_env_set, do: gettext("From MAIL_FROM_NAME"), else: "Mila")
|
||||
}
|
||||
placeholder="Mila"
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -450,12 +421,7 @@ defmodule MvWeb.GlobalSettingsLive do
|
|||
)}
|
||||
</p>
|
||||
<.button
|
||||
:if={
|
||||
not @smtp_env_mode and
|
||||
not (@smtp_host_env_set and @smtp_port_env_set and @smtp_username_env_set and
|
||||
@smtp_password_env_set and @smtp_ssl_env_set and @smtp_from_email_env_set and
|
||||
@smtp_from_name_env_set)
|
||||
}
|
||||
:if={not @smtp_env_mode}
|
||||
phx-disable-with={gettext("Saving...")}
|
||||
variant="primary"
|
||||
class="mt-2"
|
||||
|
|
@ -941,9 +907,9 @@ defmodule MvWeb.GlobalSettingsLive do
|
|||
|> assign(:oidc_only, Mv.Config.oidc_only?())
|
||||
|> assign(:oidc_configured, Mv.Config.oidc_configured?())
|
||||
|> assign(:smtp_configured, Mv.Config.smtp_configured?())
|
||||
|> assign(:smtp_env_mode, Mv.Config.smtp_env_mode?())
|
||||
|> assign(:smtp_missing_required_env_keys, Mv.Config.smtp_missing_required_env_keys())
|
||||
|> assign(:smtp_password_set, present?(Mv.Config.smtp_password()))
|
||||
|> assign(:smtp_from_name_env_set, Mv.Config.mail_from_name_env_set?())
|
||||
|> assign(:smtp_from_email_env_set, Mv.Config.mail_from_email_env_set?())
|
||||
|> assign(:vereinfacht_test_result, test_result)
|
||||
|> put_flash(:success, gettext("Settings updated successfully"))
|
||||
|> assign_form()
|
||||
|
|
@ -1283,25 +1249,17 @@ defmodule MvWeb.GlobalSettingsLive do
|
|||
end
|
||||
|
||||
defp merge_smtp_env_values(s) do
|
||||
s
|
||||
|> put_if_env_set(:smtp_host, Mv.Config.smtp_host_env_set?(), Mv.Config.smtp_host())
|
||||
|> put_if_env_set(:smtp_port, Mv.Config.smtp_port_env_set?(), Mv.Config.smtp_port())
|
||||
|> put_if_env_set(
|
||||
:smtp_username,
|
||||
Mv.Config.smtp_username_env_set?(),
|
||||
Mv.Config.smtp_username()
|
||||
)
|
||||
|> put_if_env_set(:smtp_ssl, Mv.Config.smtp_ssl_env_set?(), Mv.Config.smtp_ssl())
|
||||
|> put_if_env_set(
|
||||
:smtp_from_email,
|
||||
Mv.Config.mail_from_email_env_set?(),
|
||||
Mv.Config.mail_from_email()
|
||||
)
|
||||
|> put_if_env_set(
|
||||
:smtp_from_name,
|
||||
Mv.Config.mail_from_name_env_set?(),
|
||||
Mv.Config.mail_from_name()
|
||||
)
|
||||
if Mv.Config.smtp_env_mode?() do
|
||||
s
|
||||
|> Map.put(:smtp_host, Mv.Config.smtp_host())
|
||||
|> Map.put(:smtp_port, Mv.Config.smtp_port())
|
||||
|> Map.put(:smtp_username, Mv.Config.smtp_username())
|
||||
|> Map.put(:smtp_ssl, Mv.Config.smtp_ssl())
|
||||
|> Map.put(:smtp_from_email, Mv.Config.mail_from_email())
|
||||
|> Map.put(:smtp_from_name, Mv.Config.mail_from_name())
|
||||
else
|
||||
s
|
||||
end
|
||||
end
|
||||
|
||||
defp enrich_sync_errors([]), do: []
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue