Skip to content

Commit

Permalink
Fixup
Browse files Browse the repository at this point in the history
  • Loading branch information
aerosol committed Jul 2, 2024
1 parent a520ae5 commit 44816c6
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 101 deletions.
20 changes: 15 additions & 5 deletions lib/plausible_web/controllers/site_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ defmodule PlausibleWeb.SiteController do
%{
site_id: site.id,
type: type,
threshold: (if type == "spike", do: 10, else: 1),
threshold: if(type == "spike", do: 10, else: 1),
recipients: [conn.assigns[:current_user].email]
}
)
Expand All @@ -507,17 +507,24 @@ defmodule PlausibleWeb.SiteController do
site = conn.assigns[:site]

Repo.delete_all(
from(mr in Plausible.Site.TrafficChangeNotification, where: mr.site_id == ^site.id and mr.type == ^type)
from(mr in Plausible.Site.TrafficChangeNotification,
where: mr.site_id == ^site.id and mr.type == ^type
)
)

conn
|> put_flash(:success, "Traffic #{type} notifications disabled")
|> redirect(external: Routes.site_path(conn, :settings_email_reports, site.domain))
end

def update_traffic_change_notification(conn, %{"traffic_change_notification" => params, "type" => type}) do
def update_traffic_change_notification(conn, %{
"traffic_change_notification" => params,
"type" => type
}) do
site = conn.assigns[:site]
notification = Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: type)

notification =
Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: type)

Plausible.Site.TrafficChangeNotification.changeset(notification, params)
|> Repo.update!()
Expand All @@ -539,7 +546,10 @@ defmodule PlausibleWeb.SiteController do
|> redirect(external: Routes.site_path(conn, :settings_email_reports, site.domain))
end

def remove_traffic_change_notification_recipient(conn, %{"recipient" => recipient, "type" => type}) do
def remove_traffic_change_notification_recipient(conn, %{
"recipient" => recipient,
"type" => type
}) do
site = conn.assigns[:site]

Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: type)
Expand Down
14 changes: 11 additions & 3 deletions lib/plausible_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -322,9 +322,17 @@ defmodule PlausibleWeb.Router do
SiteController,
:remove_monthly_report_recipient

post "/sites/:website/traffic-change-notification/:type/enable", SiteController, :enable_traffic_change_notification
post "/sites/:website/traffic-change-notification/:type/disable", SiteController, :disable_traffic_change_notification
put "/sites/:website/traffic-change-notification/:type", SiteController, :update_traffic_change_notification
post "/sites/:website/traffic-change-notification/:type/enable",
SiteController,
:enable_traffic_change_notification

post "/sites/:website/traffic-change-notification/:type/disable",
SiteController,
:disable_traffic_change_notification

put "/sites/:website/traffic-change-notification/:type",
SiteController,
:update_traffic_change_notification

post "/sites/:website/traffic-change-notification/:type/recipients",
SiteController,
Expand Down
39 changes: 19 additions & 20 deletions lib/plausible_web/templates/site/settings_email_reports.html.heex
Original file line number Diff line number Diff line change
Expand Up @@ -195,25 +195,24 @@
<% end %>
</div>

<%= render("traffic_change_form",
conn: @conn,
notification: @spike_notification,
site: @site,
heading: "Traffic Spike Notifications",
subtitle: "Get notified when your site has unusually high number of current visitors",
toggle_text: "Send notifications of traffic spikes",
threshold_label: "Current visitor threshold",
type: :spike
<%= render("traffic_change_form",
conn: @conn,
notification: @spike_notification,
site: @site,
heading: "Traffic Spike Notifications",
subtitle: "Get notified when your site has unusually high number of current visitors",
toggle_text: "Send notifications of traffic spikes",
threshold_label: "Current visitor threshold",
type: :spike
) %>


<%= render("traffic_change_form",
conn: @conn,
notification: @drop_notification,
site: @site,
heading: "Traffic Drop Notifications",
subtitle: "Get notified when your site has unusually low number of visitors within 12 hours",
toggle_text: "Send notifications of traffic drops",
threshold_label: "12 hours visitor threshold",
type: :drop
) %>
<%= render("traffic_change_form",
conn: @conn,
notification: @drop_notification,
site: @site,
heading: "Traffic Drop Notifications",
subtitle: "Get notified when your site has unusually low number of visitors within 12 hours",
toggle_text: "Send notifications of traffic drops",
threshold_label: "12 hours visitor threshold",
type: :drop
) %>
2 changes: 1 addition & 1 deletion lib/plausible_web/templates/site/traffic_change_form.heex
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
</svg>
</div>
<%= number_input(f, :threshold,
min: 0,
min: 0,
class:
"focus:ring-indigo-500 dark:bg-gray-900 focus:border-indigo-500 block w-full rounded-none rounded-l-md pl-10 sm:text-sm border-gray-300 dark:border-gray-500 dark:text-gray-100"
) %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@ defmodule Plausible.Repo.Migrations.TrafficDropNotifications do
end

create unique_index(:spike_notifications, [:site_id, :type])

end
end
180 changes: 109 additions & 71 deletions test/plausible_web/controllers/site_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1229,109 +1229,147 @@ defmodule PlausibleWeb.SiteControllerTest do
end

for type <- [:spike, :drop] do
describe "POST /sites/:website/traffic-change-notification/#{type}/enable" do
setup [:create_user, :log_in, :create_site]
describe "POST /sites/:website/traffic-change-notification/#{type}/enable" do
setup [:create_user, :log_in, :create_site]

test "creates a #{type} notification record with the user email", %{
conn: conn,
site: site,
user: user
} do
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/enable")
test "creates a #{type} notification record with the user email", %{
conn: conn,
site: site,
user: user
} do
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/enable")

notification = Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: unquote(type))
assert notification.recipients == [user.email]
end
notification =
Repo.get_by(Plausible.Site.TrafficChangeNotification,
site_id: site.id,
type: unquote(type)
)

