-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathexternal_sites_controller.ex
More file actions
355 lines (298 loc) · 10.5 KB
/
external_sites_controller.ex
File metadata and controls
355 lines (298 loc) · 10.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
defmodule PlausibleWeb.Api.ExternalSitesController do
use PlausibleWeb, :controller
use Plausible.Repo
use PlausibleWeb.Plugs.ErrorHandler
import Plausible.Pagination
alias Plausible.Sites
alias Plausible.Goal
alias Plausible.Goals
alias Plausible.Teams
alias PlausibleWeb.Api.Helpers, as: H
@pagination_opts [cursor_fields: [{:id, :desc}], limit: 100, maximum_limit: 1000]
def index(conn, params) do
team = Teams.get(params["team_id"])
user = conn.assigns.current_user
page =
user
|> Sites.for_user_query(team)
|> paginate(params, @pagination_opts)
json(conn, %{
sites: page.entries,
meta: pagination_meta(page.metadata)
})
end
def guests_index(conn, params) do
user = conn.assigns.current_user
with {:ok, site_id} <- expect_param_key(params, "site_id"),
{:ok, site} <- get_site(user, site_id, [:owner, :admin, :editor, :viewer]) do
opts = [cursor_fields: [inserted_at: :desc, id: :desc], limit: 100, maximum_limit: 1000]
page = site |> Sites.list_guests_query() |> paginate(params, opts)
json(conn, %{
guests:
Enum.map(page.entries, fn entry ->
Map.take(entry, [:email, :role, :accepted])
end),
meta: pagination_meta(page.metadata)
})
else
{:missing, "site_id"} ->
H.bad_request(conn, "Parameter `site_id` is required to list goals")
{:error, :site_not_found} ->
H.not_found(conn, "Site could not be found")
end
end
def goals_index(conn, params) do
user = conn.assigns.current_user
with {:ok, site_id} <- expect_param_key(params, "site_id"),
{:ok, site} <- get_site(user, site_id, [:owner, :admin, :editor, :viewer]) do
page =
site
|> Plausible.Goals.for_site_query()
|> paginate(params, @pagination_opts)
json(conn, %{
goals:
Enum.map(page.entries, fn goal ->
%{
id: goal.id,
display_name: goal.display_name,
goal_type: Goal.type(goal),
event_name: goal.event_name,
page_path: goal.page_path
}
end),
meta: pagination_meta(page.metadata)
})
else
{:missing, "site_id"} ->
H.bad_request(conn, "Parameter `site_id` is required to list goals")
{:error, :site_not_found} ->
H.not_found(conn, "Site could not be found")
end
end
def create_site(conn, params) do
user = conn.assigns.current_user
team = Plausible.Teams.get(params["team_id"])
case Sites.create(user, params, team) do
{:ok, %{site: site}} ->
json(conn, site)
{:error, _, {:over_limit, limit}, _} ->
conn
|> put_status(402)
|> json(%{
error:
"Your account has reached the limit of #{limit} sites. To unlock more sites, please upgrade your subscription."
})
{:error, _, :permission_denied, _} ->
conn
|> put_status(403)
|> json(%{
error: "You can't add sites to the selected team."
})
{:error, _, :multiple_teams, _} ->
conn
|> put_status(400)
|> json(%{
error: "You must select a team with 'team_id' parameter."
})
{:error, _, changeset, _} ->
conn
|> put_status(400)
|> json(serialize_errors(changeset))
end
end
def get_site(conn, %{"site_id" => site_id}) do
case get_site(conn.assigns.current_user, site_id, [:owner, :admin, :editor, :viewer]) do
{:ok, site} ->
json(conn, %{
domain: site.domain,
timezone: site.timezone,
custom_properties: site.allowed_event_props || []
})
{:error, :site_not_found} ->
H.not_found(conn, "Site could not be found")
end
end
def delete_site(conn, %{"site_id" => site_id}) do
case get_site(conn.assigns.current_user, site_id, [:owner]) do
{:ok, site} ->
{:ok, _} = Plausible.Site.Removal.run(site)
json(conn, %{"deleted" => true})
{:error, :site_not_found} ->
H.not_found(conn, "Site could not be found")
end
end
def update_site(conn, %{"site_id" => site_id} = params) do
# for now this only allows to change the domain
with {:ok, site} <- get_site(conn.assigns.current_user, site_id, [:owner, :admin, :editor]),
{:ok, site} <- Plausible.Site.Domain.change(site, params["domain"]) do
json(conn, site)
else
{:error, :site_not_found} ->
H.not_found(conn, "Site could not be found")
{:error, %Ecto.Changeset{} = changeset} ->
conn
|> put_status(400)
|> json(serialize_errors(changeset))
end
end
def find_or_create_guest(conn, params) do
with {:ok, site_id} <- expect_param_key(params, "site_id"),
{:ok, email} <- expect_param_key(params, "email"),
{:ok, role} <- expect_param_key(params, "role", ["viewer", "editor"]),
{:ok, site} <- get_site(conn.assigns.current_user, site_id, [:owner, :admin]) do
existing = Repo.one(Sites.list_guests_query(site, email: email))
if existing do
json(conn, %{
role: existing.role,
email: existing.email,
accepted: existing.accepted
})
else
case Plausible.Site.Memberships.CreateInvitation.create_invitation(
site,
conn.assigns.current_user,
email,
role
) do
{:ok, invitation} ->
json(conn, %{
role: invitation.role,
email: invitation.team_invitation.email,
accepted: false
})
end
end
else
{:error, :site_not_found} ->
H.not_found(conn, "Site could not be found")
{:missing, "role"} ->
H.bad_request(
conn,
"Parameter `role` is required to create guest. Possible values: `viewer` or `editor`"
)
{:missing, param} ->
H.bad_request(conn, "Parameter `#{param}` is required to create guest")
end
end
def delete_guest(conn, params) do
with {:ok, site_id} <- expect_param_key(params, "site_id"),
{:ok, email} <- expect_param_key(params, "email"),
{:ok, site} <- get_site(conn.assigns.current_user, site_id, [:owner, :admin]) do
existing = Repo.one(Sites.list_guests_query(site, email: email))
case existing do
%{accepted: false, id: id} ->
with guest_invitation when not is_nil(guest_invitation) <-
Repo.get(Teams.GuestInvitation, id) do
Teams.Invitations.remove_guest_invitation(guest_invitation)
end
%{accepted: true, email: email} ->
with %{} = user <- Repo.get_by(Plausible.Auth.User, email: email) do
Teams.Memberships.remove(site, user)
end
_ ->
:ignore
end
json(conn, %{"deleted" => true})
else
{:error, :site_not_found} ->
H.not_found(conn, "Site could not be found")
{:missing, param} ->
H.bad_request(conn, "Parameter `#{param}` is required to delete a guest")
end
end
def find_or_create_shared_link(conn, params) do
with {:ok, site_id} <- expect_param_key(params, "site_id"),
{:ok, link_name} <- expect_param_key(params, "name"),
{:ok, site} <- get_site(conn.assigns.current_user, site_id, [:owner, :admin, :editor]) do
shared_link = Repo.get_by(Plausible.Site.SharedLink, site_id: site.id, name: link_name)
shared_link =
case shared_link do
nil -> Sites.create_shared_link(site, link_name)
link -> {:ok, link}
end
case shared_link do
{:ok, link} ->
json(conn, %{
name: link.name,
url: Sites.shared_link_url(site, link)
})
end
else
{:error, :site_not_found} ->
H.not_found(conn, "Site could not be found")
{:missing, "site_id"} ->
H.bad_request(conn, "Parameter `site_id` is required to create a shared link")
{:missing, "name"} ->
H.bad_request(conn, "Parameter `name` is required to create a shared link")
e ->
H.bad_request(conn, "Something went wrong: #{inspect(e)}")
end
end
def find_or_create_goal(conn, params) do
with {:ok, site_id} <- expect_param_key(params, "site_id"),
{:ok, _} <- expect_param_key(params, "goal_type"),
{:ok, site} <- get_site(conn.assigns.current_user, site_id, [:owner, :admin, :editor]),
{:ok, goal} <- Goals.find_or_create(site, params) do
json(conn, goal)
else
{:error, :site_not_found} ->
H.not_found(conn, "Site could not be found")
{:missing, param} ->
H.bad_request(conn, "Parameter `#{param}` is required to create a goal")
e ->
H.bad_request(conn, "Something went wrong: #{inspect(e)}")
end
end
def delete_goal(conn, params) do
with {:ok, site_id} <- expect_param_key(params, "site_id"),
{:ok, goal_id} <- expect_param_key(params, "goal_id"),
{:ok, site} <- get_site(conn.assigns.current_user, site_id, [:owner, :admin, :editor]),
:ok <- Goals.delete(goal_id, site) do
json(conn, %{"deleted" => true})
else
{:error, :site_not_found} ->
H.not_found(conn, "Site could not be found")
{:error, :not_found} ->
H.not_found(conn, "Goal could not be found")
{:missing, "site_id"} ->
H.bad_request(conn, "Parameter `site_id` is required to delete a goal")
{:missing, "goal_id"} ->
H.bad_request(conn, "Parameter `goal_id` is required to delete a goal")
e ->
H.bad_request(conn, "Something went wrong: #{inspect(e)}")
end
end
defp pagination_meta(meta) do
%{
after: meta.after,
before: meta.before,
limit: meta.limit
}
end
defp get_site(user, site_id, roles) do
case Plausible.Sites.get_for_user(user, site_id, roles) do
nil -> {:error, :site_not_found}
site -> {:ok, site}
end
end
defp serialize_errors(changeset) do
{field, {msg, _opts}} = List.first(changeset.errors)
error_msg = Atom.to_string(field) <> ": " <> msg
%{"error" => error_msg}
end
defp expect_param_key(params, key, inclusion \\ [])
defp expect_param_key(params, key, []) do
case Map.fetch(params, key) do
:error -> {:missing, key}
res -> res
end
end
defp expect_param_key(params, key, inclusion) do
case expect_param_key(params, key, []) do
{:ok, value} = ok ->
if value in inclusion, do: ok, else: {:missing, key}
_ ->
{:missing, key}
end
end
end