diff --git a/config/runtime.exs b/config/runtime.exs
index 7339e64ec..02d06211e 100644
--- a/config/runtime.exs
+++ b/config/runtime.exs
@@ -506,7 +506,9 @@ cloud_cron = [
# Daily at 15
{"0 15 * * *", Plausible.Workers.NotifyAnnualRenewal},
# Every midnight
- {"0 0 * * *", Plausible.Workers.LockSites}
+ {"0 0 * * *", Plausible.Workers.LockSites},
+ # Daily at 5
+ {"0 5 * * *", Plausible.Workers.AcceptTrafficUntil}
]
crontab = if(is_selfhost, do: base_cron, else: base_cron ++ cloud_cron)
@@ -520,7 +522,8 @@ base_queues = [
site_setup_emails: 1,
clean_invitations: 1,
google_analytics_imports: 1,
- domain_change_transition: 1
+ domain_change_transition: 1,
+ check_accept_traffic_until: 1
]
cloud_queues = [
diff --git a/lib/plausible/auth/user.ex b/lib/plausible/auth/user.ex
index b0ecc5b1a..7dc117ff4 100644
--- a/lib/plausible/auth/user.ex
+++ b/lib/plausible/auth/user.ex
@@ -19,6 +19,9 @@ defmodule Plausible.Auth.User do
@required [:email, :name, :password]
+ @trial_accept_traffic_until_offset_days 14
+ @susbscription_accept_traffic_until_offset_days 30
+
schema "users" do
field :email, :string
field :password_hash
@@ -30,6 +33,7 @@ defmodule Plausible.Auth.User do
field :theme, Ecto.Enum, values: [:system, :light, :dark]
field :email_verified, :boolean
field :previous_email, :string
+ field :accept_traffic_until, :date
# A field only used as a manual override - allow subscribing
# to any plan, even when exceeding its pageview limit
@@ -107,12 +111,28 @@ defmodule Plausible.Auth.User do
:email_verified,
:theme,
:trial_expiry_date,
- :allow_next_upgrade_override
+ :allow_next_upgrade_override,
+ :accept_traffic_until
])
|> validate_required([:email, :name, :email_verified])
+ |> maybe_bump_accept_traffic_until()
|> unique_constraint(:email)
end
+ defp maybe_bump_accept_traffic_until(changeset) do
+ expiry_change = get_change(changeset, :trial_expiry_date)
+
+ if expiry_change do
+ put_change(
+ changeset,
+ :accept_traffic_until,
+ Date.add(expiry_change, @trial_accept_traffic_until_offset_days)
+ )
+ else
+ changeset
+ end
+ end
+
def set_password(user, password) do
user
|> cast(%{password: password}, [:password])
@@ -135,7 +155,12 @@ defmodule Plausible.Auth.User do
end
def start_trial(user) do
- change(user, trial_expiry_date: trial_expiry())
+ trial_expiry = trial_expiry()
+
+ change(user,
+ trial_expiry_date: trial_expiry,
+ accept_traffic_until: Date.add(trial_expiry, @trial_accept_traffic_until_offset_days)
+ )
end
def end_trial(user) do
@@ -183,6 +208,11 @@ defmodule Plausible.Auth.User do
Path.join(PlausibleWeb.Endpoint.url(), ["avatar/", hash])
end
+ def trial_accept_traffic_until_offset_days(), do: @trial_accept_traffic_until_offset_days
+
+ def subscription_accept_traffic_until_offset_days(),
+ do: @susbscription_accept_traffic_until_offset_days
+
defp validate_email_changed(changeset) do
if !get_change(changeset, :email) && !changeset.errors[:email] do
add_error(changeset, :email, "can't be the same", validation: :different_email)
diff --git a/lib/plausible/auth/user_admin.ex b/lib/plausible/auth/user_admin.ex
index 2cd6c3c94..690e34c1a 100644
--- a/lib/plausible/auth/user_admin.ex
+++ b/lib/plausible/auth/user_admin.ex
@@ -14,8 +14,13 @@ defmodule Plausible.Auth.UserAdmin do
name: nil,
email: nil,
previous_email: nil,
- trial_expiry_date: nil,
- allow_next_upgrade_override: nil
+ trial_expiry_date: %{
+ help_text: "Change will also update Accept Traffic Until date"
+ },
+ allow_next_upgrade_override: nil,
+ accept_traffic_until: %{
+ help_text: "Change will take up to 15 minutes to propagate"
+ }
]
end
@@ -32,7 +37,11 @@ defmodule Plausible.Auth.UserAdmin do
subscription_plan: %{value: &subscription_plan/1},
subscription_status: %{value: &subscription_status/1},
usage: %{value: &usage_link/1},
- grace_period: %{value: &grace_period_status/1}
+ grace_period: %{value: &grace_period_status/1},
+ accept_traffic_until: %{
+ name: "Accept traffic until",
+ value: &format_date(&1.accept_traffic_until)
+ }
]
end
diff --git a/lib/plausible/billing/billing.ex b/lib/plausible/billing/billing.ex
index fcc5d2785..5a958f51c 100644
--- a/lib/plausible/billing/billing.ex
+++ b/lib/plausible/billing/billing.ex
@@ -275,6 +275,7 @@ defmodule Plausible.Billing do
|> Map.put(:subscription, subscription)
user
+ |> Plausible.Users.update_accept_traffic_until()
|> maybe_remove_grace_period()
|> Plausible.Users.maybe_reset_next_upgrade_override()
|> tap(&Plausible.Billing.SiteLocker.update_sites_for/1)
diff --git a/lib/plausible/ingestion/counters/telemetry_handler.ex b/lib/plausible/ingestion/counters/telemetry_handler.ex
index a4b264ee9..1abfa83ca 100644
--- a/lib/plausible/ingestion/counters/telemetry_handler.ex
+++ b/lib/plausible/ingestion/counters/telemetry_handler.ex
@@ -1,10 +1,10 @@
defmodule Plausible.Ingestion.Counters.TelemetryHandler do
@moduledoc """
- Susbcribes to telemetry events emitted by `Plausible.Ingestion.Event`.
+ Subscribes to telemetry events emitted by `Plausible.Ingestion.Event`.
Every time a request derived event is either dispatched to clickhouse or dropped,
a telemetry event is emitted respectively. That event is captured here,
its metadata is extracted and sent for internal stats aggregation via
- `Counters.Buffer` interface.
+ `Counters.Buffer` interface.
"""
alias Plausible.Ingestion.Counters
alias Plausible.Ingestion.Event
diff --git a/lib/plausible/site.ex b/lib/plausible/site.ex
index 8a1a5e360..c571619ab 100644
--- a/lib/plausible/site.ex
+++ b/lib/plausible/site.ex
@@ -21,7 +21,6 @@ defmodule Plausible.Site do
field :conversions_enabled, :boolean, default: true
field :props_enabled, :boolean, default: true
field :funnels_enabled, :boolean, default: true
- field :accept_traffic_until, :naive_datetime
field :ingest_rate_limit_scale_seconds, :integer, default: 60
# default is set via changeset/2
diff --git a/lib/plausible/site/admin.ex b/lib/plausible/site/admin.ex
index c4b75113f..7bf3e0c03 100644
--- a/lib/plausible/site/admin.ex
+++ b/lib/plausible/site/admin.ex
@@ -14,7 +14,11 @@ defmodule Plausible.SiteAdmin do
end
def custom_index_query(_conn, _schema, query) do
- from(r in query, preload: [memberships: :user])
+ from(r in query,
+ inner_join: o in assoc(r, :owner),
+ inner_join: m in assoc(r, :memberships),
+ preload: [owner: o, memberships: {m, :user}]
+ )
end
def form_fields(_) do
@@ -39,15 +43,26 @@ defmodule Plausible.SiteAdmin do
inserted_at: %{name: "Created at", value: &format_date(&1.inserted_at)},
timezone: nil,
public: nil,
- owner: %{value: &get_owner_email/1},
+ owner: %{value: &get_owner/1},
other_members: %{value: &get_other_members/1},
limits: %{
value: fn site ->
- case site.ingest_rate_limit_threshold do
- nil -> ""
- 0 -> "🛑 BLOCKED"
- n -> "⏱ #{n}/#{site.ingest_rate_limit_scale_seconds}s (per server)"
- end
+ rate_limiting_status =
+ case site.ingest_rate_limit_threshold do
+ nil -> ""
+ 0 -> "🛑 BLOCKED"
+ n -> "⏱ #{n}/#{site.ingest_rate_limit_scale_seconds}s (per server)"
+ end
+
+ owner = site.owner
+
+ owner_limits =
+ if owner.accept_traffic_until &&
+ Date.after?(Date.utc_today(), owner.accept_traffic_until) do
+ "💸 Rejecting traffic"
+ end
+
+ {:safe, Enum.join([rate_limiting_status, owner_limits], "
")}
end
}
]
@@ -132,11 +147,16 @@ defmodule Plausible.SiteAdmin do
Timex.format!(date, "{Mshort} {D}, {YYYY}")
end
- defp get_owner_email(site) do
- owner = Enum.find(site.memberships, fn m -> m.role == :owner end)
+ defp get_owner(site) do
+ owner = site.owner
if owner do
- owner.user.email
+ {:safe,
+ """
+ #{owner.name}
+
+ #{owner.email}
+ """}
end
end
diff --git a/lib/plausible/site/cache.ex b/lib/plausible/site/cache.ex
index 55df2bf4e..21c7417cf 100644
--- a/lib/plausible/site/cache.ex
+++ b/lib/plausible/site/cache.ex
@@ -56,7 +56,6 @@ defmodule Plausible.Site.Cache do
domain_changed_from
ingest_rate_limit_scale_seconds
ingest_rate_limit_threshold
- accept_traffic_until
)a
@type t() :: Site.t()
@@ -117,12 +116,13 @@ defmodule Plausible.Site.Cache do
defp sites_by_domain_query do
from s in Site,
left_join: rg in assoc(s, :revenue_goals),
+ inner_join: owner in assoc(s, :owner),
select: {
s.domain,
s.domain_changed_from,
%{struct(s, ^@cached_schema_fields) | from_cache?: true}
},
- preload: [revenue_goals: rg]
+ preload: [revenue_goals: rg, owner: owner]
end
@spec merge(new_items :: [Site.t()], opts :: Keyword.t()) :: :ok
diff --git a/lib/plausible/site/gate_keeper.ex b/lib/plausible/site/gate_keeper.ex
index ca3df06d8..e552f1637 100644
--- a/lib/plausible/site/gate_keeper.ex
+++ b/lib/plausible/site/gate_keeper.ex
@@ -44,9 +44,9 @@ defmodule Plausible.Site.GateKeeper do
defp policy(domain, opts) when is_binary(domain) do
with from_cache <- Cache.get(domain, Keyword.get(opts, :cache_opts, [])),
- site = %Site{accept_traffic_until: accept_traffic_until} <- from_cache do
+ site = %Site{owner: %{accept_traffic_until: accept_traffic_until}} <- from_cache do
if not is_nil(accept_traffic_until) and
- NaiveDateTime.after?(NaiveDateTime.utc_now(), accept_traffic_until) do
+ Date.after?(Date.utc_today(), accept_traffic_until) do
:payment_required
else
check_rate_limit(site, opts)
diff --git a/lib/plausible/users.ex b/lib/plausible/users.ex
index 3d07cfff0..3cd22453b 100644
--- a/lib/plausible/users.ex
+++ b/lib/plausible/users.ex
@@ -2,6 +2,8 @@ defmodule Plausible.Users do
@moduledoc """
User context
"""
+ use Plausible
+ @accept_traffic_until_free ~D[2135-01-01]
import Ecto.Query
@@ -9,6 +11,42 @@ defmodule Plausible.Users do
alias Plausible.Billing.Subscription
alias Plausible.Repo
+ @spec update_accept_traffic_until(Auth.User.t()) :: Auth.User.t()
+ def update_accept_traffic_until(user) do
+ user
+ |> Auth.User.changeset(%{accept_traffic_until: accept_traffic_until(user)})
+ |> Repo.update!()
+ end
+
+ @spec accept_traffic_until(Auth.User.t()) :: Date.t()
+ on_full_build do
+ def accept_traffic_until(user) do
+ user = with_subscription(user)
+
+ cond do
+ Plausible.Billing.on_trial?(user) ->
+ Timex.shift(user.trial_expiry_date,
+ days: Auth.User.trial_accept_traffic_until_offset_days()
+ )
+
+ user.subscription && user.subscription.paddle_plan_id == "free_10k" ->
+ @accept_traffic_until_free
+
+ user.subscription && user.subscription.next_bill_date ->
+ Timex.shift(user.subscription.next_bill_date,
+ days: Auth.User.subscription_accept_traffic_until_offset_days()
+ )
+
+ true ->
+ raise "This user is neither on trial or has a valid subscription. Manual intervention required."
+ end
+ end
+ else
+ def accept_traffic_until(_user) do
+ @accept_traffic_until_free
+ end
+ end
+
def with_subscription(%Auth.User{id: user_id} = user) do
Repo.preload(user, subscription: last_subscription_query(user_id))
end
diff --git a/lib/plausible_web/controllers/billing_controller.ex b/lib/plausible_web/controllers/billing_controller.ex
index b8abf6db0..6069e2958 100644
--- a/lib/plausible_web/controllers/billing_controller.ex
+++ b/lib/plausible_web/controllers/billing_controller.ex
@@ -150,8 +150,4 @@ defmodule PlausibleWeb.BillingController do
{:error, :no_subscription}
end
end
-
- def preview_susbcription(_, _) do
- {:error, :no_user_id}
- end
end
diff --git a/lib/plausible_web/email.ex b/lib/plausible_web/email.ex
index 7385839f8..fd1c7300a 100644
--- a/lib/plausible_web/email.ex
+++ b/lib/plausible_web/email.ex
@@ -113,7 +113,10 @@ defmodule PlausibleWeb.Email do
|> to(user)
|> tag("trial-over-email")
|> subject("Your Plausible trial has ended")
- |> render("trial_over_email.html", user: user)
+ |> render("trial_over_email.html",
+ user: user,
+ extra_offset: Plausible.Auth.User.trial_accept_traffic_until_offset_days()
+ )
end
def stats_report(email, assigns) do
@@ -190,7 +193,12 @@ defmodule PlausibleWeb.Email do
end
def yearly_expiration_notification(user) do
- date = Timex.format!(user.subscription.next_bill_date, "{Mfull} {D}, {YYYY}")
+ next_bill_date = Timex.format!(user.subscription.next_bill_date, "{Mfull} {D}, {YYYY}")
+
+ accept_traffic_until =
+ user
+ |> Plausible.Users.accept_traffic_until()
+ |> Timex.format!("{Mfull} {D}, {YYYY}")
priority_email()
|> to(user)
@@ -198,7 +206,8 @@ defmodule PlausibleWeb.Email do
|> subject("Your Plausible subscription is about to expire")
|> render("yearly_expiration_notification.html", %{
user: user,
- date: date
+ next_bill_date: next_bill_date,
+ accept_traffic_until: accept_traffic_until
})
end
@@ -344,6 +353,22 @@ defmodule PlausibleWeb.Email do
})
end
+ def approaching_accept_traffic_until(user) do
+ base_email()
+ |> to(user.email)
+ |> tag("drop-traffic-warning-first")
+ |> subject("We'll stop counting your stats")
+ |> render("approaching_accept_traffic_until.html", time: "next week")
+ end
+
+ def approaching_accept_traffic_until_tomorrow(user) do
+ base_email()
+ |> to(user.email)
+ |> tag("drop-traffic-warning-final")
+ |> subject("A reminder that we'll stop counting your stats tomorrow")
+ |> render("approaching_accept_traffic_until.html", time: "tomorrow")
+ end
+
@doc """
Unlike the default 'base' emails, priority emails cannot be unsubscribed from. This is achieved
by sending them through a dedicated 'priority' message stream in Postmark.
diff --git a/lib/plausible_web/templates/email/approaching_accept_traffic_until.html.heex b/lib/plausible_web/templates/email/approaching_accept_traffic_until.html.heex
new file mode 100644
index 000000000..f5e12414a
--- /dev/null
+++ b/lib/plausible_web/templates/email/approaching_accept_traffic_until.html.heex
@@ -0,0 +1,8 @@
+You used to have an active account with Plausible Analytics, a simple, lightweight, open source and privacy-first Google Analytics alternative.
+
+We've noticed that you're still sending us stats so we're writing to inform you that we'll stop accepting stats from your sites <%= @time %>. We're an independent, bootstrapped service and we don't sell your data, so this will reduce our server costs and help keep us sustainable.
+
+If you'd like to continue counting your site stats in a privacy-friendly way, please
+login to your Plausible account and start a subscription.
+
+Do you have any questions or need help with anything? Just reply to this email and we'll gladly help.
diff --git a/lib/plausible_web/templates/email/trial_over_email.html.eex b/lib/plausible_web/templates/email/trial_over_email.html.eex
index 55a422fce..d5d9a174b 100644
--- a/lib/plausible_web/templates/email/trial_over_email.html.eex
+++ b/lib/plausible_web/templates/email/trial_over_email.html.eex
@@ -3,4 +3,4 @@ Your free Plausible trial has now expired. Upgrade your account to continue rece
<%= link("Upgrade now", to: PlausibleWeb.Router.Helpers.billing_url(PlausibleWeb.Endpoint, :choose_plan)) %>
-We will keep recording stats for another month to give you time to upgrade.
+We will keep recording stats for <%= @extra_offset %> days to give you time to upgrade.
diff --git a/lib/plausible_web/templates/email/yearly_expiration_notification.html.eex b/lib/plausible_web/templates/email/yearly_expiration_notification.html.eex
index 1f3a96b7f..fe2bbe344 100644
--- a/lib/plausible_web/templates/email/yearly_expiration_notification.html.eex
+++ b/lib/plausible_web/templates/email/yearly_expiration_notification.html.eex
@@ -1,7 +1,9 @@
-Time flies! This is a reminder that your annual subscription for Plausible Analytics will expire on <%= @date %>.
+Time flies! This is a reminder that your annual subscription for Plausible Analytics will expire on <%= @next_bill_date %>.
You need to <%= link("renew your subscription", to: PlausibleWeb.Router.Helpers.billing_url(PlausibleWeb.Endpoint, :choose_plan)) %> if you want to continue using Plausible to count your website stats in a privacy-friendly way.
-If you don't want to continue your subscription, there's no action required. You will lose access to the stats on <%= @date %>.
+If you don't want to continue your subscription, there's no action required. You will lose access to your dashboard on <%= @next_bill_date %>.
Have a question, feedback or need some guidance? Just reply to this email to get in touch!
+
+Note that we'll stop accepting stats on <%= @accept_traffic_until %>. We're an independent, bootstrapped service and we don't sell your data, so this will reduce our server costs and help keep us sustainable. If you'd like to continue counting your site stats in a privacy-friendly way, please login to your Plausible account and renew your subscription.
diff --git a/lib/plausible_web/templates/layout/_notice.html.heex b/lib/plausible_web/templates/layout/_notice.html.heex
index 66afb1b41..190acacdf 100644
--- a/lib/plausible_web/templates/layout/_notice.html.heex
+++ b/lib/plausible_web/templates/layout/_notice.html.heex
@@ -43,7 +43,7 @@
your account. If you do not upgrade your account <%= grace_period_end(
@conn.assigns[:current_user]
) %>, we will
- lock your stats and they won't be accessible. <%= link("Upgrade now →",
+ lock your dashboard and it won't be accessible. <%= link("Upgrade now →",
to: "/settings",
class: "text-sm font-medium text-yellow-800"
) %>
diff --git a/lib/workers/accept_traffic_until_notification.ex b/lib/workers/accept_traffic_until_notification.ex
new file mode 100644
index 000000000..6ac7cbba3
--- /dev/null
+++ b/lib/workers/accept_traffic_until_notification.ex
@@ -0,0 +1,95 @@
+defmodule Plausible.Workers.AcceptTrafficUntil do
+ @moduledoc """
+ A worker meant to be run once a day that sends out e-mail notifications to site
+ owners assuming:
+ - their sites still receive traffic (i.e. have stats for yesterday)
+ - `site.accept_traffic_until` is approaching either tomorrow or exactly in 7 days
+
+ Users having no sites or sites that receive no traffic, won't be notified.
+ We make a tiny effort here to make sure we send the same notification at most once a day.
+ """
+ use Oban.Worker, queue: :check_accept_traffic_until
+ import Ecto.Query
+
+ alias Plausible.Auth.User
+ alias Plausible.Site
+ alias Plausible.Repo
+ alias Plausible.ClickhouseRepo
+
+ @impl Oban.Worker
+ def perform(_job, today \\ Date.utc_today()) do
+ tomorrow = today |> Date.add(+1)
+ next_week = today |> Date.add(+7)
+
+ # send at most one notification per user, per day
+ sent_today_query =
+ from s in "sent_accept_traffic_until_notifications",
+ where: s.user_id == parent_as(:user).id and s.sent_on == ^today,
+ select: true
+
+ notifications =
+ Repo.all(
+ from u in User,
+ as: :user,
+ join: sm in Site.Membership,
+ on: sm.user_id == u.id,
+ where: sm.role == :owner,
+ where: u.accept_traffic_until == ^tomorrow or u.accept_traffic_until == ^next_week,
+ where: not exists(sent_today_query),
+ select: %{
+ id: u.id,
+ email: u.email,
+ deadline: u.accept_traffic_until,
+ site_ids: fragment("array_agg(?.site_id)", sm)
+ },
+ group_by: u.id
+ )
+
+ for notification <- notifications do
+ case {has_stats?(notification.site_ids, today), notification.deadline} do
+ {true, ^tomorrow} ->
+ notification
+ |> store_sent(today)
+ |> PlausibleWeb.Email.approaching_accept_traffic_until_tomorrow()
+ |> Plausible.Mailer.send()
+
+ {true, ^next_week} ->
+ notification
+ |> store_sent(today)
+ |> PlausibleWeb.Email.approaching_accept_traffic_until()
+ |> Plausible.Mailer.send()
+
+ _ ->
+ nil
+ end
+ end
+
+ {:ok, Enum.count(notifications)}
+ end
+
+ defp has_stats?(site_ids, today) do
+ yesterday = Date.add(today, -1)
+
+ ClickhouseRepo.exists?(
+ from e in "events_v2",
+ where: fragment("toDate(?) >= ?", e.timestamp, ^yesterday),
+ where: e.site_id in ^site_ids
+ )
+ end
+
+ defp store_sent(notification, today) do
+ Repo.insert_all(
+ "sent_accept_traffic_until_notifications",
+ [
+ %{
+ user_id: notification.id,
+ sent_on: today
+ }
+ ],
+ on_conflict: :nothing,
+ conflict_target: [:user_id, :sent_on]
+ )
+
+ notification
+ end
+end
diff --git a/test/plausible/auth/users_test.exs b/test/plausible/auth/users_test.exs
new file mode 100644
index 000000000..ce8c86001
--- /dev/null
+++ b/test/plausible/auth/users_test.exs
@@ -0,0 +1,56 @@
+defmodule Plausible.UsersTest do
+ use Plausible.DataCase, async: true
+
+ alias Plausible.Users
+ alias Plausible.Auth.User
+ alias Plausible.Repo
+
+ describe "update_accept_traffic_until" do
+ @describetag :full_build_only
+ test "update" do
+ user = insert(:user) |> User.start_trial() |> Repo.update!()
+ # 30 for trial + 14
+ assert Date.diff(user.accept_traffic_until, Date.utc_today()) ==
+ 30 + User.trial_accept_traffic_until_offset_days()
+
+ future = Date.add(Date.utc_today(), 30)
+ insert(:subscription, user: user, next_bill_date: future)
+
+ assert updated_user = Users.update_accept_traffic_until(user)
+
+ assert Date.diff(updated_user.accept_traffic_until, future) ==
+ User.subscription_accept_traffic_until_offset_days()
+ end
+
+ test "retrieve: trial + 14 days" do
+ user = insert(:user)
+
+ assert Users.accept_traffic_until(user) ==
+ Date.utc_today() |> Date.add(30 + User.trial_accept_traffic_until_offset_days())
+ end
+
+ test "retrieve: last_bill_date + 30 days" do
+ future = Date.add(Date.utc_today(), 30)
+ user = insert(:user, subscription: build(:subscription, next_bill_date: future))
+
+ assert Users.accept_traffic_until(user) ==
+ future |> Date.add(User.subscription_accept_traffic_until_offset_days())
+ end
+
+ test "retrieve: free plan" do
+ user = insert(:user, subscription: build(:subscription, paddle_plan_id: "free_10k"))
+
+ assert Users.accept_traffic_until(user) == ~D[2135-01-01]
+ end
+
+ test "retrieve: invalid user (there's one in the prod DB as of today, due to some testing)" do
+ user = insert(:user, trial_expiry_date: nil)
+
+ assert_raise RuntimeError,
+ ~r/Manual intervention required/,
+ fn ->
+ assert Users.accept_traffic_until(user) == ~D[2135-01-01]
+ end
+ end
+ end
+end
diff --git a/test/plausible/billing/billing_test.exs b/test/plausible/billing/billing_test.exs
index da7330e40..d4295572c 100644
--- a/test/plausible/billing/billing_test.exs
+++ b/test/plausible/billing/billing_test.exs
@@ -189,6 +189,19 @@ defmodule Plausible.BillingTest do
refute Repo.reload!(site).locked
end
+ @tag :full_build_only
+ test "updates accept_traffic_until" do
+ user = insert(:user)
+
+ %{@subscription_created_params | "passthrough" => user.id}
+ |> Billing.subscription_created()
+
+ next_bill = Date.from_iso8601!(@subscription_created_params["next_bill_date"])
+
+ assert Repo.reload!(user).accept_traffic_until ==
+ Date.add(next_bill, 30)
+ end
+
test "sets user.allow_next_upgrade_override field to false" do
user = insert(:user, allow_next_upgrade_override: true)
@@ -250,6 +263,24 @@ defmodule Plausible.BillingTest do
refute Repo.reload!(site).locked
end
+ @tag :full_build_only
+ test "updates accept_traffic_until" do
+ user = insert(:user)
+ subscription = insert(:subscription, user: user)
+
+ @subscription_updated_params
+ |> Map.merge(%{
+ "subscription_id" => subscription.paddle_subscription_id,
+ "passthrough" => user.id
+ })
+ |> Billing.subscription_updated()
+
+ next_bill = Date.from_iso8601!(@subscription_updated_params["next_bill_date"])
+
+ assert Repo.reload!(user).accept_traffic_until ==
+ Date.add(next_bill, 30)
+ end
+
test "sets user.allow_next_upgrade_override field to false" do
user = insert(:user, allow_next_upgrade_override: true)
subscription = insert(:subscription, user: user)
diff --git a/test/plausible/ingestion/event_test.exs b/test/plausible/ingestion/event_test.exs
index 3db601961..8388a4f6b 100644
--- a/test/plausible/ingestion/event_test.exs
+++ b/test/plausible/ingestion/event_test.exs
@@ -118,8 +118,13 @@ defmodule Plausible.Ingestion.EventTest do
end
test "event pipeline drops events for site with accept_trafic_until in the past" do
- yesterday = NaiveDateTime.add(NaiveDateTime.utc_now(), -1, :day)
- site = insert(:site, ingest_rate_limit_threshold: 1, accept_traffic_until: yesterday)
+ yesterday = Date.add(Date.utc_today(), -1)
+
+ site =
+ insert(:site,
+ ingest_rate_limit_threshold: 1,
+ members: [build(:user, accept_traffic_until: yesterday)]
+ )
payload = %{
name: "pageview",
diff --git a/test/plausible/site/cache_test.exs b/test/plausible/site/cache_test.exs
index d081a003f..e0b50c8f1 100644
--- a/test/plausible/site/cache_test.exs
+++ b/test/plausible/site/cache_test.exs
@@ -36,7 +36,23 @@ defmodule Plausible.Site.CacheTest do
%{id: first_id} = site1 = insert(:site, domain: "site1.example.com")
_ =
- insert(:site, domain: "site2.example.com", accept_traffic_until: ~N[2021-01-01 00:00:00])
+ insert(:site,
+ domain: "site2.example.com",
+ memberships: [
+ build(:site_membership,
+ user: build(:user, accept_traffic_until: ~D[2022-01-01]),
+ role: :viewer
+ ),
+ build(:site_membership,
+ user: build(:user, accept_traffic_until: ~D[2021-01-01]),
+ role: :owner
+ ),
+ build(:site_membership,
+ user: build(:user, accept_traffic_until: ~D[2020-01-01]),
+ role: :admin
+ )
+ ]
+ )
:ok = Cache.refresh_all(cache_name: test)
@@ -50,7 +66,7 @@ defmodule Plausible.Site.CacheTest do
assert %Site{from_cache?: true} =
Cache.get("site2.example.com", force?: true, cache_name: test)
- assert %Site{from_cache?: false, accept_traffic_until: ~N[2021-01-01 00:00:00]} =
+ assert %Site{from_cache?: false, owner: %{accept_traffic_until: ~D[2021-01-01]}} =
Cache.get("site2.example.com", cache_name: test)
refute Cache.get("site3.example.com", cache_name: test, force?: true)
diff --git a/test/plausible/site/gate_keeper_test.exs b/test/plausible/site/gate_keeper_test.exs
index 88d314bd1..13b590968 100644
--- a/test/plausible/site/gate_keeper_test.exs
+++ b/test/plausible/site/gate_keeper_test.exs
@@ -16,9 +16,13 @@ defmodule Plausible.Site.GateKeeperTest do
test "sites with accepted_traffic_until < now are denied", %{test: test, opts: opts} do
domain = "expired.example.com"
- yesterday = NaiveDateTime.utc_now() |> NaiveDateTime.add(-1, :day)
+ yesterday = Date.utc_today() |> Date.add(-1)
- %{id: _} = add_site_and_refresh_cache(test, domain: domain, accept_traffic_until: yesterday)
+ %{id: _} =
+ add_site_and_refresh_cache(test,
+ domain: domain,
+ members: [build(:user, accept_traffic_until: yesterday)]
+ )
assert {:deny, :payment_required} = GateKeeper.check(domain, opts)
end
diff --git a/test/plausible/site/sites_test.exs b/test/plausible/site/sites_test.exs
index 7406d2e85..bc70c1696 100644
--- a/test/plausible/site/sites_test.exs
+++ b/test/plausible/site/sites_test.exs
@@ -3,6 +3,17 @@ defmodule Plausible.SitesTest do
alias Plausible.Sites
+ describe "create a site" do
+ test "creates a site" do
+ user = insert(:user)
+
+ params = %{"domain" => "example.com", "timezone" => "Europe/London"}
+
+ assert {:ok, %{site: %{domain: "example.com", timezone: "Europe/London"}}} =
+ Sites.create(user, params)
+ end
+ end
+
describe "is_member?" do
test "is true if user is a member of the site" do
user = insert(:user)
diff --git a/test/plausible_web/email_test.exs b/test/plausible_web/email_test.exs
index 8cee8b0bc..fc52753c4 100644
--- a/test/plausible_web/email_test.exs
+++ b/test/plausible_web/email_test.exs
@@ -245,6 +245,34 @@ defmodule PlausibleWeb.EmailTest do
end
end
+ describe "approaching accept_traffic_until" do
+ test "renders first warning" do
+ user = build(:user)
+
+ %{html_body: body, subject: subject} =
+ PlausibleWeb.Email.approaching_accept_traffic_until(user)
+
+ assert subject == "We'll stop counting your stats"
+ assert body =~ plausible_link()
+
+ assert body =~
+ "We've noticed that you're still sending us stats so we're writing to inform you that we'll stop accepting stats from your sites next week."
+ end
+
+ test "renders final warning" do
+ user = build(:user)
+
+ %{html_body: body, subject: subject} =
+ PlausibleWeb.Email.approaching_accept_traffic_until_tomorrow(user)
+
+ assert subject == "A reminder that we'll stop counting your stats tomorrow"
+ assert body =~ plausible_link()
+
+ assert body =~
+ "We've noticed that you're still sending us stats so we're writing to inform you that we'll stop accepting stats from your sites tomorrow."
+ end
+ end
+
def plausible_link() do
plausible_url = PlausibleWeb.EmailView.plausible_url()
"#{plausible_url}"
diff --git a/test/support/test_utils.ex b/test/support/test_utils.ex
index 843abefcb..01ae5a2cf 100644
--- a/test/support/test_utils.ex
+++ b/test/support/test_utils.ex
@@ -213,6 +213,10 @@ defmodule Plausible.TestUtils do
NaiveDateTime.truncate(naive, :second)
end
+ def to_naive_truncate(%Date{} = date) do
+ NaiveDateTime.new!(date, ~T[00:00:00])
+ end
+
def eventually(expectation, wait_time_ms \\ 50, retries \\ 10) do
Enum.reduce_while(1..retries, nil, fn attempt, _acc ->
case expectation.() do
diff --git a/test/workers/accept_traffic_until_test.exs b/test/workers/accept_traffic_until_test.exs
new file mode 100644
index 000000000..b66b05908
--- /dev/null
+++ b/test/workers/accept_traffic_until_test.exs
@@ -0,0 +1,126 @@
+defmodule Plausible.Workers.AcceptTrafficUntilTest do
+ use Plausible.DataCase, async: true
+ use Bamboo.Test
+
+ alias Plausible.Workers.AcceptTrafficUntil
+
+ @moduletag :full_build_only
+
+ test "does not send any notifications when sites have no stats" do
+ today = Date.utc_today()
+ next_week = today |> Date.add(+7)
+ tomorrow = today |> Date.add(+1)
+
+ user1 = insert(:user, accept_traffic_until: next_week)
+ user2 = insert(:user, accept_traffic_until: tomorrow)
+
+ _site1 = insert(:site, members: [user1])
+
+ _site2 = insert(:site, members: [user2])
+
+ {:ok, 2} = AcceptTrafficUntil.perform(nil)
+
+ refute_notifications(user1.email)
+ refute_notifications(user2.email)
+ end
+
+ test "tomorrow: sends one e-mail" do
+ tomorrow = Date.utc_today() |> Date.add(+1)
+ user = insert(:user, accept_traffic_until: tomorrow)
+
+ :site |> insert(members: [user]) |> populate_stats([build(:pageview)])
+
+ {:ok, 1} = AcceptTrafficUntil.perform(nil)
+ assert_final_notification(user.email)
+ end
+
+ test "next week: sends one e-mail" do
+ next_week = Date.utc_today() |> Date.add(+7)
+ user = insert(:user, accept_traffic_until: next_week)
+
+ :site |> insert(members: [user]) |> populate_stats([build(:pageview)])
+
+ {:ok, 1} = AcceptTrafficUntil.perform(nil)
+ assert_weekly_notification(user.email)
+ end
+
+ test "sends combined warnings in one shot" do
+ today = Date.utc_today()
+ next_week = today |> Date.add(+7)
+ tomorrow = today |> Date.add(+1)
+ in_8_days = today |> Date.add(+8)
+
+ user1 = insert(:user, accept_traffic_until: next_week)
+ user2 = insert(:user, accept_traffic_until: tomorrow)
+ user3 = insert(:user, accept_traffic_until: in_8_days, email: "nope@example.com")
+ user4 = insert(:user, accept_traffic_until: today, email: "nope2@example.com")
+
+ :site |> insert(members: [user1]) |> populate_stats([build(:pageview)])
+ :site |> insert(members: [user2]) |> populate_stats([build(:pageview)])
+ :site |> insert(members: [user3]) |> populate_stats([build(:pageview)])
+ :site |> insert(members: [user4]) |> populate_stats([build(:pageview)])
+
+ {:ok, 2} = AcceptTrafficUntil.perform(nil)
+ assert_weekly_notification(user1.email)
+ assert_final_notification(user2.email)
+ refute_notifications("nope@example.com")
+ refute_notifications("nope2@example.com")
+ end
+
+ test "sends multiple notifications per recipient as the time passes" do
+ today = Date.utc_today()
+ email = "cycle@example.com"
+ user = insert(:user, email: email) |> Plausible.Auth.User.start_trial() |> Repo.update!()
+ site = insert(:site, members: [user])
+
+ populate_stats(site, [
+ build(:pageview, timestamp: today),
+ build(:pageview, timestamp: Date.add(user.trial_expiry_date, 7)),
+ build(:pageview, timestamp: Date.add(user.trial_expiry_date, 8)),
+ build(:pageview, timestamp: Date.add(user.trial_expiry_date, 13))
+ ])
+
+ # today's worker is no-op
+ {:ok, 0} = AcceptTrafficUntil.perform(nil, today)
+ refute_notifications(email)
+
+ # trial_expiry + 7 days worker => we'll stop counting next week
+ {:ok, 1} = AcceptTrafficUntil.perform(nil, Date.add(user.trial_expiry_date, 7))
+ assert_weekly_notification(email)
+
+ # another call on the same day == no-op, e-mail already sent
+ {:ok, 0} = AcceptTrafficUntil.perform(nil, Date.add(user.trial_expiry_date, 7))
+ refute_notifications(email)
+
+ # another call on another day == no-op, no notification type applies
+ {:ok, 0} = AcceptTrafficUntil.perform(nil, Date.add(user.trial_expiry_date, 8))
+ refute_notifications(email)
+
+ # day before accept_stats_until (trial_expiry_date + 14 - 1)
+ {:ok, 1} = AcceptTrafficUntil.perform(nil, Date.add(user.trial_expiry_date, 13))
+ assert_final_notification(email)
+
+ # another one on the day before accept_stats_until (trial_expiry_date + 14 - 1) == no-op
+ {:ok, 0} = AcceptTrafficUntil.perform(nil, Date.add(user.trial_expiry_date, 13))
+ refute_notifications(email)
+ end
+
+ defp assert_weekly_notification(email) when is_binary(email) do
+ assert_email_delivered_with(
+ to: [nil: email],
+ subject: PlausibleWeb.Email.approaching_accept_traffic_until(%{email: email}).subject
+ )
+ end
+
+ defp assert_final_notification(email) when is_binary(email) do
+ assert_email_delivered_with(
+ to: [nil: email],
+ subject:
+ PlausibleWeb.Email.approaching_accept_traffic_until_tomorrow(%{email: email}).subject
+ )
+ end
+
+ defp refute_notifications(email) when is_binary(email) do
+ refute_email_delivered_with(to: [nil: email])
+ end
+end