analytics/lib/plausible_web/controllers/api/internal_controller.ex

51 lines
1.1 KiB
Elixir
Raw Normal View History

2019-09-02 14:29:19 +03:00
defmodule PlausibleWeb.Api.InternalController do
use PlausibleWeb, :controller
use Plausible.Repo
2020-05-22 12:33:17 +03:00
alias Plausible.Stats.Clickhouse, as: Stats
2019-09-02 14:29:19 +03:00
def domain_status(conn, %{"domain" => domain}) do
2020-05-22 12:33:17 +03:00
if Stats.has_pageviews?(%Plausible.Site{domain: domain}) do
2019-09-02 14:29:19 +03:00
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, params)
|> buildResponse(conn)
json(conn, sites)
2021-03-03 12:36:19 +03:00
else
PlausibleWeb.Api.Helpers.unauthorized(
conn,
"You need to be logged in to request a list of sites"
)
2021-03-03 12:36:19 +03:00
end
end
defp sites_for(user, params) do
Repo.paginate(
from(
s in Plausible.Site,
join: sm in Plausible.Site.Membership,
on: sm.site_id == s.id,
where: sm.user_id == ^user.id,
order_by: s.domain
),
params
)
end
defp buildResponse({sites, pagination}, conn) do
%{
data: Enum.map(sites, &%{domain: &1.domain}),
pagination: Phoenix.Pagination.JSON.paginate(conn, pagination)
}
end
2019-09-02 14:29:19 +03:00
end