Open source, privacy-first web analytics. Lightweight, cookie-free Google Analytics alternative. Self-hosted or cloud.
TypeScript analyticsanalytics-dashboardclickhouseelixirgoogle-analyticsmarketingopen-source-analyticsphoenixplausible-analyticspostgresqlprivacyprivacy-friendlyself-hostedsimple-analyticsstatisticstailwindcssweb-analyticswebsite-analyticswebsite-statswebsite-tracking
Listing email suppressions + handle reactivations (#6709)
Adam01d0f9590bbc
diff --git a/lib/plausible/email_suppressions.ex b/lib/plausible/email_suppressions.exindex 8f6483c94c..3dc2cb6281 100644--- a/lib/plausible/email_suppressions.ex+++ b/lib/plausible/email_suppressions.ex@@ -82,20 +82,67 @@ defmodule Plausible.EmailSuppressions do|> upsert()end+ @page_size 50++ @doc """+ List suppressions, most recent first - for manual review (CRM).+ """+ @spec list(keyword(), map()) :: Paginator.Page.t()+ def list(filters \\ [], pagination_params \\ %{}) do+ EmailSuppression+ |> filter_reason(Keyword.get(filters, :reason))+ |> filter_search(Keyword.get(filters, :search))+ |> order_by([s], desc: s.inserted_at, desc: s.id)+ |> preload(:reactivated_by)+ |> Plausible.Pagination.paginate(+ pagination_params,+ cursor_fields: [inserted_at: :desc, id: :desc],+ limit: @page_size+ )+ end++ defp filter_reason(query, reason) when reason in [nil, ""], do: query+ defp filter_reason(query, reason), do: where(query, [s], s.reason == ^reason)++ defp filter_search(query, search) when search in [nil, ""], do: query++ defp filter_search(query, search) do+ where(query, [s], ilike(s.email, ^"%#{search}%"))+ end+@doc """Lifts a suppression after manual review, recording who did it.+ Encapsulates suppression remote deletion at Postmark - can't be done for spam complaints."""@spec reactivate(String.t(), Plausible.Auth.User.t()) ::- {:ok, EmailSuppression.t()} | {:error, :not_found | Ecto.Changeset.t()}+ {:ok, EmailSuppression.t()}+ | {:error,+ :not_found+ | :cannot_delete_spam_complaint+ | {:postmark_error, term()}+ | Ecto.Changeset.t()}def reactivate(email, %Plausible.Auth.User{id: user_id}) docase Repo.get_by(EmailSuppression, email: email) donil ->{:error, :not_found}suppression ->- suppression- |> EmailSuppression.reactivate_changeset(user_id)- |> Repo.update()+ with :ok <- delete_in_postmark(suppression) do+ suppression+ |> EmailSuppression.reactivate_changeset(user_id)+ |> Repo.update()+ end+ end+ end++ defp delete_in_postmark(%{reason: :spam_complaint}) do+ {:error, :cannot_delete_spam_complaint}+ end++ defp delete_in_postmark(%{email: email}) do+ case Plausible.Postmark.delete_suppression(email) do+ :ok -> :ok+ {:error, reason} -> {:error, {:postmark_error, reason}}endenddiff --git a/lib/plausible/postmark.ex b/lib/plausible/postmark.exindex dd3cd7a179..e17e923ffe 100644--- a/lib/plausible/postmark.ex+++ b/lib/plausible/postmark.ex@@ -30,6 +30,34 @@ defmodule Plausible.Postmark do"SpamComplaint" => :spam_complaint}+ @doc """+ https://postmarkapp.com/developer/api/suppressions-api#delete-a-suppression++ Lifts a suppression on every stream we send from.+ Postmark refuses this only for a spam complaint.+ """+ @spec delete_suppression(String.t()) :: :ok | {:error, term()}+ def delete_suppression(email) do+ @streams+ |> Enum.map(&delete_suppression(&1, email))+ |> Enum.find(:ok, &match?({:error, _}, &1))+ end++ defp delete_suppression(stream, email) do+ case post("/message-streams/#{stream}/suppressions/delete", %{+ "Suppressions" => [%{"EmailAddress" => email}]+ }) do+ {:ok, %{"Suppressions" => [%{"Status" => "Deleted"}]}} ->+ :ok++ {:ok, %{"Suppressions" => [%{"Status" => "Failed", "Message" => message}]}} ->+ {:error, {stream, message}}++ {:error, reason} ->+ {:error, reason}+ end+ end+@doc """One-off backfill of e-mail suppressions from Postmark's Suppressions API,across every message stream we send from.@@ -131,16 +159,22 @@ defmodule Plausible.Postmark doenddefp get(path, params) do+ request(&Req.get/2, path, params: params)+ end++ defp post(path, body) do+ request(&Req.post/2, path, json: body)+ end++ defp request(req_fun, path, opts) doextra_opts = Application.get_env(:plausible, __MODULE__)[:req_opts] || []opts =- [- params: params,- headers: [{"accept", "application/json"}, {"x-postmark-server-token", api_key()}]- ]+ [headers: [{"accept", "application/json"}, {"x-postmark-server-token", api_key()}]]+ |> Keyword.merge(opts)|> Keyword.merge(extra_opts)- case Req.get(@base_api_url <> path, opts) do+ case req_fun.(@base_api_url <> path, opts) do{:ok, %{status: 200, body: body}} ->{:ok, body}diff --git a/test/plausible/email_suppressions_test.exs b/test/plausible/email_suppressions_test.exsindex c68f1661f0..3fc3d07b82 100644--- a/test/plausible/email_suppressions_test.exs+++ b/test/plausible/email_suppressions_test.exs@@ -2,9 +2,24 @@ defmodule Plausible.EmailSuppressionsTest douse Plausible.DataCasealias Plausible.EmailSuppressions+ alias Plausible.Postmark@moduletag :ee_only+ # Suppressions without Bounce ID (backfilled)+ # go through delete suppressions API call on reactivate.+ # Tests that expect different result may override this.+ setup do+ Req.Test.stub(Postmark, fn conn ->+ Req.Test.json(conn, %{+ "Message" => "OK",+ "Suppressions" => [%{"Status" => "Deleted"}]+ })+ end)++ :ok+ end+describe "suppressed?/1" dotest "false when no record exists" dorefute EmailSuppressions.suppressed?("[email protected]")@@ -111,8 +126,9 @@ defmodule Plausible.EmailSuppressionsTest douser = insert(:user){:ok, _} =- EmailSuppressions.create_from_spam_complaint(%{+ EmailSuppressions.create_from_bounce(%{email: "[email protected]",+ reason: :blocked,source: :webhook})@@ -171,6 +187,56 @@ defmodule Plausible.EmailSuppressionsTest doendend+ describe "list/1" do+ setup do+ {:ok, hard} =+ EmailSuppressions.create_from_bounce(%{+ email: "[email protected]",+ reason: :hard_bounce,+ source: :webhook+ })++ {:ok, _blocked} =+ EmailSuppressions.create_from_bounce(%{+ email: "[email protected]",+ reason: :blocked,+ source: :backfill+ })++ %{hard: hard}+ end++ test "returns everything, most recently suppressed first" do+ assert %{entries: entries} = EmailSuppressions.list()++ assert Enum.map(entries, & &1.email) == ["[email protected]", "[email protected]"]+ end++ test "filters by reason" do+ assert %{entries: [suppression]} = EmailSuppressions.list(reason: :blocked)++ assert suppression.email == "[email protected]"+ end++ test "filters by an email substring, case-insensitively" do+ assert %{entries: [suppression]} = EmailSuppressions.list(search: "HARD@")++ assert suppression.email == "[email protected]"+ end++ test "paginates via before/after cursors" do+ assert %{entries: [first], metadata: %{after: cursor, before: nil}} =+ EmailSuppressions.list([], %{"limit" => "1"})++ assert first.email == "[email protected]"++ assert %{entries: [second], metadata: %{after: nil}} =+ EmailSuppressions.list([], %{"limit" => "1", "after" => cursor})++ assert second.email == "[email protected]"+ end+ end+describe "reactivate/2" dotest "returns :not_found when there is no suppression for the address" douser = insert(:user)@@ -191,5 +257,69 @@ defmodule Plausible.EmailSuppressionsTest doassert suppression.reactivated_by_user_id == user.idassert suppression.reactivated_atend++ test "deletes the suppression in Postmark" do+ user = insert(:user)++ {:ok, _} =+ EmailSuppressions.create_from_bounce(%{+ email: "[email protected]",+ reason: :hard_bounce,+ source: :backfill+ })++ Req.Test.stub(Postmark, fn conn ->+ assert conn.method == "POST"++ assert conn.request_path in [+ "/message-streams/outbound/suppressions/delete",+ "/message-streams/priority/suppressions/delete"+ ]++ Req.Test.json(conn, %{+ "Suppressions" => [%{"EmailAddress" => "[email protected]", "Status" => "Deleted"}]+ })+ end)++ assert {:ok, _suppression} = EmailSuppressions.reactivate("[email protected]", user)+ refute EmailSuppressions.suppressed?("[email protected]")+ end++ test "refuses to reactivate a spam complaint, leaving it suppressed" do+ user = insert(:user)++ {:ok, _} =+ EmailSuppressions.create_from_spam_complaint(%{+ email: "[email protected]",+ source: :backfill+ })++ Req.Test.stub(Postmark, fn _conn -> flunk("Postmark should not have been called") end)++ assert {:error, :cannot_delete_spam_complaint} =+ EmailSuppressions.reactivate("[email protected]", user)++ assert EmailSuppressions.suppressed?("[email protected]")+ end++ test "leaves it suppressed when the Postmark deletion call fails" do+ user = insert(:user)++ {:ok, _} =+ EmailSuppressions.create_from_bounce(%{+ email: "[email protected]",+ reason: :hard_bounce,+ source: :backfill+ })++ Req.Test.stub(Postmark, fn conn ->+ conn |> Plug.Conn.put_status(500) |> Req.Test.json(%{"Message" => "boom"})+ end)++ assert {:error, {:postmark_error, _reason}} =+ EmailSuppressions.reactivate("[email protected]", user)++ assert EmailSuppressions.suppressed?("[email protected]")+ endendenddiff --git a/test/plausible/mailer_test.exs b/test/plausible/mailer_test.exsindex b276004601..25dfd4fc29 100644--- a/test/plausible/mailer_test.exs+++ b/test/plausible/mailer_test.exs@@ -78,6 +78,10 @@ defmodule Plausible.MailerTest dosource: :webhook})+ Req.Test.stub(Plausible.Postmark, fn conn ->+ Req.Test.json(conn, %{"Suppressions" => [%{"Status" => "Deleted"}]})+ end)+{:ok, _} = Plausible.EmailSuppressions.reactivate(user.email, reviewer)email = PlausibleWeb.Email.welcome_email(user)diff --git a/test/plausible/postmark_test.exs b/test/plausible/postmark_test.exsindex 862544ddd7..cefbfcb516 100644--- a/test/plausible/postmark_test.exs+++ b/test/plausible/postmark_test.exs@@ -21,6 +21,77 @@ defmodule Plausible.PostmarkTest doendend+ describe "delete_suppression/1" do+ test "deletes the suppression from every stream we send from" do+ Req.Test.stub(Postmark, fn conn ->+ assert conn.method == "POST"++ assert conn.request_path in [+ "/message-streams/outbound/suppressions/delete",+ "/message-streams/priority/suppressions/delete"+ ]++ conn = Plug.Conn.fetch_query_params(conn)+ {:ok, body, _conn} = Plug.Conn.read_body(conn)+ assert Jason.decode!(body) == %{"Suppressions" => [%{"EmailAddress" => "[email protected]"}]}++ Req.Test.json(conn, %{+ "Suppressions" => [%{"EmailAddress" => "[email protected]", "Status" => "Deleted"}]+ })+ end)++ assert :ok = Postmark.delete_suppression("[email protected]")+ end++ test "no-ops (still :ok) when the address wasn't suppressed on a stream" do+ Req.Test.stub(Postmark, fn conn ->+ Req.Test.json(conn, %{+ "Suppressions" => [+ %{"EmailAddress" => "[email protected]", "Status" => "Deleted", "Message" => nil}+ ]+ })+ end)++ assert :ok = Postmark.delete_suppression("[email protected]")+ end++ test "returns an error when Postmark refuses (e.g. a spam complaint can't be deleted)" do+ Req.Test.stub(Postmark, fn conn ->+ Req.Test.json(conn, %{+ "Suppressions" => [+ %{+ "EmailAddress" => "[email protected]",+ "Status" => "Failed",+ "Message" => "SpamComplaint suppressions cannot be deleted"+ }+ ]+ })+ end)++ assert {:error, {stream, "SpamComplaint suppressions cannot be deleted"}} =+ Postmark.delete_suppression("[email protected]")++ assert stream in ["outbound", "priority"]+ end++ test "returns an error on a non-200 response from either stream" do+ Req.Test.stub(Postmark, fn conn ->+ case conn.request_path do+ "/message-streams/outbound/suppressions/delete" ->+ Req.Test.json(conn, %{+ "Suppressions" => [%{"EmailAddress" => "[email protected]", "Status" => "Deleted"}]+ })++ "/message-streams/priority/suppressions/delete" ->+ conn |> Plug.Conn.put_status(500) |> Req.Test.json(%{"Message" => "boom"})+ end+ end)++ assert {:error, {:unexpected_status, 500, _body}} =+ Postmark.delete_suppression("[email protected]")+ end+ end+describe "backfill_suppressions/0" dotest "upserts a suppression for every entry found, across both streams" doReq.Test.stub(Postmark, fn conn ->