diff --git a/lib/plausible_web/templates/email/spike_notification.html.eex b/lib/plausible_web/templates/email/spike_notification.html.eex index 47a9f27a8..2243ca1bd 100644 --- a/lib/plausible_web/templates/email/spike_notification.html.eex +++ b/lib/plausible_web/templates/email/spike_notification.html.eex @@ -10,7 +10,7 @@ There are currently <%= @current_visitors %> visitors on <%= link(@site.domain, <%= if @link do %>

-View dashboard: @link +View dashboard: <%= link(@link, to: @link) %> <% end %>

-- diff --git a/lib/workers/spike_notifier.ex b/lib/workers/spike_notifier.ex index ddd0331b1..4e0b0c410 100644 --- a/lib/workers/spike_notifier.ex +++ b/lib/workers/spike_notifier.ex @@ -31,7 +31,7 @@ defmodule Plausible.Workers.SpikeNotifier do :ok end - def notify(notification, current_visitors, sources) do + defp notify(notification, current_visitors, sources) do for recipient <- notification.recipients do send_notification(recipient, notification.site, current_visitors, sources) end @@ -45,7 +45,7 @@ defmodule Plausible.Workers.SpikeNotifier do site = Repo.preload(site, :members) dashboard_link = - if Enum.member?(site.members, recipient) do + if Enum.any?(site.members, &(&1.email == recipient)) do PlausibleWeb.Endpoint.url() <> "/" <> URI.encode_www_form(site.domain) end diff --git a/test/workers/spike_notifier_test.exs b/test/workers/spike_notifier_test.exs index 5dbea18e0..dea8f6fc9 100644 --- a/test/workers/spike_notifier_test.exs +++ b/test/workers/spike_notifier_test.exs @@ -85,4 +85,18 @@ defmodule Plausible.Workers.SpikeNotifierTest do assert_no_emails_delivered() end + + test "adds a dashboard link if recipient has access to the site" do + user = insert(:user, email: "robert@example.com") + site = insert(:site, domain: "example.com", members: [user]) + insert(:spike_notification, site: site, threshold: 10, recipients: ["robert@example.com"]) + + clickhouse_stub = + stub(Plausible.Stats.Clickhouse, :current_visitors, fn _site, _query -> 10 end) + |> stub(:top_sources, fn _site, _query, _limit, _page, _show_noref -> [] end) + + SpikeNotifier.perform(nil, clickhouse_stub) + + assert_email_delivered_with(html_body: ~r/View dashboard: