You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
analytics/lib/plausible_web/controllers/api/internal_controller.ex

83 lines
2.5 KiB
Elixir

defmodule PlausibleWeb.Api.InternalController do
use PlausibleWeb, :controller
use Plausible.Repo
alias Plausible.Stats.Clickhouse, as: Stats
alias Plausible.{Sites, Site, Auth}
alias Plausible.Auth.User
def domain_status(conn, %{"domain" => domain}) do
with %User{id: user_id} <- conn.assigns[:current_user],
%Site{} = site <- Sites.get_by_domain(domain),
true <- Sites.has_admin_access?(user_id, site) || Auth.is_super_admin?(user_id),
true <- Stats.has_pageviews?(site) do
json(conn, "READY")
else
_ ->
json(conn, "WAITING")
end
end
def sites(conn, _params) do
current_user = conn.assigns[:current_user]
if current_user do
sites = sites_for(current_user)
json(conn, %{data: sites})
else
PlausibleWeb.Api.Helpers.unauthorized(
conn,
"You need to be logged in to request a list of sites"
)
end
end
@features %{
"funnels" => Plausible.Billing.Feature.Funnels,
"props" => Plausible.Billing.Feature.Props,
"conversions" => Plausible.Billing.Feature.Goals
}
def disable_feature(conn, %{"domain" => domain, "feature" => feature}) do
with %User{id: user_id} <- conn.assigns[:current_user],
site <- Sites.get_by_domain(domain),
true <- Sites.has_admin_access?(user_id, site) || Auth.is_super_admin?(user_id),
{:ok, mod} <- Map.fetch(@features, feature),
{:ok, _site} <- mod.toggle(site, override: false) do
json(conn, "ok")
else
{:error, :upgrade_required} ->
PlausibleWeb.Api.Helpers.payment_required(
conn,
"This feature is part of the Plausible Business plan. To get access to this feature, please upgrade your account"
)
:error ->
PlausibleWeb.Api.Helpers.bad_request(
conn,
"The feature you tried to disable is not valid. Valid features are: #{@features |> Map.keys() |> Enum.join(", ")}"
)
_ ->
PlausibleWeb.Api.Helpers.unauthorized(
conn,
"You need to be logged in as the owner or admin account of this site"
)
end
end
defp sites_for(user) do
Repo.all(
from(
s in Site,
join: sm in Site.Membership,
on: sm.site_id == s.id,
where: sm.user_id == ^user.id,
order_by: s.domain,
select: %{domain: s.domain},
# there are keyboard shortcuts for switching between sites, hence 9
limit: 9
)
)
end
end