diff --git a/lib/philomena/bans/fingerprint.ex b/lib/philomena/bans/fingerprint.ex index ec9e5a44..cb5ba090 100644 --- a/lib/philomena/bans/fingerprint.ex +++ b/lib/philomena/bans/fingerprint.ex @@ -3,7 +3,8 @@ defmodule Philomena.Bans.Fingerprint do import Ecto.Changeset alias Philomena.Users.User - alias RelativeDate.Parser + import Philomena.Schema.Time + import Philomena.Schema.BanId schema "fingerprint_bans" do belongs_to :banning_user, User @@ -24,38 +25,14 @@ defmodule Philomena.Bans.Fingerprint do def changeset(fingerprint_ban, attrs) do fingerprint_ban |> cast(attrs, []) - |> populate_until() + |> propagate_time(:valid_until, :until) end def save_changeset(fingerprint_ban, attrs) do fingerprint_ban |> cast(attrs, [:reason, :note, :enabled, :fingerprint, :until]) - |> populate_valid_until() - |> put_ban_id() + |> assign_time(:until, :valid_until) + |> put_ban_id("F") |> validate_required([:reason, :enabled, :fingerprint, :valid_until]) end - - defp populate_until(%{data: data} = changeset) do - put_change(changeset, :until, to_string(data.valid_until)) - end - - defp populate_valid_until(changeset) do - changeset - |> get_field(:until) - |> Parser.parse() - |> case do - {:ok, time} -> - change(changeset, valid_until: time) - - {:error, _err} -> - add_error(changeset, :until, "is not a valid absolute or relative date and time") - end - end - - defp put_ban_id(%{data: %{generated_ban_id: nil}} = changeset) do - ban_id = Base.encode16(:crypto.strong_rand_bytes(3)) - - put_change(changeset, :generated_ban_id, "F#{ban_id}") - end - defp put_ban_id(changeset), do: changeset end diff --git a/lib/philomena/bans/subnet.ex b/lib/philomena/bans/subnet.ex index 5b683d34..2992a51f 100644 --- a/lib/philomena/bans/subnet.ex +++ b/lib/philomena/bans/subnet.ex @@ -3,7 +3,8 @@ defmodule Philomena.Bans.Subnet do import Ecto.Changeset alias Philomena.Users.User - alias RelativeDate.Parser + import Philomena.Schema.Time + import Philomena.Schema.BanId schema "subnet_bans" do belongs_to :banning_user, User @@ -24,38 +25,14 @@ defmodule Philomena.Bans.Subnet do def changeset(subnet_ban, attrs) do subnet_ban |> cast(attrs, []) - |> populate_until() + |> propagate_time(:valid_until, :until) end def save_changeset(subnet_ban, attrs) do subnet_ban |> cast(attrs, [:reason, :note, :enabled, :specification, :until]) - |> populate_valid_until() - |> put_ban_id() + |> assign_time(:until, :valid_until) + |> put_ban_id("S") |> validate_required([:reason, :enabled, :specification, :valid_until]) end - - defp populate_until(%{data: data} = changeset) do - put_change(changeset, :until, to_string(data.valid_until)) - end - - defp populate_valid_until(changeset) do - changeset - |> get_field(:until) - |> Parser.parse() - |> case do - {:ok, time} -> - change(changeset, valid_until: time) - - {:error, _err} -> - add_error(changeset, :until, "is not a valid absolute or relative date and time") - end - end - - defp put_ban_id(%{data: %{generated_ban_id: nil}} = changeset) do - ban_id = Base.encode16(:crypto.strong_rand_bytes(3)) - - put_change(changeset, :generated_ban_id, "S#{ban_id}") - end - defp put_ban_id(changeset), do: changeset end diff --git a/lib/philomena/bans/user.ex b/lib/philomena/bans/user.ex index 757d8f74..670f8699 100644 --- a/lib/philomena/bans/user.ex +++ b/lib/philomena/bans/user.ex @@ -4,7 +4,8 @@ defmodule Philomena.Bans.User do alias Philomena.Users.User alias Philomena.Repo - alias RelativeDate.Parser + import Philomena.Schema.Time + import Philomena.Schema.BanId schema "user_bans" do belongs_to :user, User @@ -27,36 +28,19 @@ defmodule Philomena.Bans.User do def changeset(user_ban, attrs) do user_ban |> cast(attrs, []) - |> populate_until() + |> propagate_time(:valid_until, :until) |> populate_username() end def save_changeset(user_ban, attrs) do user_ban |> cast(attrs, [:reason, :note, :enabled, :override_ip_ban, :username, :until]) - |> populate_valid_until() + |> assign_time(:until, :valid_until) |> populate_user_id() - |> put_ban_id() + |> put_ban_id("U") |> validate_required([:reason, :enabled, :user_id, :valid_until]) end - defp populate_until(%{data: data} = changeset) do - put_change(changeset, :until, to_string(data.valid_until)) - end - - defp populate_valid_until(changeset) do - changeset - |> get_field(:until) - |> Parser.parse() - |> case do - {:ok, time} -> - change(changeset, valid_until: time) - - {:error, _err} -> - add_error(changeset, :until, "is not a valid absolute or relative date and time") - end - end - defp populate_username(changeset) do case maybe_get_by(:id, get_field(changeset, :user_id)) do nil -> changeset @@ -71,13 +55,6 @@ defmodule Philomena.Bans.User do end end - defp put_ban_id(%{data: %{generated_ban_id: nil}} = changeset) do - ban_id = Base.encode16(:crypto.strong_rand_bytes(3)) - - put_change(changeset, :generated_ban_id, "U#{ban_id}") - end - defp put_ban_id(changeset), do: changeset - defp maybe_get_by(_field, nil), do: nil defp maybe_get_by(field, value), do: Repo.get_by(User, [{field, value}]) end diff --git a/lib/philomena/schema/ban_id.ex b/lib/philomena/schema/ban_id.ex new file mode 100644 index 00000000..a0f9201f --- /dev/null +++ b/lib/philomena/schema/ban_id.ex @@ -0,0 +1,10 @@ +defmodule Philomena.Schema.BanId do + import Ecto.Changeset + + def put_ban_id(%{data: %{generated_ban_id: nil}} = changeset, prefix) do + ban_id = Base.encode16(:crypto.strong_rand_bytes(3)) + + put_change(changeset, :generated_ban_id, "#{prefix}#{ban_id}") + end + def put_ban_id(changeset, _prefix), do: changeset +end diff --git a/lib/philomena/schema/time.ex b/lib/philomena/schema/time.ex new file mode 100644 index 00000000..86b29e39 --- /dev/null +++ b/lib/philomena/schema/time.ex @@ -0,0 +1,23 @@ +defmodule Philomena.Schema.Time do + alias RelativeDate.Parser + import Ecto.Changeset + + def assign_time(changeset, field, target_field) do + changeset + |> get_field(field) + |> Parser.parse() + |> case do + {:ok, time} -> + put_change(changeset, target_field, time) + + _err -> + add_error(changeset, field, "is not a valid relative or absolute date and time") + end + end + + def propagate_time(changeset, field, target_field) do + time = get_field(changeset, field) + + put_change(changeset, target_field, to_string(time)) + end +end diff --git a/lib/philomena_web/templates/activity/_channel_strip.html.slime b/lib/philomena_web/templates/activity/_channel_strip.html.slime index 39c06458..040ae3ee 100644 --- a/lib/philomena_web/templates/activity/_channel_strip.html.slime +++ b/lib/philomena_web/templates/activity/_channel_strip.html.slime @@ -16,4 +16,4 @@ = if @channel.viewers == 1, do: "viewer", else: "viewers" - else span.channel-strip__state.label.label--narrow.label--danger - ' OFF AIR \ No newline at end of file + ' OFF AIR diff --git a/lib/philomena_web/views/app_view.ex b/lib/philomena_web/views/app_view.ex index 58537281..81413f1f 100644 --- a/lib/philomena_web/views/app_view.ex +++ b/lib/philomena_web/views/app_view.ex @@ -126,25 +126,24 @@ defmodule PhilomenaWeb.AppView do |> to_string() end - defp text_or_na(nil), do: "N/A" - defp text_or_na(text), do: to_string(text) - + def link_to_ip(_conn, nil), do: content_tag(:code, "null") def link_to_ip(conn, ip) do link(to: Routes.ip_profile_path(conn, :show, to_string(ip))) do [ content_tag(:i, "", class: "fas fa-network-wired"), " ", - text_or_na(ip) + to_string(ip) ] end end + def link_to_fingerprint(_conn, nil), do: content_tag(:code, "null") def link_to_fingerprint(conn, fp) do link(to: Routes.fingerprint_profile_path(conn, :show, fp)) do [ content_tag(:i, "", class: "fas fa-desktop"), " ", - String.slice(text_or_na(fp), 0..6) + String.slice(fp, 0..6) ] end end