10d2e698b76dfe4f368742ec5cac2d16a1f966b8
[akkoma] / lib / pleroma / web / admin_api / views / account_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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
9 alias Pleroma.Web.AdminAPI
10 alias Pleroma.Web.AdminAPI.AccountView
11 alias Pleroma.Web.MastodonAPI
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("index.json", %{users: users}) do
23 %{
24 users: render_many(users, AccountView, "show.json", as: :user)
25 }
26 end
27
28 def render("credentials.json", %{user: user, for: for_user}) do
29 user = User.sanitize_html(user, User.html_filter_policy(for_user))
30 avatar = User.avatar_url(user) |> MediaProxy.url()
31 banner = User.banner_url(user) |> MediaProxy.url()
32 background = image_url(user.background) |> MediaProxy.url()
33
34 user
35 |> Map.take([
36 :id,
37 :bio,
38 :email,
39 :fields,
40 :name,
41 :nickname,
42 :is_locked,
43 :no_rich_text,
44 :default_scope,
45 :hide_follows,
46 :hide_followers_count,
47 :hide_follows_count,
48 :hide_followers,
49 :hide_favorites,
50 :allow_following_move,
51 :show_role,
52 :skip_thread_containment,
53 :pleroma_settings_store,
54 :raw_fields,
55 :is_discoverable,
56 :actor_type
57 ])
58 |> Map.merge(%{
59 "avatar" => avatar,
60 "banner" => banner,
61 "background" => background
62 })
63 end
64
65 def render("show.json", %{user: user}) do
66 avatar = User.avatar_url(user) |> MediaProxy.url()
67 display_name = Pleroma.HTML.strip_tags(user.name || user.nickname)
68 user = User.sanitize_html(user, FastSanitize.Sanitizer.StripTags)
69
70 %{
71 "id" => user.id,
72 "email" => user.email,
73 "avatar" => avatar,
74 "nickname" => user.nickname,
75 "display_name" => display_name,
76 "deactivated" => user.deactivated,
77 "local" => user.local,
78 "roles" => User.roles(user),
79 "tags" => user.tags || [],
80 "is_confirmed" => user.is_confirmed,
81 "approval_pending" => user.approval_pending,
82 "url" => user.uri || user.ap_id,
83 "registration_reason" => user.registration_reason,
84 "actor_type" => user.actor_type
85 }
86 end
87
88 def render("created.json", %{user: user}) do
89 %{
90 type: "success",
91 code: 200,
92 data: %{
93 nickname: user.nickname,
94 email: user.email
95 }
96 }
97 end
98
99 def render("create-error.json", %{changeset: %Ecto.Changeset{changes: changes, errors: errors}}) do
100 %{
101 type: "error",
102 code: 409,
103 error: parse_error(errors),
104 data: %{
105 nickname: Map.get(changes, :nickname),
106 email: Map.get(changes, :email)
107 }
108 }
109 end
110
111 def merge_account_views(%User{} = user) do
112 MastodonAPI.AccountView.render("show.json", %{user: user, skip_visibility_check: true})
113 |> Map.merge(AdminAPI.AccountView.render("show.json", %{user: user}))
114 end
115
116 def merge_account_views(_), do: %{}
117
118 defp parse_error([]), do: ""
119
120 defp parse_error(errors) do
121 ## when nickname is duplicate ap_id constraint error is raised
122 nickname_error = Keyword.get(errors, :nickname) || Keyword.get(errors, :ap_id)
123 email_error = Keyword.get(errors, :email)
124 password_error = Keyword.get(errors, :password)
125
126 cond do
127 nickname_error ->
128 "nickname #{elem(nickname_error, 0)}"
129
130 email_error ->
131 "email #{elem(email_error, 0)}"
132
133 password_error ->
134 "password #{elem(password_error, 0)}"
135
136 true ->
137 ""
138 end
139 end
140
141 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
142 defp image_url(_), do: nil
143 end