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