Merge branch 'benchmarks/favourites_timeline' 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.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 "confirmation_pending" => user.confirmation_pending
41 }
42 end
43
44 def render("invite.json", %{invite: invite}) do
45 %{
46 "id" => invite.id,
47 "token" => invite.token,
48 "used" => invite.used,
49 "expires_at" => invite.expires_at,
50 "uses" => invite.uses,
51 "max_use" => invite.max_use,
52 "invite_type" => invite.invite_type
53 }
54 end
55
56 def render("invites.json", %{invites: invites}) do
57 %{
58 invites: render_many(invites, AccountView, "invite.json", as: :invite)
59 }
60 end
61
62 def render("created.json", %{user: user}) do
63 %{
64 type: "success",
65 code: 200,
66 data: %{
67 nickname: user.nickname,
68 email: user.email
69 }
70 }
71 end
72
73 def render("create-error.json", %{changeset: %Ecto.Changeset{changes: changes, errors: errors}}) do
74 %{
75 type: "error",
76 code: 409,
77 error: parse_error(errors),
78 data: %{
79 nickname: Map.get(changes, :nickname),
80 email: Map.get(changes, :email)
81 }
82 }
83 end
84
85 defp parse_error([]), do: ""
86
87 defp parse_error(errors) do
88 ## when nickname is duplicate ap_id constraint error is raised
89 nickname_error = Keyword.get(errors, :nickname) || Keyword.get(errors, :ap_id)
90 email_error = Keyword.get(errors, :email)
91 password_error = Keyword.get(errors, :password)
92
93 cond do
94 nickname_error ->
95 "nickname #{elem(nickname_error, 0)}"
96
97 email_error ->
98 "email #{elem(email_error, 0)}"
99
100 password_error ->
101 "password #{elem(password_error, 0)}"
102
103 true ->
104 ""
105 end
106 end
107 end