2020-06-02 13:37:38 +03:00
defmodule Plausible.Workers.SendSiteSetupEmails do
2020-03-23 12:34:25 +03:00
use Plausible.Repo
2020-06-02 13:37:38 +03:00
use Oban.Worker , queue : :site_setup_emails
2020-03-23 12:34:25 +03:00
require Logger
2020-05-22 12:33:17 +03:00
alias Plausible.Stats.Clickhouse , as : Stats
2020-03-23 12:34:25 +03:00
2020-06-02 13:37:38 +03:00
@impl Oban.Worker
def perform ( _args , _job ) do
send_create_site_emails ( )
send_setup_help_emails ( )
send_setup_success_emails ( )
2020-03-23 12:34:25 +03:00
2020-06-02 13:37:38 +03:00
:ok
2020-03-23 12:34:25 +03:00
end
2020-06-02 13:37:38 +03:00
defp send_create_site_emails ( ) do
2020-03-23 12:34:25 +03:00
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 . count ( user . sites ) == 0 do
2020-06-02 13:37:38 +03:00
send_create_site_email ( user )
2020-03-23 12:34:25 +03:00
end
end
end
2020-06-02 13:37:38 +03:00
defp send_setup_help_emails ( ) do
2020-03-23 12:34:25 +03:00
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 )
2020-05-22 12:33:17 +03:00
setup_completed = Stats . has_pageviews? ( site )
2020-03-23 12:34:25 +03:00
hours_passed = Timex . diff ( Timex . now ( ) , site . inserted_at , :hours )
if ! setup_completed && hours_passed > 47 do
2020-06-02 13:37:38 +03:00
send_setup_help_email ( owner , site )
2020-03-23 12:34:25 +03:00
end
end
end
2020-06-02 13:37:38 +03:00
defp send_setup_success_emails ( ) do
2020-03-23 12:34:25 +03:00
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 )
2020-05-22 12:33:17 +03:00
if Stats . has_pageviews? ( site ) do
2020-06-02 13:37:38 +03:00
send_setup_success_email ( owner , site )
2020-03-23 12:34:25 +03:00
end
end
end
2020-06-02 13:37:38 +03:00
defp send_create_site_email ( user ) do
2020-03-23 12:34:25 +03:00
PlausibleWeb.Email . create_site_email ( user )
2020-05-26 16:09:34 +03:00
|> Plausible.Mailer . send_email ( )
2020-03-23 12:34:25 +03:00
Repo . insert_all ( " create_site_emails " , [ %{
user_id : user . id ,
timestamp : NaiveDateTime . utc_now ( )
} ] )
end
2020-06-02 13:37:38 +03:00
defp send_setup_success_email ( user , site ) do
2020-03-23 12:34:25 +03:00
PlausibleWeb.Email . site_setup_success ( user , site )
2020-05-26 16:09:34 +03:00
|> Plausible.Mailer . send_email ( )
2020-03-23 12:34:25 +03:00
Repo . insert_all ( " setup_success_emails " , [ %{
site_id : site . id ,
timestamp : NaiveDateTime . utc_now ( )
} ] )
end
2020-06-02 13:37:38 +03:00
defp send_setup_help_email ( user , site ) do
2020-03-23 12:34:25 +03:00
PlausibleWeb.Email . site_setup_help ( user , site )
2020-05-26 16:09:34 +03:00
|> Plausible.Mailer . send_email ( )
2020-03-23 12:34:25 +03:00
Repo . insert_all ( " setup_help_emails " , [ %{
site_id : site . id ,
timestamp : NaiveDateTime . utc_now ( )
} ] )
end
end