Merge branch '1973-chats-fix-with-restrict-unauthenticated' into 'develop'
[akkoma] / lib / pleroma / web / admin_api / views / account_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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 :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 :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 "avatar" => avatar,
73 "nickname" => user.nickname,
74 "display_name" => display_name,
75 "deactivated" => user.deactivated,
76 "local" => user.local,
77 "roles" => User.roles(user),
78 "tags" => user.tags || [],
79 "confirmation_pending" => user.confirmation_pending,
80 "url" => user.uri || user.ap_id
81 }
82 end
83
84 def render("created.json", %{user: user}) do
85 %{
86 type: "success",
87 code: 200,
88 data: %{
89 nickname: user.nickname,
90 email: user.email
91 }
92 }
93 end
94
95 def render("create-error.json", %{changeset: %Ecto.Changeset{changes: changes, errors: errors}}) do
96 %{
97 type: "error",
98 code: 409,
99 error: parse_error(errors),
100 data: %{
101 nickname: Map.get(changes, :nickname),
102 email: Map.get(changes, :email)
103 }
104 }
105 end
106
107 def merge_account_views(%User{} = user) do
108 MastodonAPI.AccountView.render("show.json", %{user: user, skip_visibility_check: true})
109 |> Map.merge(AdminAPI.AccountView.render("show.json", %{user: user}))
110 end
111
112 def merge_account_views(_), do: %{}
113
114 defp parse_error([]), do: ""
115
116 defp parse_error(errors) do
117 ## when nickname is duplicate ap_id constraint error is raised
118 nickname_error = Keyword.get(errors, :nickname) || Keyword.get(errors, :ap_id)
119 email_error = Keyword.get(errors, :email)
120 password_error = Keyword.get(errors, :password)
121
122 cond do
123 nickname_error ->
124 "nickname #{elem(nickname_error, 0)}"
125
126 email_error ->
127 "email #{elem(email_error, 0)}"
128
129 password_error ->
130 "password #{elem(password_error, 0)}"
131
132 true ->
133 ""
134 end
135 end
136
137 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
138 defp image_url(_), do: nil
139 end