test "does not allow duplicate #{type} notification to be created", %{
conn: conn,
site: site
} do
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/enable")
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/enable")
assert notification.recipients == [user.email]
end

assert Repo.aggregate(
from(s in Plausible.Site.TrafficChangeNotification, where: s.site_id == ^site.id and s.type == ^unquote(type)),
:count
) == 1
test "does not allow duplicate #{type} notification to be created", %{
conn: conn,
site: site
} do
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/enable")
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/enable")

assert Repo.aggregate(
from(s in Plausible.Site.TrafficChangeNotification,
where: s.site_id == ^site.id and s.type == ^unquote(type)
),
:count
) == 1
end
end
end

describe "POST /sites/:website/traffic-change-notification/#{type}/disable" do
setup [:create_user, :log_in, :create_site]
describe "POST /sites/:website/traffic-change-notification/#{type}/disable" do
setup [:create_user, :log_in, :create_site]

test "deletes the #{type} notification record", %{conn: conn, site: site} do
insert(:"#{unquote(type)}_notification", site: site)
test "deletes the #{type} notification record", %{conn: conn, site: site} do
insert(:"#{unquote(type)}_notification", site: site)

post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/disable")
post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/disable")

refute Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id)
refute Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id)
end
end
end

describe "PUT /sites/:website/traffic-change-notification/#{type}" do
setup [:create_user, :log_in, :create_site]
describe "PUT /sites/:website/traffic-change-notification/#{type}" do
setup [:create_user, :log_in, :create_site]

test "updates #{type} notification threshold", %{conn: conn, site: site} do
insert(:"#{unquote(type)}_notification", site: site, threshold: 10)
test "updates #{type} notification threshold", %{conn: conn, site: site} do
insert(:"#{unquote(type)}_notification", site: site, threshold: 10)

put(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}", %{
"traffic_change_notification" => %{"threshold" => "15"}
})
put(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}", %{
"traffic_change_notification" => %{"threshold" => "15"}
})

notification =
Repo.get_by(Plausible.Site.TrafficChangeNotification,
site_id: site.id,
type: unquote(type)
)

notification = Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: unquote(type))
assert notification.threshold == 15
assert notification.threshold == 15
end
end
end

describe "POST /sites/:website/traffic-change-notification/#{type}/recipients" do
setup [:create_user, :log_in, :create_site]
describe "POST /sites/:website/traffic-change-notification/#{type}/recipients" do
setup [:create_user, :log_in, :create_site]

test "adds a recipient to the #{type} notification", %{conn: conn, site: site} do
insert(:"#{unquote(type)}_notification", site: site)
test "adds a recipient to the #{type} notification", %{conn: conn, site: site} do
insert(:"#{unquote(type)}_notification", site: site)

post(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/recipients",
recipient: "[email protected]"
)
post(
conn,
"/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/recipients",
recipient: "[email protected]"
)

report = Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: unquote(type))
assert report.recipients == ["[email protected]"]
report =
Repo.get_by(Plausible.Site.TrafficChangeNotification,
site_id: site.id,
type: unquote(type)
)

assert report.recipients == ["[email protected]"]
end
end
end

describe "DELETE /sites/:website/traffic-change-notification/#{type}/recipients/:recipient" do
setup [:create_user, :log_in, :create_site]
describe "DELETE /sites/:website/traffic-change-notification/#{type}/recipients/:recipient" do
setup [:create_user, :log_in, :create_site]

test "removes a recipient from the #{type} notification", %{conn: conn, site: site} do
insert(:"#{unquote(type)}_notification", site: site, recipients: ["[email protected]"])
test "removes a recipient from the #{type} notification", %{conn: conn, site: site} do
insert(:"#{unquote(type)}_notification", site: site, recipients: ["[email protected]"])

delete(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/recipients/[email protected]")
delete(
conn,
"/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/recipients/[email protected]"
)

report = Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: unquote(type))
assert report.recipients == []
end
report =
Repo.get_by(Plausible.Site.TrafficChangeNotification,
site_id: site.id,
type: unquote(type)
)

test "fails to remove a recipient from the #{type} notification in a foreign website", %{
conn: conn
} do
site = insert(:site)
insert(:"#{unquote(type)}_notification", site: site, recipients: ["[email protected]"])
assert report.recipients == []
end

conn =
delete(conn, "/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/recipients/[email protected]")
test "fails to remove a recipient from the #{type} notification in a foreign website", %{
conn: conn
} do
site = insert(:site)
insert(:"#{unquote(type)}_notification", site: site, recipients: ["[email protected]"])

assert conn.status == 404
conn =
delete(
conn,
"/sites/#{site.domain}/traffic-change-notification/#{unquote(type)}/recipients/[email protected]"
)

conn =
delete(conn, "/sites/#{site.domain}/traffic-change-notification/recipients/#{unquote(type)}/recipient%40email.com")
assert conn.status == 404

assert conn.status == 404
conn =
delete(
conn,
"/sites/#{site.domain}/traffic-change-notification/recipients/#{unquote(type)}/recipient%40email.com"
)

report = Repo.get_by(Plausible.Site.TrafficChangeNotification, site_id: site.id, type: unquote(type))
assert [_] = report.recipients
assert conn.status == 404

report =
Repo.get_by(Plausible.Site.TrafficChangeNotification,
site_id: site.id,
type: unquote(type)
)

assert [_] = report.recipients
end
end
end
end

describe "GET /sites/:website/shared-links/new" do
setup [:create_user, :log_in, :create_site]
Expand Down

0 comments on commit 44816c6

Please sign in to comment.