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
|
|
|
|
|
2020-03-31 16:16:21 +03:00
|
|
|
def index(conn, _params) do
|
|
|
|
user = conn.assigns[:current_user] |> Repo.preload(:sites)
|
|
|
|
render(conn, "index.html", sites: user.sites)
|
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
def new(conn, _params) do
|
|
|
|
changeset = Plausible.Site.changeset(%Plausible.Site{})
|
|
|
|
|
|
|
|
render(conn, "new.html", changeset: changeset, layout: {PlausibleWeb.LayoutView, "focus.html"})
|
|
|
|
end
|
|
|
|
|
|
|
|
def create_site(conn, %{"site" => site_params}) do
|
|
|
|
user = conn.assigns[:current_user]
|
|
|
|
|
|
|
|
case insert_site(user.id, site_params) do
|
|
|
|
{:ok, %{site: site}} ->
|
|
|
|
Plausible.Slack.notify("#{user.name} created #{site.domain}")
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2019-09-10 18:51:34 +03:00
|
|
|
conn
|
|
|
|
|> put_session(site.domain <> "_offer_email_report", true)
|
2020-02-04 16:44:13 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/snippet")
|
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,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def add_snippet(conn, %{"website" => website}) do
|
2020-06-08 10:35:13 +03:00
|
|
|
site =
|
|
|
|
Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
|> Repo.preload(:custom_domain)
|
2020-02-26 12:46:28 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
|
|
|
|> render("snippet.html", site: site, layout: {PlausibleWeb.LayoutView, "focus.html"})
|
|
|
|
end
|
|
|
|
|
2019-10-31 08:39:51 +03:00
|
|
|
def new_goal(conn, %{"website" => website}) do
|
|
|
|
site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
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
|
|
|
|
|
|
|
|
def create_goal(conn, %{"website" => website, "goal" => goal}) do
|
|
|
|
site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
|
|
|
|
case Plausible.Goals.create(site, goal) do
|
|
|
|
{:ok, _} ->
|
|
|
|
conn
|
|
|
|
|> put_flash(:success, "Goal created succesfully")
|
2020-02-04 16:44:13 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings")
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2019-10-31 08:39:51 +03:00
|
|
|
{:error, :goal, changeset, _} ->
|
|
|
|
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
|
|
|
|
|> put_flash(:success, "Goal deleted succesfully")
|
2020-02-04 16:44:13 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(website)}/settings")
|
2019-10-31 08:39:51 +03:00
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
def settings(conn, %{"website" => website}) do
|
2020-06-08 10:35:13 +03:00
|
|
|
site =
|
|
|
|
Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
|> Repo.preload(:google_auth)
|
|
|
|
|> Repo.preload(: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
|
|
|
|
2019-09-09 14:37:57 +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)
|
2019-11-28 07:01:07 +03:00
|
|
|
goals = Goals.for_site(site.domain)
|
2020-01-29 12:29:11 +03:00
|
|
|
shared_links = Repo.all(from l in Plausible.Site.SharedLink, where: l.site_id == ^site.id)
|
2019-09-10 18:17:48 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
conn
|
|
|
|
|> assign(:skip_plausible_tracking, true)
|
2019-09-07 17:01:37 +03:00
|
|
|
|> render("settings.html",
|
|
|
|
site: site,
|
2020-01-22 12:16:53 +03:00
|
|
|
weekly_report: weekly_report,
|
|
|
|
monthly_report: monthly_report,
|
2019-10-10 07:12:15 +03:00
|
|
|
search_console_domains: search_console_domains,
|
2019-11-28 07:01:07 +03:00
|
|
|
goals: goals,
|
2020-01-29 12:29:11 +03:00
|
|
|
shared_links: shared_links,
|
2019-09-09 14:37:57 +03:00
|
|
|
changeset: Plausible.Site.changeset(site, %{})
|
2019-09-07 17:01:37 +03:00
|
|
|
)
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
2019-10-10 07:12:15 +03:00
|
|
|
def update_google_auth(conn, %{"website" => website, "google_auth" => attrs}) do
|
2020-06-08 10:35:13 +03:00
|
|
|
site =
|
|
|
|
Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
|> 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
|
|
|
|
|> put_flash(:success, "Google integration saved succesfully")
|
2020-02-04 16:44:13 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings#google-auth")
|
2019-10-10 07:12:15 +03:00
|
|
|
end
|
|
|
|
|
2020-06-30 11:11:47 +03:00
|
|
|
def delete_google_auth(conn, %{"website" => website}) do
|
|
|
|
site =
|
|
|
|
Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
|> Repo.preload(:google_auth)
|
|
|
|
|
|
|
|
Repo.delete!(site.google_auth)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:success, "Google account unlinked succesfully")
|
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings#google-auth")
|
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
def update_settings(conn, %{"website" => website, "site" => site_params}) do
|
|
|
|
site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
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)
|
2019-09-02 14:29:19 +03:00
|
|
|
|> put_flash(:success, "Site settings saved succesfully")
|
2020-02-04 16:44:13 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings")
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
{:error, changeset} ->
|
|
|
|
render("settings.html", site: site, changeset: changeset)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-16 13:07:45 +03:00
|
|
|
def reset_stats(conn, %{"website" => website}) do
|
2020-07-21 09:58:00 +03:00
|
|
|
site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
2020-07-16 13:07:45 +03:00
|
|
|
Plausible.Clickhouse.delete_stats!(site)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:success, "#{site.domain} stats will be reset in a few minutes")
|
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings")
|
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
def delete_site(conn, %{"website" => website}) do
|
|
|
|
site =
|
|
|
|
Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
|> Repo.preload(:google_auth)
|
|
|
|
|
|
|
|
Repo.delete_all(from sm in "site_memberships", where: sm.site_id == ^site.id)
|
|
|
|
|
2019-09-06 12:50:46 +03:00
|
|
|
if site.google_auth do
|
|
|
|
Repo.delete!(site.google_auth)
|
|
|
|
end
|
2020-06-08 10:35:13 +03:00
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
Repo.delete!(site)
|
2020-07-16 13:07:45 +03:00
|
|
|
Plausible.Clickhouse.delete_stats!(site)
|
2019-09-02 14:29:19 +03:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:success, "Site deleted succesfully along with all pageviews")
|
2020-06-29 11:17:15 +03:00
|
|
|
|> redirect(to: "/sites")
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def make_public(conn, %{"website" => website}) do
|
2020-06-08 10:35:13 +03:00
|
|
|
site =
|
|
|
|
Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
|> Plausible.Site.make_public()
|
|
|
|
|> Repo.update!()
|
2019-09-02 14:29:19 +03:00
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:success, "Congrats! Stats for #{site.domain} are now public.")
|
2020-02-04 16:44:13 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings")
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def make_private(conn, %{"website" => website}) do
|
2020-06-08 10:35:13 +03:00
|
|
|
site =
|
|
|
|
Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
|> 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.")
|
2020-02-04 16:44:13 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings")
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
|
2019-09-09 14:37:57 +03:00
|
|
|
def enable_weekly_report(conn, %{"website" => website}) do
|
2019-09-07 17:01:37 +03:00
|
|
|
site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
|
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
|
|
|
|
|> put_flash(:success, "Success! You will receive an email report every Monday going forward")
|
2020-02-04 16:44:13 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings#email-reports")
|
2019-09-07 17:01:37 +03:00
|
|
|
end
|
|
|
|
|
2019-09-09 14:37:57 +03:00
|
|
|
def disable_weekly_report(conn, %{"website" => website}) do
|
2019-09-07 17:01:37 +03:00
|
|
|
site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
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
|
|
|
|
|> put_flash(:success, "Success! You will not receive weekly email reports going forward")
|
2020-02-04 16:44:13 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings#email-reports")
|
2019-09-07 17:01:37 +03:00
|
|
|
end
|
|
|
|
|
2020-01-22 12:16:53 +03:00
|
|
|
def add_weekly_report_recipient(conn, %{"website" => website, "recipient" => recipient}) do
|
2019-09-09 14:37:57 +03:00
|
|
|
site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
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-01-22 12:16:53 +03:00
|
|
|
|> put_flash(:success, "Succesfully added #{recipient} as a recipient for the weekly report")
|
2020-02-04 16:44:13 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings#email-reports")
|
2020-01-22 12:16:53 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def remove_weekly_report_recipient(conn, %{"website" => website, "recipient" => recipient}) do
|
|
|
|
site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
|
|
|
|
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,
|
|
|
|
"Succesfully removed #{recipient} as a recipient for the weekly report"
|
|
|
|
)
|
2020-02-04 16:44:13 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings#email-reports")
|
2019-09-09 14:37:57 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def enable_monthly_report(conn, %{"website" => website}) do
|
|
|
|
site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
|
|
|
|
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
|
|
|
|
|> put_flash(:success, "Success! You will receive an email report every month going forward")
|
2020-02-04 16:44:13 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings#email-reports")
|
2019-09-09 14:37:57 +03:00
|
|
|
end
|
|
|
|
|
|
|
|
def disable_monthly_report(conn, %{"website" => website}) do
|
|
|
|
site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
Repo.delete_all(from mr in Plausible.Site.MonthlyReport, where: mr.site_id == ^site.id)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:success, "Success! You will not receive monthly email reports going forward")
|
2020-02-04 16:44:13 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings#email-reports")
|
2019-09-09 14:37:57 +03:00
|
|
|
end
|
|
|
|
|
2020-01-22 12:16:53 +03:00
|
|
|
def add_monthly_report_recipient(conn, %{"website" => website, "recipient" => recipient}) do
|
2019-09-09 14:37:57 +03:00
|
|
|
site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
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-01-22 12:16:53 +03:00
|
|
|
|> put_flash(:success, "Succesfully added #{recipient} as a recipient for the monthly report")
|
2020-02-04 16:44:13 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings#email-reports")
|
2019-09-09 14:37:57 +03:00
|
|
|
end
|
|
|
|
|
2020-01-22 12:16:53 +03:00
|
|
|
def remove_monthly_report_recipient(conn, %{"website" => website, "recipient" => recipient}) do
|
2019-10-10 07:12:15 +03:00
|
|
|
site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
|
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,
|
|
|
|
"Succesfully removed #{recipient} as a recipient for the monthly report"
|
|
|
|
)
|
2020-02-04 16:44:13 +03:00
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings#email-reports")
|
2019-10-10 07:12:15 +03:00
|
|
|
end
|
|
|
|
|
2020-01-29 12:29:11 +03:00
|
|
|
def new_shared_link(conn, %{"website" => website}) do
|
|
|
|
site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
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
|
|
|
|
|
|
|
|
def create_shared_link(conn, %{"website" => website, "shared_link" => link}) do
|
|
|
|
site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
2020-06-08 10:35:13 +03:00
|
|
|
|
|
|
|
changes =
|
|
|
|
Plausible.Site.SharedLink.changeset(
|
|
|
|
%Plausible.Site.SharedLink{
|
|
|
|
site_id: site.id,
|
|
|
|
slug: Nanoid.generate()
|
|
|
|
},
|
|
|
|
link
|
|
|
|
)
|
2020-01-29 12:29:11 +03:00
|
|
|
|
|
|
|
case Repo.insert(changes) do
|
|
|
|
{:ok, _created} ->
|
2020-02-04 16:44:13 +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,
|
|
|
|
layout: {PlausibleWeb.LayoutView, "focus.html"}
|
|
|
|
)
|
2020-01-29 12:29:11 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def delete_shared_link(conn, %{"website" => website, "slug" => slug}) do
|
2020-02-04 16:44:13 +03:00
|
|
|
site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
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-02-04 16:44:13 +03:00
|
|
|
redirect(conn, to: "/#{URI.encode_www_form(site.domain)}/settings#visibility")
|
2020-01-29 12:29:11 +03:00
|
|
|
end
|
|
|
|
|
2020-02-26 11:54:21 +03:00
|
|
|
def new_custom_domain(conn, %{"website" => website}) do
|
|
|
|
site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
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
|
|
|
|
|
|
|
|
def custom_domain_dns_setup(conn, %{"website" => website}) do
|
2020-06-08 10:35:13 +03:00
|
|
|
site =
|
|
|
|
Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
|> 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
|
|
|
|
|
|
|
|
def custom_domain_snippet(conn, %{"website" => website}) do
|
2020-06-08 10:35:13 +03:00
|
|
|
site =
|
|
|
|
Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
|> 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
|
|
|
|
|
|
|
|
def add_custom_domain(conn, %{"website" => website, "custom_domain" => domain}) do
|
|
|
|
site = Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2020-06-30 11:00:19 +03:00
|
|
|
def delete_custom_domain(conn, %{"website" => website}) do
|
2020-07-21 09:58:00 +03:00
|
|
|
site =
|
|
|
|
Sites.get_for_user!(conn.assigns[:current_user].id, website)
|
|
|
|
|> Repo.preload(:custom_domain)
|
2020-06-30 11:00:19 +03:00
|
|
|
|
|
|
|
Repo.delete!(site.custom_domain)
|
|
|
|
|
|
|
|
conn
|
|
|
|
|> put_flash(:success, "Custom domain deleted succesfully")
|
|
|
|
|> redirect(to: "/#{URI.encode_www_form(site.domain)}/settings")
|
|
|
|
end
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
defp insert_site(user_id, params) do
|
|
|
|
site_changeset = Plausible.Site.changeset(%Plausible.Site{}, params)
|
|
|
|
|
|
|
|
Ecto.Multi.new()
|
|
|
|
|> Ecto.Multi.insert(:site, site_changeset)
|
2020-06-08 10:35:13 +03:00
|
|
|
|> Ecto.Multi.run(:site_membership, fn repo, %{site: site} ->
|
|
|
|
membership_changeset =
|
|
|
|
Plausible.Site.Membership.changeset(%Plausible.Site.Membership{}, %{
|
|
|
|
site_id: site.id,
|
|
|
|
user_id: user_id
|
|
|
|
})
|
|
|
|
|
2019-09-02 14:29:19 +03:00
|
|
|
repo.insert(membership_changeset)
|
|
|
|
end)
|
2020-06-08 10:35:13 +03:00
|
|
|
|> Repo.transaction()
|
2019-09-02 14:29:19 +03:00
|
|
|
end
|
|
|
|
end
|