1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.AdminAPI.AccountView do
9 alias Pleroma.Web.AdminAPI
10 alias Pleroma.Web.AdminAPI.AccountView
11 alias Pleroma.Web.MastodonAPI
12 alias Pleroma.Web.MediaProxy
14 def render("index.json", %{users: users, count: count, page_size: page_size}) do
16 users: render_many(users, AccountView, "show.json", as: :user),
22 def render("index.json", %{users: users}) do
24 users: render_many(users, AccountView, "show.json", as: :user)
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()
46 :hide_followers_count,
50 :allow_following_move,
52 :skip_thread_containment,
53 :pleroma_settings_store,
61 "background" => background
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)
72 "email" => user.email,
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 "confirmation_pending" => user.confirmation_pending,
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
88 def render("created.json", %{user: user}) do
93 nickname: user.nickname,
99 def render("create-error.json", %{changeset: %Ecto.Changeset{changes: changes, errors: errors}}) do
103 error: parse_error(errors),
105 nickname: Map.get(changes, :nickname),
106 email: Map.get(changes, :email)
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}))
116 def merge_account_views(_), do: %{}
118 defp parse_error([]), do: ""
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)
128 "nickname #{elem(nickname_error, 0)}"
131 "email #{elem(email_error, 0)}"
134 "password #{elem(password_error, 0)}"
141 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
142 defp image_url(_), do: nil