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
|
2020-08-13 13:45:18 +03:00
|
|
|
|
2022-04-18 12:32:01 +03:00
|
|
|
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
|
2022-04-18 12:32:01 +03:00
|
|
|
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
|
2020-08-13 13:45:18 +03:00
|
|
|
end
|
2022-04-18 12:32:01 +03:00
|
|
|
|
|
|
|
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
|