mirror of
https://github.com/plausible/analytics.git
synced 2024-11-27 17:23:00 +03:00
ae42b86792
* Checks - added Dialyzer - fixed Dialyzer errors - added Dialyzer check to GitHub Actions with cache - added Credo - fixed Credo Warnings - added Credo Warnings check to GitHub Actions with cache - added compile warnings check to GitHub Actions - reformated GitHub Actions YAML * Dialyzer - allow it in test env * Dialyzer - fixed test env - renamed GitHub actions steps * AppSignal - upgraded deprecated version - Upgraded: appsignal 2.0.5 => 2.0.7 certifi 2.5.2 => 2.5.3 hackney 1.16.0 => 1.17.0 idna 6.0.1 => 6.1.1 parse_trans 3.3.0 => 3.3.1 unicode_util_compat 0.5.0 => 0.7.0 (minor) * Credo - fixed CRM plug
112 lines
2.8 KiB
Elixir
112 lines
2.8 KiB
Elixir
defmodule Plausible.Workers.SendSiteSetupEmails do
|
|
use Plausible.Repo
|
|
use Oban.Worker, queue: :site_setup_emails
|
|
require Logger
|
|
alias Plausible.Stats.Clickhouse, as: Stats
|
|
|
|
@impl Oban.Worker
|
|
def perform(_args, _job) do
|
|
send_create_site_emails()
|
|
send_setup_help_emails()
|
|
send_setup_success_emails()
|
|
|
|
:ok
|
|
end
|
|
|
|
defp send_create_site_emails() do
|
|
q =
|
|
from(s in Plausible.Auth.User,
|
|
left_join: se in "create_site_emails",
|
|
on: se.user_id == s.id,
|
|
where: is_nil(se.id),
|
|
where:
|
|
s.inserted_at > fragment("(now() at time zone 'utc') - '72 hours'::interval") and
|
|
s.inserted_at < fragment("(now() at time zone 'utc') - '48 hours'::interval"),
|
|
preload: :sites
|
|
)
|
|
|
|
for user <- Repo.all(q) do
|
|
if Enum.empty?(user.sites) do
|
|
send_create_site_email(user)
|
|
end
|
|
end
|
|
end
|
|
|
|
defp send_setup_help_emails() do
|
|
q =
|
|
from(s in Plausible.Site,
|
|
left_join: se in "setup_help_emails",
|
|
on: se.site_id == s.id,
|
|
where: is_nil(se.id),
|
|
where: s.inserted_at > fragment("(now() at time zone 'utc') - '72 hours'::interval"),
|
|
preload: :members
|
|
)
|
|
|
|
for site <- Repo.all(q) do
|
|
owner = List.first(site.members)
|
|
|
|
setup_completed = Stats.has_pageviews?(site)
|
|
hours_passed = Timex.diff(Timex.now(), site.inserted_at, :hours)
|
|
|
|
if !setup_completed && hours_passed > 47 do
|
|
send_setup_help_email(owner, site)
|
|
end
|
|
end
|
|
end
|
|
|
|
defp send_setup_success_emails() do
|
|
q =
|
|
from(s in Plausible.Site,
|
|
left_join: se in "setup_success_emails",
|
|
on: se.site_id == s.id,
|
|
where: is_nil(se.id),
|
|
where: s.inserted_at > fragment("(now() at time zone 'utc') - '72 hours'::interval"),
|
|
preload: :members
|
|
)
|
|
|
|
for site <- Repo.all(q) do
|
|
owner = List.first(site.members)
|
|
|
|
if Stats.has_pageviews?(site) do
|
|
send_setup_success_email(owner, site)
|
|
end
|
|
end
|
|
end
|
|
|
|
defp send_create_site_email(user) do
|
|
PlausibleWeb.Email.create_site_email(user)
|
|
|> Plausible.Mailer.send_email()
|
|
|
|
Repo.insert_all("create_site_emails", [
|
|
%{
|
|
user_id: user.id,
|
|
timestamp: NaiveDateTime.utc_now()
|
|
}
|
|
])
|
|
end
|
|
|
|
defp send_setup_success_email(user, site) do
|
|
PlausibleWeb.Email.site_setup_success(user, site)
|
|
|> Plausible.Mailer.send_email()
|
|
|
|
Repo.insert_all("setup_success_emails", [
|
|
%{
|
|
site_id: site.id,
|
|
timestamp: NaiveDateTime.utc_now()
|
|
}
|
|
])
|
|
end
|
|
|
|
defp send_setup_help_email(user, site) do
|
|
PlausibleWeb.Email.site_setup_help(user, site)
|
|
|> Plausible.Mailer.send_email()
|
|
|
|
Repo.insert_all("setup_help_emails", [
|
|
%{
|
|
site_id: site.id,
|
|
timestamp: NaiveDateTime.utc_now()
|
|
}
|
|
])
|
|
end
|
|
end
|