Merge branch 'fix-all-custom-fields-deleted' into 'develop'
[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.HTML
9 alias Pleroma.User
10 alias Pleroma.User.Info
11 alias Pleroma.Web.AdminAPI.AccountView
12 alias Pleroma.Web.MediaProxy
13
14 def render("index.json", %{users: users, count: count, page_size: page_size}) do
15 %{
16 users: render_many(users, AccountView, "show.json", as: :user),
17 count: count,
18 page_size: page_size
19 }
20 end
21
22 def render("show.json", %{user: user}) do
23 avatar = User.avatar_url(user) |> MediaProxy.url()
24 display_name = HTML.strip_tags(user.name || user.nickname)
25
26 %{
27 "id" => user.id,
28 "avatar" => avatar,
29 "nickname" => user.nickname,
30 "display_name" => display_name,
31 "deactivated" => user.info.deactivated,
32 "local" => user.local,
33 "roles" => Info.roles(user.info),
34 "tags" => user.tags || []
35 }
36 end
37
38 def render("invite.json", %{invite: invite}) do
39 %{
40 "id" => invite.id,
41 "token" => invite.token,
42 "used" => invite.used,
43 "expires_at" => invite.expires_at,
44 "uses" => invite.uses,
45 "max_use" => invite.max_use,
46 "invite_type" => invite.invite_type
47 }
48 end
49
50 def render("invites.json", %{invites: invites}) do
51 %{
52 invites: render_many(invites, AccountView, "invite.json", as: :invite)
53 }
54 end
55
56 def render("created.json", %{user: user}) do
57 %{
58 type: "success",
59 code: 200,
60 data: %{
61 nickname: user.nickname,
62 email: user.email
63 }
64 }
65 end
66
67 def render("create-error.json", %{changeset: %Ecto.Changeset{changes: changes, errors: errors}}) do
68 %{
69 type: "error",
70 code: 409,
71 error: parse_error(errors),
72 data: %{
73 nickname: Map.get(changes, :nickname),
74 email: Map.get(changes, :email)
75 }
76 }
77 end
78
79 defp parse_error([]), do: ""
80
81 defp parse_error(errors) do
82 ## when nickname is duplicate ap_id constraint error is raised
83 nickname_error = Keyword.get(errors, :nickname) || Keyword.get(errors, :ap_id)
84 email_error = Keyword.get(errors, :email)
85 password_error = Keyword.get(errors, :password)
86
87 cond do
88 nickname_error ->
89 "nickname #{elem(nickname_error, 0)}"
90
91 email_error ->
92 "email #{elem(email_error, 0)}"
93
94 password_error ->
95 "password #{elem(password_error, 0)}"
96
97 true ->
98 ""
99 end
100 end
101 end