Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslandoga committed Feb 7, 2023
1 parent f1f9a8a commit 45fffd8
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ All notable changes to this project will be documented in this file.
- Fix breakdown API pagination when using event metrics plausible/analytics#2562
- Automatically update all visible dashboard reports in the realtime view
- Connect via TLS when using HTTPS scheme in ClickHouse URL plausible/analytics#2570
- Add more descriptive error message in case a transfer to an invited (but not joined) user is requested plausible/analytics#2651
- Add error message in case a transfer to an invited (but not joined) user is requested plausible/analytics#2651

### Changed
- Reject events with long URIs and data URIs plausible/analytics#2536
Expand Down
13 changes: 13 additions & 0 deletions lib/plausible/helpers/changeset.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
defmodule Plausible.ChangesetHelpers do
@moduledoc "Helper function for working with Ecto changesets"

def traverse_errors(changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} ->
Regex.replace(~r"%{(\w+)}", msg, fn _, key ->
opts
|> Keyword.get(String.to_existing_atom(key), key)
|> to_string()
end)
end)
end
end
14 changes: 2 additions & 12 deletions lib/plausible_web/controllers/api/external_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ defmodule PlausibleWeb.Api.ExternalController do
conn
|> put_resp_header("x-plausible-dropped", "#{Enum.count(dropped)}")
|> put_status(400)
|> json(%{errors: traverse_errors(first_invalid_changeset)})
|> json(%{errors: Plausible.ChangesetHelpers.traverse_errors(first_invalid_changeset)})
else
conn
|> put_resp_header("x-plausible-dropped", "#{Enum.count(dropped)}")
Expand All @@ -38,7 +38,7 @@ defmodule PlausibleWeb.Api.ExternalController do
{:error, %Ecto.Changeset{} = changeset} ->
conn
|> put_status(400)
|> json(%{errors: traverse_errors(changeset)})
|> json(%{errors: Plausible.ChangesetHelpers.traverse_errors(changeset)})
end
end

Expand Down Expand Up @@ -102,14 +102,4 @@ defmodule PlausibleWeb.Api.ExternalController do
end
end)
end

defp traverse_errors(changeset) do
Ecto.Changeset.traverse_errors(changeset, fn {msg, opts} ->
Regex.replace(~r"%{(\w+)}", msg, fn _, key ->
opts
|> Keyword.get(String.to_existing_atom(key), key)
|> to_string()
end)
end)
end
end
13 changes: 7 additions & 6 deletions lib/plausible_web/controllers/site/membership_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -136,16 +136,17 @@ defmodule PlausibleWeb.Site.MembershipController do
put_flash(conn, :success, "Site transfer request has been sent to #{email}")

{:error, changeset} ->
errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, _} -> msg end)
errors = Plausible.ChangesetHelpers.traverse_errors(changeset)

message =
if errors[:invitation] do
"#{email} has already been invited but hasn't yet accepted the join request to #{site_domain}"
else
"Site transfer request to #{email} has failed"
case errors do
%{invitation: [error | _]} -> String.capitalize(error)
_other -> "Site transfer request to #{email} has failed"
end

put_flash(conn, :error, message)
conn
|> put_flash(:error_title, "Transfer error")
|> put_flash(:error, message)
end

redirect(conn, to: Routes.site_path(conn, :settings_people, site.domain))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,7 @@ defmodule PlausibleWeb.Site.MembershipControllerTest do

conn = post(conn, "/sites/#{site.domain}/transfer-ownership", %{email: invited})
conn = get(recycle(conn), redirected_to(conn, 302))

assert html_response(conn, 200) =~
"#{invited} has already been invited but hasn't yet accepted the join request to #{site.domain}"
assert html_response(conn, 200) =~ "Already sent"
end
end

Expand Down

0 comments on commit 45fffd8

Please sign in to comment.