Merge remote-tracking branch 'upstream/develop' into admin-create-users
[akkoma] / lib / pleroma / web / admin_api / views / account_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.AdminAPI.AccountView do
6 use Pleroma.Web, :view
7
8 alias Pleroma.User.Info
9 alias Pleroma.Web.AdminAPI.AccountView
10
11 def render("index.json", %{users: users, count: count, page_size: page_size}) do
12 %{
13 users: render_many(users, AccountView, "show.json", as: :user),
14 count: count,
15 page_size: page_size
16 }
17 end
18
19 def render("show.json", %{user: user}) do
20 %{
21 "id" => user.id,
22 "nickname" => user.nickname,
23 "deactivated" => user.info.deactivated,
24 "local" => user.local,
25 "roles" => Info.roles(user.info),
26 "tags" => user.tags || []
27 }
28 end
29
30 def render("invite.json", %{invite: invite}) do
31 %{
32 "id" => invite.id,
33 "token" => invite.token,
34 "used" => invite.used,
35 "expires_at" => invite.expires_at,
36 "uses" => invite.uses,
37 "max_use" => invite.max_use,
38 "invite_type" => invite.invite_type
39 }
40 end
41
42 def render("invites.json", %{invites: invites}) do
43 %{
44 invites: render_many(invites, AccountView, "invite.json", as: :invite)
45 }
46 end
47
48 def render("created.json", %{user: user}) do
49 %{
50 type: "success",
51 code: 200,
52 data: %{
53 nickname: user.nickname,
54 email: user.email
55 }
56 }
57 end
58
59 def render("create-error.json", %{changeset: %Ecto.Changeset{changes: changes, errors: errors}}) do
60 %{
61 type: "error",
62 code: 409,
63 error: parse_error(errors),
64 data: %{
65 nickname: Map.get(changes, :nickname),
66 email: Map.get(changes, :email)
67 }
68 }
69 end
70
71 defp parse_error([]), do: ""
72
73 defp parse_error(errors) do
74 ## when nickname is duplicate ap_id constraint error is raised
75 nickname_error = Keyword.get(errors, :nickname) || Keyword.get(errors, :ap_id)
76 email_error = Keyword.get(errors, :email)
77 password_error = Keyword.get(errors, :password)
78
79 cond do
80 nickname_error ->
81 "nickname #{elem(nickname_error, 0)}"
82
83 email_error ->
84 "email #{elem(email_error, 0)}"
85
86 password_error ->
87 "password #{elem(password_error, 0)}"
88
89 true ->
90 ""
91 end
92 end
93 end