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