2019-09-02 14:29:19 +03:00
|
|
|
defmodule PlausibleWeb.SiteController do
|
|
|
|
use PlausibleWeb, :controller
|
|
|
|
use Plausible.Repo
|
2019-10-31 08:39:51 +03:00
|
|
|
alias Plausible.{Sites, Goals}
|
2019-09-02 14:29:19 +03:00
|
|
|
|
|
|
|
plug PlausibleWeb.RequireAccountPlug
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
plug PlausibleWeb.AuthorizeSiteAccess,
|
|
|
|
[:owner, :admin] when action not in [:index, :new, :create_site]
|
|
|
|
|
2021-05-05 16:17:59 +03:00
|
|
|
def index(conn, params) do
|
2020-11-26 11:19:26 +03:00
|
|
|
user = conn.assigns[:current_user]
|
2020-12-29 16:17:27 +03:00
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
invitations =
|
|
|
|
Repo.all(
|
|
|
|
from i in Plausible.Auth.Invitation,
|
|
|
|
where: i.email == ^user.email
|
|
|
|
)
|
|
|
|
|> Repo.preload(:site)
|
|
|
|
|
|
|
|
invitation_site_ids = Enum.map(invitations, & &1.site.id)
|
|
|
|
|
2021-05-05 16:17:59 +03:00
|
|
|
{sites, pagination} =
|
|
|
|
Repo.paginate(
|
|
|
|
from(s in Plausible.Site,
|
2020-12-29 16:17:27 +03:00
|
|
|
join: sm in Plausible.Site.Membership,
|
|
|
|
on: sm.site_id == s.id,
|
|
|
|
where: sm.user_id == ^user.id,
|
2021-06-16 15:00:07 +03:00
|
|
|
where: s.id not in ^invitation_site_ids,
|
|
|
|
order_by: s.domain,
|
|
|
|
preload: [memberships: sm]
|
2021-05-05 16:17:59 +03:00
|
|
|
),
|
|
|
|
params
|
2020-12-29 16:17:27 +03:00
|
|
|
)
|
2020-11-26 11:19:26 +03:00
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
user_owns_sites =
|
|
|
|
Enum.any?(sites, fn site -> List.first(site.memberships).role == :owner end) ||
|
|
|
|
Plausible.Auth.user_owns_sites?(user)
|
|
|
|
|
|
|
|
visitors =
|
|
|
|
Plausible.Stats.Clickhouse.last_24h_visitors(sites ++ Enum.map(invitations, & &1.site))
|
|
|
|
|
|
|
|
render(conn, "index.html",
|
|
|
|
invitations: invitations,
|
|
|
|
sites: sites,
|
|
|
|
visitors: visitors,
|
|
|
|
pagination: pagination,
|
|
|
|
needs_to_upgrade: user_owns_sites && Plausible.Billing.needs_to_upgrade?(user)
|
|
|
|
)
|
2020-03-31 16:16:21 +03:00
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
def new(conn, _params) do
|
2021-09-08 15:15:37 +03:00
|
|
|
current_user = conn.assigns[:current_user] |> Repo.preload(site_memberships: :site)
|
|
|
|
|
|
|
|
owned_site_count =
|
|
|
|
current_user.site_memberships |> Enum.filter(fn m -> m.role == :owner end) |> Enum.count()
|
|
|
|
|
2021-05-04 15:37:58 +03:00
|
|
|
site_limit = Plausible.Billing.sites_limit(current_user)
|
2021-09-08 15:15:37 +03:00
|
|
|
is_at_limit = site_limit && owned_site_count >= site_limit
|
|
|
|
is_first_site = Enum.empty?(current_user.site_memberships)
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2021-05-04 15:37:58 +03:00
|
|
|
changeset = Plausible.Site.changeset(%Plausible.Site{})
|
2020-12-15 12:30:45 +03:00
|
|
|
|
2020-12-29 16:17:27 +03:00
|
|
|
render(conn, "new.html",
|
|
|
|
changeset: changeset,
|
|
|
|
is_first_site: is_first_site,
|
2021-05-04 15:37:58 +03:00
|
|
|
is_at_limit: is_at_limit,
|
|
|
|
site_limit: site_limit,
|
2020-12-29 16:17:27 +03:00
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_site(conn, %{"site" => site_params}) do
|
|
|
|
user = conn.assigns[:current_user]
|
2021-06-16 15:00:07 +03:00
|
|
|
site_count = Enum.count(Plausible.Sites.owned_by(user))
|
2021-05-04 15:37:58 +03:00
|
|
|
is_first_site = site_count == 0
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2021-05-04 15:37:58 +03:00
|
|
|
case Sites.create(user, site_params) do
|
2019-09-02 14:29:19 +03:00
|
|
|
{:ok, %{site: site}} ->
|
2020-11-05 11:02:18 +03:00
|
|
|
Plausible.Slack.notify("#{user.name} created #{site.domain} [email=#{user.email}]")
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2020-12-15 13:09:03 +03:00
|
|
|
if is_first_site do
|
|
|
|
PlausibleWeb.Email.welcome_email(user)
|
|
|
|
|> Plausible.Mailer.send_email()
|
|
|
|
end
|
|
|
|
|
2019-09-10 18:51:34 +03:00
|
|
|
conn
|
|
|
|
|> put_session(site.domain <> "_offer_email_report", true)
|
2021-10-26 11:59:14 +03:00
|
|
|
|> redirect(to: Routes.site_path(conn, :add_snippet, site.domain))
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
{:error, :site, changeset, _} ->
|
2020-06-08 10:35:13 +03:00
|
|
|
render(conn, "new.html",
|
|
|
|
changeset: changeset,
|
2020-12-15 12:30:45 +03:00
|
|
|
is_first_site: is_first_site,
|
2021-05-04 15:37:58 +03:00
|
|
|
is_at_limit: false,
|
2020-06-08 10:35:13 +03:00
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2021-05-04 15:37:58 +03:00
|
|
|
|
|
|
|
{:error, :limit, _limit} ->
|
|
|
|
send_resp(conn, 400, "Site limit reached")
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def add_snippet(conn, _params) do
|
2020-12-15 12:30:45 +03:00
|
|
|
user = conn.assigns[:current_user]
|
2021-06-16 15:00:07 +03:00
|
|
|
site = conn.assigns[:site] |> Repo.preload(:custom_domain)
|
2020-02-26 12:46:28 +03:00
|
|
|
|
2020-12-29 16:17:27 +03:00
|
|
|
is_first_site =
|
|
|
|
!Repo.exists?(
|
|
|
|
from sm in Plausible.Site.Membership,
|
|
|
|
where:
|
|
|
|
sm.user_id == ^user.id and
|
|
|
|
sm.site_id != ^site.id
|
|
|
|
)
|
2020-12-15 12:30:45 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
2020-12-29 16:17:27 +03:00
|
|
|
|> render("snippet.html",
|
|
|
|
site: site,
|
|
|
|
is_first_site: is_first_site,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def new_goal(conn, _params) do
|
|
|
|
site = conn.assigns[:site]
|
2019-10-31 08:39:51 +03:00
|
|
|
changeset = Plausible.Goal.changeset(%Plausible.Goal{})
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
|
|
|
|> render("new_goal.html",
|
|
|
|
site: site,
|
|
|
|
changeset: changeset,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def create_goal(conn, %{"goal" => goal}) do
|
|
|
|
site = conn.assigns[:site]
|
2019-10-31 08:39:51 +03:00
|
|
|
|
|
|
|
case Plausible.Goals.create(site, goal) do
|
|
|
|
{:ok, _} ->
|
|
|
|
conn
|
2021-03-15 12:40:53 +03:00
|
|
|
|> put_flash(:success, "Goal created successfully")
|
2021-10-26 11:59:14 +03:00
|
|
|
|> redirect(to: Routes.site_path(conn, :settings_goals, site.domain))
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2020-11-27 10:54:29 +03:00
|
|
|
{:error, changeset} ->
|
2019-10-31 08:39:51 +03:00
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
|
|
|
|> render("new_goal.html",
|
|
|
|
site: site,
|
|
|
|
changeset: changeset,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-31 09:20:45 +03:00
|
|
|
def delete_goal(conn, %{"website" => website, "id" => goal_id}) do
|
|
|
|
Plausible.Goals.delete(goal_id)
|
2019-10-31 08:39:51 +03:00
|
|
|
|
|
|
|
conn
|
2021-03-15 12:40:53 +03:00
|
|
|
|> put_flash(:success, "Goal deleted successfully")
|
2021-10-26 11:59:14 +03:00
|
|
|
|> redirect(to: Routes.site_path(conn, :settings_goals, website))
|
2019-10-31 08:39:51 +03:00
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
def settings(conn, %{"website" => website}) do
|
2021-10-26 11:59:14 +03:00
|
|
|
redirect(conn, to: Routes.site_path(conn, :settings_general, website))
|
2020-11-06 14:30:38 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def settings_general(conn, _params) do
|
2020-12-29 16:17:27 +03:00
|
|
|
site =
|
2021-06-16 15:00:07 +03:00
|
|
|
conn.assigns[:site]
|
2020-12-29 16:17:27 +03:00
|
|
|
|> Repo.preload(:custom_domain)
|
2020-11-19 15:57:55 +03:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
|
|
|
|> render("settings_general.html",
|
|
|
|
site: site,
|
|
|
|
changeset: Plausible.Site.changeset(site, %{}),
|
|
|
|
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
|
|
|
|
)
|
|
|
|
end
|
2020-11-16 16:38:44 +03:00
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def settings_people(conn, _params) do
|
|
|
|
site =
|
|
|
|
conn.assigns[:site]
|
2021-06-17 10:51:58 +03:00
|
|
|
|> Repo.preload(memberships: :user, invitations: [], custom_domain: [])
|
2021-06-16 15:00:07 +03:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
|
|
|
|> render("settings_people.html",
|
|
|
|
site: site,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
def settings_visibility(conn, _params) do
|
2021-06-17 10:51:58 +03:00
|
|
|
site = conn.assigns[:site] |> Repo.preload(:custom_domain)
|
2020-11-16 16:38:44 +03:00
|
|
|
shared_links = Repo.all(from l in Plausible.Site.SharedLink, where: l.site_id == ^site.id)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
2020-11-19 15:57:55 +03:00
|
|
|
|> render("settings_visibility.html",
|
2020-11-16 16:38:44 +03:00
|
|
|
site: site,
|
|
|
|
shared_links: shared_links,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def settings_goals(conn, _params) do
|
2021-06-17 10:51:58 +03:00
|
|
|
site = conn.assigns[:site] |> Repo.preload(:custom_domain)
|
2020-11-16 16:38:44 +03:00
|
|
|
goals = Goals.for_site(site.domain)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
|
|
|
|> render("settings_goals.html",
|
|
|
|
site: site,
|
|
|
|
goals: goals,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def settings_search_console(conn, _params) do
|
2020-12-29 16:17:27 +03:00
|
|
|
site =
|
2021-06-16 15:00:07 +03:00
|
|
|
conn.assigns[:site]
|
2021-06-17 10:51:58 +03:00
|
|
|
|> Repo.preload([:google_auth, :custom_domain])
|
2019-09-02 14:29:19 +03:00
|
|
|
|
2020-06-08 10:35:13 +03:00
|
|
|
search_console_domains =
|
|
|
|
if site.google_auth do
|
|
|
|
Plausible.Google.Api.fetch_verified_properties(site.google_auth)
|
|
|
|
end
|
2019-09-02 14:29:19 +03:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
2020-11-16 16:38:44 +03:00
|
|
|
|> render("settings_search_console.html",
|
2019-09-07 17:01:37 +03:00
|
|
|
site: site,
|
2019-10-10 07:12:15 +03:00
|
|
|
search_console_domains: search_console_domains,
|
2020-11-16 16:38:44 +03:00
|
|
|
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
|
2019-09-07 17:01:37 +03:00
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def settings_email_reports(conn, _params) do
|
2021-06-17 10:51:58 +03:00
|
|
|
site = conn.assigns[:site] |> Repo.preload(:custom_domain)
|
2020-11-06 14:30:38 +03:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
2020-11-16 16:38:44 +03:00
|
|
|
|> render("settings_email_reports.html",
|
2020-11-06 14:30:38 +03:00
|
|
|
site: site,
|
2020-11-16 16:38:44 +03:00
|
|
|
weekly_report: Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id),
|
|
|
|
monthly_report: Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id),
|
2020-12-09 16:59:41 +03:00
|
|
|
spike_notification: Repo.get_by(Plausible.Site.SpikeNotification, site_id: site.id),
|
2020-11-16 16:38:44 +03:00
|
|
|
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
|
2020-11-06 14:30:38 +03:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def settings_custom_domain(conn, _params) do
|
2020-12-29 16:17:27 +03:00
|
|
|
site =
|
2021-06-16 15:00:07 +03:00
|
|
|
conn.assigns[:site]
|
2020-11-16 16:38:44 +03:00
|
|
|
|> Repo.preload(:custom_domain)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
2020-12-29 16:17:27 +03:00
|
|
|
|> render("settings_custom_domain.html",
|
|
|
|
site: site,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
|
|
|
|
)
|
2020-11-16 16:38:44 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def settings_danger_zone(conn, _params) do
|
2021-06-17 10:51:58 +03:00
|
|
|
site = conn.assigns[:site] |> Repo.preload(:custom_domain)
|
|
|
|
|
2020-11-19 15:57:55 +03:00
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
|
|
|
|> render("settings_danger_zone.html",
|
2021-06-17 10:51:58 +03:00
|
|
|
site: site,
|
2020-11-19 15:57:55 +03:00
|
|
|
layout: {PlausibleWeb.LayoutView, "site_settings.html"}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def update_google_auth(conn, %{"google_auth" => attrs}) do
|
|
|
|
site = conn.assigns[:site] |> Repo.preload(:google_auth)
|
2019-10-10 07:12:15 +03:00
|
|
|
|
|
|
|
Plausible.Site.GoogleAuth.set_property(site.google_auth, attrs)
|
2020-06-08 10:35:13 +03:00
|
|
|
|> Repo.update!()
|
2019-10-10 07:12:15 +03:00
|
|
|
|
|
|
|
conn
|
2021-03-15 12:40:53 +03:00
|
|
|
|> put_flash(:success, "Google integration saved successfully")
|
2021-10-26 11:59:14 +03:00
|
|
|
|> redirect(to: Routes.site_path(conn, :settings_search_console, site.domain))
|
2019-10-10 07:12:15 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def delete_google_auth(conn, _params) do
|
2020-06-30 11:11:47 +03:00
|
|
|
site =
|
2021-06-16 15:00:07 +03:00
|
|
|
conn.assigns[:site]
|
2020-06-30 11:11:47 +03:00
|
|
|
|> Repo.preload(:google_auth)
|
|
|
|
|
|
|
|
Repo.delete!(site.google_auth)
|
|
|
|
|
|
|
|
conn
|
2020-11-16 16:38:44 +03:00
|
|
|
|> put_flash(:success, "Google account unlinked from Plausible")
|
2021-10-26 11:59:14 +03:00
|
|
|
|> redirect(to: Routes.site_path(conn, :settings_search_console, site.domain))
|
2020-06-30 11:11:47 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def update_settings(conn, %{"site" => site_params}) do
|
|
|
|
site = conn.assigns[:site]
|
2019-11-21 11:59:06 +03:00
|
|
|
changeset = site |> Plausible.Site.changeset(site_params)
|
2020-06-08 10:35:13 +03:00
|
|
|
res = changeset |> Repo.update()
|
2019-09-02 14:29:19 +03:00
|
|
|
|
|
|
|
case res do
|
|
|
|
{:ok, site} ->
|
2019-11-21 11:59:06 +03:00
|
|
|
site_session_key = "authorized_site__" <> site.domain
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
conn
|
2019-11-21 11:59:06 +03:00
|
|
|
|> put_session(site_session_key, nil)
|
2020-11-16 16:38:44 +03:00
|
|
|
|> put_flash(:success, "Your site settings have been saved")
|
2021-10-26 11:59:14 +03:00
|
|
|
|> redirect(to: Routes.site_path(conn, :settings_general, site.domain))
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
{:error, changeset} ->
|
2021-01-07 16:16:04 +03:00
|
|
|
render(conn, "settings_general.html", site: site, changeset: changeset)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def reset_stats(conn, _params) do
|
|
|
|
site = conn.assigns[:site]
|
2020-09-17 16:36:01 +03:00
|
|
|
Plausible.ClickhouseRepo.clear_stats_for(site.domain)
|
2020-07-16 13:07:45 +03:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:success, "#{site.domain} stats will be reset in a few minutes")
|
2020-11-20 13:05:11 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/danger-zone")
|
2020-07-16 13:07:45 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def delete_site(conn, _params) do
|
|
|
|
site = conn.assigns[:site]
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
Repo.delete!(site)
|
2020-09-17 16:36:01 +03:00
|
|
|
Plausible.ClickhouseRepo.clear_stats_for(site.domain)
|
2019-09-02 14:29:19 +03:00
|
|
|
|
|
|
|
conn
|
2021-03-15 12:40:53 +03:00
|
|
|
|> put_flash(:success, "Site deleted successfully along with all pageviews")
|
2020-06-29 11:17:15 +03:00
|
|
|
|> redirect(to: "/sites")
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def make_public(conn, _params) do
|
2020-06-08 10:35:13 +03:00
|
|
|
site =
|
2021-06-16 15:00:07 +03:00
|
|
|
conn.assigns[:site]
|
2020-06-08 10:35:13 +03:00
|
|
|
|> Plausible.Site.make_public()
|
|
|
|
|> Repo.update!()
|
2019-09-02 14:29:19 +03:00
|
|
|
|
|
|
|
conn
|
2020-11-16 16:38:44 +03:00
|
|
|
|> put_flash(:success, "Stats for #{site.domain} are now public.")
|
2021-10-26 11:59:14 +03:00
|
|
|
|> redirect(to: Routes.site_path(conn, :settings_visibility, site.domain))
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def make_private(conn, _params) do
|
2020-06-08 10:35:13 +03:00
|
|
|
site =
|
2021-06-16 15:00:07 +03:00
|
|
|
conn.assigns[:site]
|
2020-06-08 10:35:13 +03:00
|
|
|
|> Plausible.Site.make_private()
|
|
|
|
|> Repo.update!()
|
2019-09-02 14:29:19 +03:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:success, "Stats for #{site.domain} are now private.")
|
2021-10-26 11:59:14 +03:00
|
|
|
|> redirect(to: Routes.site_path(conn, :settings_visibility, site.domain))
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def enable_weekly_report(conn, _params) do
|
|
|
|
site = conn.assigns[:site]
|
2019-09-07 17:01:37 +03:00
|
|
|
|
2019-09-09 14:19:21 +03:00
|
|
|
Plausible.Site.WeeklyReport.changeset(%Plausible.Site.WeeklyReport{}, %{
|
2019-09-10 18:51:34 +03:00
|
|
|
site_id: site.id,
|
2020-01-22 12:16:53 +03:00
|
|
|
recipients: [conn.assigns[:current_user].email]
|
2019-09-10 18:51:34 +03:00
|
|
|
})
|
2020-06-08 10:35:13 +03:00
|
|
|
|> Repo.insert!()
|
2019-09-07 17:01:37 +03:00
|
|
|
|
|
|
|
conn
|
2020-11-16 16:38:44 +03:00
|
|
|
|> put_flash(:success, "You will receive an email report every Monday going forward")
|
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/email-reports")
|
2019-09-07 17:01:37 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def disable_weekly_report(conn, _params) do
|
|
|
|
site = conn.assigns[:site]
|
2019-09-09 14:37:57 +03:00
|
|
|
Repo.delete_all(from wr in Plausible.Site.WeeklyReport, where: wr.site_id == ^site.id)
|
2019-09-07 17:01:37 +03:00
|
|
|
|
|
|
|
conn
|
2020-11-16 16:38:44 +03:00
|
|
|
|> put_flash(:success, "You will not receive weekly email reports going forward")
|
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/email-reports")
|
2019-09-07 17:01:37 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def add_weekly_report_recipient(conn, %{"recipient" => recipient}) do
|
|
|
|
site = conn.assigns[:site]
|
2020-01-22 12:16:53 +03:00
|
|
|
|
2019-09-09 14:37:57 +03:00
|
|
|
Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id)
|
2020-01-22 12:16:53 +03:00
|
|
|
|> Plausible.Site.WeeklyReport.add_recipient(recipient)
|
2020-06-08 10:35:13 +03:00
|
|
|
|> Repo.update!()
|
2019-09-09 14:37:57 +03:00
|
|
|
|
|
|
|
conn
|
2020-11-16 16:38:44 +03:00
|
|
|
|> put_flash(:success, "Added #{recipient} as a recipient for the weekly report")
|
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/email-reports")
|
2020-01-22 12:16:53 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def remove_weekly_report_recipient(conn, %{"recipient" => recipient}) do
|
|
|
|
site = conn.assigns[:site]
|
2020-01-22 12:16:53 +03:00
|
|
|
|
|
|
|
Repo.get_by(Plausible.Site.WeeklyReport, site_id: site.id)
|
|
|
|
|> Plausible.Site.WeeklyReport.remove_recipient(recipient)
|
2020-06-08 10:35:13 +03:00
|
|
|
|> Repo.update!()
|
2020-01-22 12:16:53 +03:00
|
|
|
|
|
|
|
conn
|
2020-06-08 10:35:13 +03:00
|
|
|
|> put_flash(
|
|
|
|
:success,
|
2020-11-16 16:38:44 +03:00
|
|
|
"Removed #{recipient} as a recipient for the weekly report"
|
2020-06-08 10:35:13 +03:00
|
|
|
)
|
2020-11-16 16:38:44 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/email-reports")
|
2019-09-09 14:37:57 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def enable_monthly_report(conn, _params) do
|
|
|
|
site = conn.assigns[:site]
|
2019-09-09 14:37:57 +03:00
|
|
|
|
|
|
|
Plausible.Site.MonthlyReport.changeset(%Plausible.Site.MonthlyReport{}, %{
|
|
|
|
site_id: site.id,
|
2020-01-22 12:16:53 +03:00
|
|
|
recipients: [conn.assigns[:current_user].email]
|
2019-09-09 14:37:57 +03:00
|
|
|
})
|
2020-06-08 10:35:13 +03:00
|
|
|
|> Repo.insert!()
|
2019-09-09 14:37:57 +03:00
|
|
|
|
|
|
|
conn
|
2020-11-16 16:38:44 +03:00
|
|
|
|> put_flash(:success, "You will receive an email report every month going forward")
|
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/email-reports")
|
2019-09-09 14:37:57 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def disable_monthly_report(conn, _params) do
|
|
|
|
site = conn.assigns[:site]
|
2019-09-09 14:37:57 +03:00
|
|
|
Repo.delete_all(from mr in Plausible.Site.MonthlyReport, where: mr.site_id == ^site.id)
|
|
|
|
|
|
|
|
conn
|
2020-11-16 16:38:44 +03:00
|
|
|
|> put_flash(:success, "You will not receive monthly email reports going forward")
|
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/email-reports")
|
2019-09-09 14:37:57 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def add_monthly_report_recipient(conn, %{"recipient" => recipient}) do
|
|
|
|
site = conn.assigns[:site]
|
2020-01-22 12:16:53 +03:00
|
|
|
|
2019-09-09 14:37:57 +03:00
|
|
|
Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id)
|
2020-01-22 12:16:53 +03:00
|
|
|
|> Plausible.Site.MonthlyReport.add_recipient(recipient)
|
2020-06-08 10:35:13 +03:00
|
|
|
|> Repo.update!()
|
2019-09-09 14:37:57 +03:00
|
|
|
|
|
|
|
conn
|
2020-11-16 16:38:44 +03:00
|
|
|
|> put_flash(:success, "Added #{recipient} as a recipient for the monthly report")
|
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/email-reports")
|
2019-09-09 14:37:57 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def remove_monthly_report_recipient(conn, %{"recipient" => recipient}) do
|
|
|
|
site = conn.assigns[:site]
|
2019-10-10 07:12:15 +03:00
|
|
|
|
2020-01-22 12:16:53 +03:00
|
|
|
Repo.get_by(Plausible.Site.MonthlyReport, site_id: site.id)
|
|
|
|
|> Plausible.Site.MonthlyReport.remove_recipient(recipient)
|
2020-06-08 10:35:13 +03:00
|
|
|
|> Repo.update!()
|
2019-10-10 07:12:15 +03:00
|
|
|
|
2020-01-22 12:16:53 +03:00
|
|
|
conn
|
2020-06-08 10:35:13 +03:00
|
|
|
|> put_flash(
|
|
|
|
:success,
|
2020-11-16 16:38:44 +03:00
|
|
|
"Removed #{recipient} as a recipient for the monthly report"
|
2020-06-08 10:35:13 +03:00
|
|
|
)
|
2020-11-16 16:38:44 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/email-reports")
|
2019-10-10 07:12:15 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def enable_spike_notification(conn, _params) do
|
|
|
|
site = conn.assigns[:site]
|
2020-12-09 16:59:41 +03:00
|
|
|
|
2021-01-19 12:41:15 +03:00
|
|
|
res =
|
|
|
|
Plausible.Site.SpikeNotification.changeset(%Plausible.Site.SpikeNotification{}, %{
|
|
|
|
site_id: site.id,
|
|
|
|
threshold: 10,
|
|
|
|
recipients: [conn.assigns[:current_user].email]
|
|
|
|
})
|
|
|
|
|> Repo.insert()
|
2020-12-09 16:59:41 +03:00
|
|
|
|
2021-01-19 12:41:15 +03:00
|
|
|
case res do
|
|
|
|
{:ok, _} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:success, "You will a notification with traffic spikes going forward")
|
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/email-reports")
|
|
|
|
|
|
|
|
{:error, _} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:error, "Unable to create a spike notification")
|
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/email-reports")
|
|
|
|
end
|
2020-12-09 16:59:41 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def disable_spike_notification(conn, _params) do
|
|
|
|
site = conn.assigns[:site]
|
2020-12-09 16:59:41 +03:00
|
|
|
Repo.delete_all(from mr in Plausible.Site.SpikeNotification, where: mr.site_id == ^site.id)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:success, "Spike notification disabled")
|
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/email-reports")
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def update_spike_notification(conn, %{"spike_notification" => params}) do
|
|
|
|
site = conn.assigns[:site]
|
2020-12-09 16:59:41 +03:00
|
|
|
notification = Repo.get_by(Plausible.Site.SpikeNotification, site_id: site.id)
|
|
|
|
|
|
|
|
Plausible.Site.SpikeNotification.changeset(notification, params)
|
|
|
|
|> Repo.update!()
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:success, "Notification settings updated")
|
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/email-reports")
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def add_spike_notification_recipient(conn, %{"recipient" => recipient}) do
|
|
|
|
site = conn.assigns[:site]
|
2020-12-09 16:59:41 +03:00
|
|
|
|
|
|
|
Repo.get_by(Plausible.Site.SpikeNotification, site_id: site.id)
|
|
|
|
|> Plausible.Site.SpikeNotification.add_recipient(recipient)
|
|
|
|
|> Repo.update!()
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:success, "Added #{recipient} as a recipient for the traffic spike notification")
|
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/email-reports")
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def remove_spike_notification_recipient(conn, %{"recipient" => recipient}) do
|
|
|
|
site = conn.assigns[:site]
|
2020-12-09 16:59:41 +03:00
|
|
|
|
|
|
|
Repo.get_by(Plausible.Site.SpikeNotification, site_id: site.id)
|
|
|
|
|> Plausible.Site.SpikeNotification.remove_recipient(recipient)
|
|
|
|
|> Repo.update!()
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(
|
|
|
|
:success,
|
|
|
|
"Removed #{recipient} as a recipient for the monthly report"
|
|
|
|
)
|
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/email-reports")
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def new_shared_link(conn, _params) do
|
|
|
|
site = conn.assigns[:site]
|
2020-01-29 12:29:11 +03:00
|
|
|
changeset = Plausible.Site.SharedLink.changeset(%Plausible.Site.SharedLink{}, %{})
|
|
|
|
|
2020-03-02 12:12:11 +03:00
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
2020-06-08 10:35:13 +03:00
|
|
|
|> render("new_shared_link.html",
|
|
|
|
site: site,
|
|
|
|
changeset: changeset,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2020-01-29 12:29:11 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def create_shared_link(conn, %{"shared_link" => link}) do
|
|
|
|
site = conn.assigns[:site]
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2021-04-15 15:31:57 +03:00
|
|
|
case Sites.create_shared_link(site, link["name"], link["password"]) do
|
2020-01-29 12:29:11 +03:00
|
|
|
{:ok, _created} ->
|
2020-11-20 13:05:11 +03:00
|
|
|
redirect(conn, to: "/#{URI.encode_www_form(site.domain)}/settings/visibility")
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2020-01-29 12:29:11 +03:00
|
|
|
{:error, changeset} ->
|
2020-03-02 12:12:11 +03:00
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
2020-06-08 10:35:13 +03:00
|
|
|
|> render("new_shared_link.html",
|
|
|
|
site: site,
|
|
|
|
changeset: changeset,
|
2021-04-06 14:32:38 +03:00
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def edit_shared_link(conn, %{"slug" => slug}) do
|
|
|
|
site = conn.assigns[:site]
|
2021-04-06 14:32:38 +03:00
|
|
|
shared_link = Repo.get_by(Plausible.Site.SharedLink, slug: slug)
|
|
|
|
changeset = Plausible.Site.SharedLink.changeset(shared_link, %{})
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
|
|
|
|> render("edit_shared_link.html",
|
|
|
|
site: site,
|
|
|
|
changeset: changeset,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def update_shared_link(conn, %{"slug" => slug, "shared_link" => params}) do
|
|
|
|
site = conn.assigns[:site]
|
2021-04-06 14:32:38 +03:00
|
|
|
shared_link = Repo.get_by(Plausible.Site.SharedLink, slug: slug)
|
|
|
|
changeset = Plausible.Site.SharedLink.changeset(shared_link, params)
|
|
|
|
|
|
|
|
case Repo.update(changeset) do
|
|
|
|
{:ok, _created} ->
|
|
|
|
redirect(conn, to: "/#{URI.encode_www_form(site.domain)}/settings/visibility")
|
|
|
|
|
|
|
|
{:error, changeset} ->
|
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
|
|
|
|> render("edit_shared_link.html",
|
|
|
|
site: site,
|
|
|
|
changeset: changeset,
|
2020-06-08 10:35:13 +03:00
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2020-01-29 12:29:11 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def delete_shared_link(conn, %{"slug" => slug}) do
|
|
|
|
site = conn.assigns[:site]
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2020-01-29 12:29:11 +03:00
|
|
|
Repo.get_by(Plausible.Site.SharedLink, slug: slug)
|
2020-06-08 10:35:13 +03:00
|
|
|
|> Repo.delete!()
|
2020-01-29 12:29:11 +03:00
|
|
|
|
2020-11-20 13:05:11 +03:00
|
|
|
redirect(conn, to: "/#{URI.encode_www_form(site.domain)}/settings/visibility")
|
2020-01-29 12:29:11 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def new_custom_domain(conn, _params) do
|
|
|
|
site = conn.assigns[:site]
|
2020-02-26 11:54:21 +03:00
|
|
|
changeset = Plausible.Site.CustomDomain.changeset(%Plausible.Site.CustomDomain{}, %{})
|
|
|
|
|
2020-03-02 12:12:11 +03:00
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
2020-06-08 10:35:13 +03:00
|
|
|
|> render("new_custom_domain.html",
|
|
|
|
site: site,
|
|
|
|
changeset: changeset,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2020-02-26 11:54:21 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def custom_domain_dns_setup(conn, _params) do
|
|
|
|
site = conn.assigns[:site] |> Repo.preload(:custom_domain)
|
2020-02-26 11:54:21 +03:00
|
|
|
|
2020-03-02 12:12:11 +03:00
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
2020-06-08 10:35:13 +03:00
|
|
|
|> render("custom_domain_dns_setup.html",
|
|
|
|
site: site,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2020-02-26 11:54:21 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def custom_domain_snippet(conn, _params) do
|
2020-06-08 10:35:13 +03:00
|
|
|
site =
|
2021-06-16 15:00:07 +03:00
|
|
|
conn.assigns[:site]
|
2020-06-08 10:35:13 +03:00
|
|
|
|> Repo.preload(:custom_domain)
|
2020-02-26 11:54:21 +03:00
|
|
|
|
2020-03-02 12:12:11 +03:00
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
2020-06-08 10:35:13 +03:00
|
|
|
|> render("custom_domain_snippet.html",
|
|
|
|
site: site,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2020-02-26 11:54:21 +03:00
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def add_custom_domain(conn, %{"custom_domain" => domain}) do
|
|
|
|
site = conn.assigns[:site]
|
2020-02-26 11:54:21 +03:00
|
|
|
|
|
|
|
case Sites.add_custom_domain(site, domain["domain"]) do
|
|
|
|
{:ok, _custom_domain} ->
|
|
|
|
redirect(conn, to: "/sites/#{URI.encode_www_form(site.domain)}/custom-domains/dns-setup")
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2020-02-26 11:54:21 +03:00
|
|
|
{:error, changeset} ->
|
2020-03-02 12:12:11 +03:00
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
2020-06-08 10:35:13 +03:00
|
|
|
|> render("new_custom_domain.html",
|
|
|
|
site: site,
|
|
|
|
changeset: changeset,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2020-02-26 11:54:21 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-06-16 15:00:07 +03:00
|
|
|
def delete_custom_domain(conn, _params) do
|
2020-07-21 09:58:00 +03:00
|
|
|
site =
|
2021-06-16 15:00:07 +03:00
|
|
|
conn.assigns[:site]
|
2020-07-21 09:58:00 +03:00
|
|
|
|> Repo.preload(:custom_domain)
|
2020-06-30 11:00:19 +03:00
|
|
|
|
|
|
|
Repo.delete!(site.custom_domain)
|
|
|
|
|
|
|
|
conn
|
2021-03-15 12:40:53 +03:00
|
|
|
|> put_flash(:success, "Custom domain deleted successfully")
|
2021-06-17 10:51:58 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings/general")
|
2020-06-30 11:00:19 +03:00
|
|
|
end
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|