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
Avoid applying customer mapping to HelpScout conversations when in excluded domains (#6060)
Adrian Gruntkowskidb83948f4d58
diff --git a/extra/lib/plausible/help_scout.ex b/extra/lib/plausible/help_scout.exindex d23d2d0b2b..c1bf99fd43 100644--- a/extra/lib/plausible/help_scout.ex+++ b/extra/lib/plausible/help_scout.ex@@ -20,10 +20,14 @@ defmodule Plausible.HelpScout do@signature_errors [:missing_signature, :bad_signature]+ @excluded_email_domains ["paddle.com"]+@type signature_error() :: unquote(Enum.reduce(@signature_errors, &{:|, [], [&1, &2]}))def signature_errors(), do: @signature_errors+ def excluded_email_domains(), do: @excluded_email_domains+@doc """Validates signature against secret key configured for theHelpScout application.@@ -72,18 +76,19 @@ defmodule Plausible.HelpScout doendend- @spec get_details_for_customer(String.t()) :: {:ok, map()} | {:error, any()}- def get_details_for_customer(customer_id) do- with {:ok, emails} <- get_customer_emails(customer_id) do- get_details_for_emails(emails, customer_id)+ @spec get_details_for_customer(String.t(), String.t()) :: {:ok, map()} | {:error, any()}+ def get_details_for_customer(customer_id, conversation_id) do+ with {:ok, emails} <- get_customer_emails(customer_id, conversation_id) do+ get_details_for_emails(emails, customer_id, conversation_id, nil)endend- @spec get_details_for_emails([String.t()], String.t(), String.t() | nil) ::+ @spec get_details_for_emails([String.t()], String.t(), String.t(), String.t() | nil) ::{:ok, map()} | {:error, any()}- def get_details_for_emails(emails, customer_id, team_identifier \\ nil) do+ def get_details_for_emails(emails, customer_id, conversation_id, team_identifier) dowith {:ok, user} <- get_user(emails) do- set_mapping(customer_id, user.email)+ set_customer_mapping(customer_id, user.email)+ set_conversation_mapping(conversation_id, user.email)teams = Teams.Users.owned_teams(user)@@ -164,9 +169,9 @@ defmodule Plausible.HelpScout doendend- @spec search_users(String.t(), String.t()) :: [map()]- def search_users(term, customer_id) do- clear_mapping(customer_id)+ @spec search_users(String.t(), String.t(), String.t()) :: [map()]+ def search_users(term, customer_id, conversation_id) do+ clear_mappings(customer_id, conversation_id)search_term = "%#{term}%"@@ -302,13 +307,39 @@ defmodule Plausible.HelpScout do)end- defp get_customer_emails(customer_id) do- case lookup_mapping(customer_id) do- {:ok, email} ->- {:ok, [email]}+ defp get_customer_emails(customer_id, conversation_id) do+ get_emails_with_conversation_mapping(conversation_id, customer_id)+ end++ defp get_emails_with_conversation_mapping(conversation_id, customer_id) do+ case lookup_conversation_mapping(conversation_id) do+ {:ok, mapped_emails} ->+ {:ok, mapped_emails}++ {:error, :not_found} ->+ get_emails_with_customer_mapping(customer_id)+ end+ end++ defp get_emails_with_customer_mapping(customer_id) do+ # We want to explicitly reject customer emails from HS which+ # are in one of excluded domains. That's why we fetch+ # emails from HS first before checking the mapping.+ case fetch_customer_emails(customer_id) do+ {:ok, emails} ->+ case lookup_customer_mapping(customer_id) do+ {:ok, mapped_emails} ->+ {:ok, mapped_emails}++ {:error, :not_found} ->+ {:ok, emails}+ end++ {:error, error} when error in [:not_found, :no_emails] ->+ lookup_customer_mapping(customer_id)- {:error, :mapping_not_found} ->- fetch_customer_emails(customer_id)+ {:error, _} = error ->+ errorendend@@ -323,7 +354,13 @@ defmodule Plausible.HelpScout docase Req.get(url, opts) do{:ok, %{body: %{"_embedded" => %{"emails" => [_ | _] = emails}}}} ->- {:ok, Enum.map(emails, & &1["value"])}+ emails = Enum.map(emails, & &1["value"])++ if Enum.any?(emails, &email_excluded?/1) do+ {:error, :excluded_email}+ else+ {:ok, emails}+ end{:ok, %{status: 200}} ->{:error, :no_emails}@@ -350,25 +387,43 @@ defmodule Plausible.HelpScout do# Exposed for testing@doc false- def lookup_mapping(customer_id) do- email =+ def lookup_customer_mapping(customer_id) do+ email_row ="SELECT email FROM help_scout_mappings WHERE customer_id = $1"|> Repo.query!([customer_id])|> Map.get(:rows)|> List.first()- case email do+ case email_row do+ [email] ->+ {:ok, [email]}++ _ ->+ {:error, :not_found}+ end+ end++ # Exposed for testing+ @doc false+ def lookup_conversation_mapping(conversation_id) do+ email_row =+ "SELECT email FROM help_scout_mappings WHERE conversation_id = $1"+ |> Repo.query!([conversation_id])+ |> Map.get(:rows)+ |> List.first()++ case email_row do[email] ->- {:ok, email}+ {:ok, [email]}_ ->- {:error, :mapping_not_found}+ {:error, :not_found}endend# Exposed for testing@doc false- def set_mapping(customer_id, email) do+ def set_customer_mapping(customer_id, email) donow = NaiveDateTime.utc_now(:second)Repo.insert_all(@@ -379,8 +434,33 @@ defmodule Plausible.HelpScout do)end- defp clear_mapping(customer_id) do- Repo.query!("DELETE FROM help_scout_mappings WHERE customer_id = $1", [customer_id])+ # Exposed for testing+ @doc false+ def set_conversation_mapping(conversation_id, email) do+ now = NaiveDateTime.utc_now(:second)++ Repo.insert_all(+ "help_scout_mappings",+ [[conversation_id: conversation_id, email: email, inserted_at: now, updated_at: now]],+ conflict_target: :conversation_id,+ on_conflict: [set: [email: email, updated_at: now]]+ )+ end++ defp email_excluded?(email) when is_binary(email) do+ case String.split(email, "@") do+ [_, domain] -> String.trim(domain) in @excluded_email_domains+ _ -> false+ end+ end++ defp email_excluded?(_), do: false++ defp clear_mappings(customer_id, conversation_id) do+ Repo.query!(+ "DELETE FROM help_scout_mappings WHERE customer_id = $1 or conversation_id = $2",+ [customer_id, conversation_id]+ )enddefp get_token!() dodiff --git a/extra/lib/plausible_web/controllers/help_scout_controller.ex b/extra/lib/plausible_web/controllers/help_scout_controller.exindex 02e69bf57a..6397426786 100644--- a/extra/lib/plausible_web/controllers/help_scout_controller.ex+++ b/extra/lib/plausible_web/controllers/help_scout_controller.ex@@ -12,7 +12,7 @@ defmodule PlausibleWeb.HelpScoutController doassigns = %{conversation_id: conversation_id, customer_id: customer_id, token: token}with :ok <- HelpScout.validate_signature(conn),- {:ok, details} <- HelpScout.get_details_for_customer(customer_id) do+ {:ok, details} <- HelpScout.get_details_for_customer(customer_id, conversation_id) doconn|> render("callback.html", Map.merge(assigns, details))else@@ -49,7 +49,12 @@ defmodule PlausibleWeb.HelpScoutController dowith :ok <- match_conversation(token, conversation_id),{:ok, details} <-- HelpScout.get_details_for_emails([email], customer_id, params["team_identifier"]) do+ HelpScout.get_details_for_emails(+ [email],+ customer_id,+ conversation_id,+ params["team_identifier"]+ ) dorender(conn, "callback.html", Map.merge(assigns, details))else{:error, error} ->@@ -71,7 +76,7 @@ defmodule PlausibleWeb.HelpScoutController docase match_conversation(token, conversation_id) do:ok ->- users = HelpScout.search_users(term, customer_id)+ users = HelpScout.search_users(term, customer_id, conversation_id)render(conn, "search.html", Map.merge(assigns, %{users: users, term: term})){:error, error} ->diff --git a/test/plausible/help_scout_test.exs b/test/plausible/help_scout_test.exsindex a47b32dad0..018353c6d5 100644--- a/test/plausible/help_scout_test.exs+++ b/test/plausible/help_scout_test.exs@@ -68,7 +68,7 @@ defmodule Plausible.HelpScoutTest doplan_link: "#",plan_label: "None",sites_count: 0- }} = HelpScout.get_details_for_customer("500")+ }} = HelpScout.get_details_for_customer("500", "1000")endtest "returns for user without trial or subscription" do@@ -81,7 +81,7 @@ defmodule Plausible.HelpScoutTest dostatus_label: "None",plan_link: "#",plan_label: "None"- }} = HelpScout.get_details_for_customer("500")+ }} = HelpScout.get_details_for_customer("500", "1000")endtest "returns for user with trial expired" do@@ -96,7 +96,7 @@ defmodule Plausible.HelpScoutTest dostatus_label: "Expired trial",plan_link: "#",plan_label: "None"- }} = HelpScout.get_details_for_customer("500")+ }} = HelpScout.get_details_for_customer("500", "1000")endtest "returns for user with paid subscription on standard plan" do@@ -121,7 +121,7 @@ defmodule Plausible.HelpScoutTest dostatus_label: "Paid",plan_link: ^plan_link,plan_label: "10k Plan (€19 monthly)"- }} = HelpScout.get_details_for_customer("500")+ }} = HelpScout.get_details_for_customer("500", "1000")endtest "returns for user with paid subscription on standard yearly plan" do@@ -146,7 +146,7 @@ defmodule Plausible.HelpScoutTest dostatus_label: "Paid",plan_link: ^plan_link,plan_label: "10k Plan (€190 yearly)"- }} = HelpScout.get_details_for_customer("500")+ }} = HelpScout.get_details_for_customer("500", "1000")endtest "returns for user with paid subscription on free 10k plan" do@@ -162,7 +162,7 @@ defmodule Plausible.HelpScoutTest dostatus_label: "Paid",plan_link: _,plan_label: "Free 10k"- }} = HelpScout.get_details_for_customer("500")+ }} = HelpScout.get_details_for_customer("500", "1000")endtest "returns for user with paid subscription on enterprise plan" do@@ -180,7 +180,7 @@ defmodule Plausible.HelpScoutTest dostatus_label: "Paid",plan_link: _,plan_label: "1M Enterprise Plan (€123 monthly)"- }} = HelpScout.get_details_for_customer("500")+ }} = HelpScout.get_details_for_customer("500", "1000")endtest "returns for user with paid subscription on yearly enterprise plan" do@@ -199,7 +199,7 @@ defmodule Plausible.HelpScoutTest dostatus_label: "Paid",plan_link: _,plan_label: "1M Enterprise Plan (€123 yearly)"- }} = HelpScout.get_details_for_customer("500")+ }} = HelpScout.get_details_for_customer("500", "1000")endtest "returns for user with subscription pending cancellation" do@@ -217,7 +217,7 @@ defmodule Plausible.HelpScoutTest dostatus_label: "Pending cancellation",plan_link: _,plan_label: "10k Plan (€19 monthly)"- }} = HelpScout.get_details_for_customer("500")+ }} = HelpScout.get_details_for_customer("500", "1000")endtest "returns for user with canceled subscription" do@@ -236,7 +236,7 @@ defmodule Plausible.HelpScoutTest dostatus_label: "Canceled",plan_link: _,plan_label: "10k Plan (€19 monthly)"- }} = HelpScout.get_details_for_customer("500")+ }} = HelpScout.get_details_for_customer("500", "1000")endtest "returns for user with paused subscription" do@@ -254,7 +254,7 @@ defmodule Plausible.HelpScoutTest dostatus_label: "Paused",plan_link: _,plan_label: "10k Plan (€19 monthly)"- }} = HelpScout.get_details_for_customer("500")+ }} = HelpScout.get_details_for_customer("500", "1000")endtest "returns for user with locked site" do@@ -273,7 +273,42 @@ defmodule Plausible.HelpScoutTest doplan_link: _,plan_label: "10k Plan (€19 monthly)",sites_count: 1- }} = HelpScout.get_details_for_customer("500")+ }} = HelpScout.get_details_for_customer("500", "1000")+ end++ test "returns details for user via customer mapped email" do+ %{email: email} = new_user(trial_expiry_date: Date.utc_today())+ stub_help_scout_requests("[email protected]")+ HelpScout.set_customer_mapping("500", email)++ assert {:ok, %{status_label: "Trial"}} = HelpScout.get_details_for_customer("500", "1000")+ end++ test "returns details for user via conversation mapped email" do+ %{email: email} = new_user(trial_expiry_date: Date.utc_today())+ stub_help_scout_requests("[email protected]")+ HelpScout.set_conversation_mapping("1000", email)++ assert {:ok, %{status_label: "Trial"}} = HelpScout.get_details_for_customer("500", "1000")+ end++ for excluded_domain <- Plausible.HelpScout.excluded_email_domains() do+ test "refuses to use customer mapped email when customer email is in #{excluded_domain}" do+ %{email: email} = new_user(trial_expiry_date: Date.utc_today())+ stub_help_scout_requests("excluded.email@#{unquote(excluded_domain)}")+ HelpScout.set_customer_mapping("500", email)++ assert {:error, :excluded_email} = HelpScout.get_details_for_customer("500", "1000")+ end++ test "uses conversation mapped email when customer email is in #{excluded_domain} " do+ %{email: email} = new_user(trial_expiry_date: Date.utc_today())+ stub_help_scout_requests("excluded.email@#{unquote(excluded_domain)}")+ HelpScout.set_conversation_mapping("1000", email)++ assert {:ok, %{status_label: "Trial"}} =+ HelpScout.get_details_for_customer("500", "1000")+ endendtest "returns error when no matching user found in database" do@@ -282,7 +317,7 @@ defmodule Plausible.HelpScoutTest dostub_help_scout_requests("[email protected]")assert {:error, {:user_not_found, ["[email protected]"]}} =- HelpScout.get_details_for_customer("500")+ HelpScout.get_details_for_customer("500", "1000")endtest "returns error when no customer found in Help Scout" do@@ -300,7 +335,7 @@ defmodule Plausible.HelpScoutTest do|> Req.Test.text("Not found")end)- assert {:error, :not_found} = HelpScout.get_details_for_customer("500")+ assert {:error, :not_found} = HelpScout.get_details_for_customer("500", "1000")endtest "returns error when found customer has no emails" do@@ -321,7 +356,7 @@ defmodule Plausible.HelpScoutTest do})end)- assert {:error, :no_emails} = HelpScout.get_details_for_customer("500")+ assert {:error, :not_found} = HelpScout.get_details_for_customer("500", "1000")endtest "uses existing access token when available" do@@ -351,7 +386,7 @@ defmodule Plausible.HelpScoutTest do})end)- assert {:ok, _} = HelpScout.get_details_for_customer("500")+ assert {:ok, _} = HelpScout.get_details_for_customer("500", "1000")endtest "refreshes token on expiry" do@@ -397,12 +432,12 @@ defmodule Plausible.HelpScoutTest doendend)- assert {:ok, _} = HelpScout.get_details_for_customer("500")+ assert {:ok, _} = HelpScout.get_details_for_customer("500", "1000")endend- describe "get_details_for_emails/2" do- test "returns details for user and persists mapping" do+ describe "get_details_for_emails/4" do+ test "returns details for user and persists mappings" do%{email: email} = user = new_user(trial_expiry_date: Date.utc_today())team = team_of(user)@@ -416,19 +451,22 @@ defmodule Plausible.HelpScoutTest doplan_link: "#",plan_label: "None",sites_count: 0- }} = HelpScout.get_details_for_emails([email], "123")+ }} = HelpScout.get_details_for_emails([email], "123", "1000", nil)- assert {:ok, ^email} = HelpScout.lookup_mapping("123")+ assert {:ok, [^email]} = HelpScout.lookup_customer_mapping("123")+ assert {:ok, [^email]} = HelpScout.lookup_conversation_mapping("1000")end- test "updates mapping if one already exists" do+ test "updates mappings if ones already exist" douser = new_user()%{email: new_email} = new_user()- HelpScout.set_mapping("123", user.email)+ HelpScout.set_customer_mapping("123", user.email)+ HelpScout.set_conversation_mapping("1000", user.email)- assert {:ok, _} = HelpScout.get_details_for_emails([new_email], "123")- assert {:ok, ^new_email} = HelpScout.lookup_mapping("123")+ assert {:ok, _} = HelpScout.get_details_for_emails([new_email], "123", "1000", nil)+ assert {:ok, [^new_email]} = HelpScout.lookup_customer_mapping("123")+ assert {:ok, [^new_email]} = HelpScout.lookup_conversation_mapping("1000")endtest "picks the match with largest number of owned sites" do@@ -451,21 +489,27 @@ defmodule Plausible.HelpScoutTest doplan_link: "#",plan_label: "None",sites_count: 2- }} = HelpScout.get_details_for_emails([user1.email, user2.email], "123")+ }} =+ HelpScout.get_details_for_emails([user1.email, user2.email], "123", "1000", nil)user2_email = user2.email- assert {:ok, ^user2_email} = HelpScout.lookup_mapping("123")+ assert {:ok, [^user2_email]} = HelpScout.lookup_customer_mapping("123")endtest "does not persist the mapping when there's no match" doassert {:error, {:user_not_found, ["[email protected]"]}} =- HelpScout.get_details_for_emails(["[email protected]"], "123")-- assert {:error, :mapping_not_found} = HelpScout.lookup_mapping("123")+ HelpScout.get_details_for_emails(+ ["[email protected]"],+ "123",+ "1000",+ nil+ )++ assert {:error, :not_found} = HelpScout.lookup_customer_mapping("123")endend- describe "search_users/2" do+ describe "search_users/3" dotest "lists matching users by email or site domain ordered by site counts" douser1 = new_user(email: "[email protected]")@@ -479,12 +523,24 @@ defmodule Plausible.HelpScoutTest do# excludednew_site(domain: "consolidated.example.com", owner: user3, consolidated: true)- assert HelpScout.search_users("match.example.co", "123") == [+ assert HelpScout.search_users("match.example.co", "123", "1000") == [%{email: user3.email, sites_count: 2},%{email: user2.email, sites_count: 1},%{email: user1.email, sites_count: 0}]end++ test "clears existing customer and conversation mappings" do+ new_user(email: "[email protected]")++ HelpScout.set_customer_mapping("123", "[email protected]")+ HelpScout.set_conversation_mapping("1000", "[email protected]")++ assert [_] = HelpScout.search_users("match.example.co", "123", "1000")++ assert {:error, :not_found} = HelpScout.lookup_customer_mapping("123")+ assert {:error, :not_found} = HelpScout.lookup_conversation_mapping("1000")+ endenddefp stub_help_scout_requests(email) do