1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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.CommonAPI
12 alias Pleroma.Web.MastodonAPI
13 alias Pleroma.Web.MediaProxy
15 def render("index.json", %{users: users, count: count, page_size: page_size}) do
17 users: render_many(users, AccountView, "show.json", as: :user),
23 def render("index.json", %{users: users}) do
25 users: render_many(users, AccountView, "show.json", as: :user)
29 def render("credentials.json", %{user: user, for: for_user}) do
30 user = User.sanitize_html(user, User.html_filter_policy(for_user))
31 avatar = User.avatar_url(user) |> MediaProxy.url()
32 banner = User.banner_url(user) |> MediaProxy.url()
33 background = image_url(user.background) |> MediaProxy.url()
47 :hide_followers_count,
51 :allow_following_move,
53 :skip_thread_containment,
54 :pleroma_settings_store,
62 "background" => background
66 def render("show.json", %{user: user}) do
67 avatar = User.avatar_url(user) |> MediaProxy.url()
68 display_name = Pleroma.HTML.strip_tags(user.name || user.nickname)
69 user = User.sanitize_html(user, FastSanitize.Sanitizer.StripTags)
73 "email" => user.email,
75 "nickname" => user.nickname,
76 "display_name" => display_name,
77 "is_active" => user.is_active,
78 "local" => user.local,
79 "roles" => roles(user),
80 "tags" => user.tags || [],
81 "is_confirmed" => user.is_confirmed,
82 "is_approved" => user.is_approved,
83 "is_suggested" => user.is_suggested,
84 "url" => user.uri || user.ap_id,
85 "registration_reason" => user.registration_reason,
86 "actor_type" => user.actor_type,
87 "created_at" => CommonAPI.Utils.to_masto_date(user.inserted_at)
91 def render("created_many.json", %{users: users}) do
92 render_many(users, AccountView, "created.json", as: :user)
95 def render("created.json", %{user: user}) do
100 nickname: user.nickname,
106 def render("create_errors.json", %{changesets: changesets}) do
107 render_many(changesets, AccountView, "create_error.json", as: :changeset)
110 def render("create_error.json", %{changeset: %Ecto.Changeset{changes: changes, errors: errors}}) do
114 error: parse_error(errors),
116 nickname: Map.get(changes, :nickname),
117 email: Map.get(changes, :email)
122 def merge_account_views(%User{} = user) do
123 MastodonAPI.AccountView.render("show.json", %{user: user, skip_visibility_check: true})
124 |> Map.merge(AdminAPI.AccountView.render("show.json", %{user: user}))
127 def merge_account_views(_), do: %{}
129 defp parse_error([]), do: ""
131 defp parse_error(errors) do
132 ## when nickname is duplicate ap_id constraint error is raised
133 nickname_error = Keyword.get(errors, :nickname) || Keyword.get(errors, :ap_id)
134 email_error = Keyword.get(errors, :email)
135 password_error = Keyword.get(errors, :password)
139 "nickname #{elem(nickname_error, 0)}"
142 "email #{elem(email_error, 0)}"
145 "password #{elem(password_error, 0)}"
152 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
153 defp image_url(_), do: nil
155 defp roles(%{is_moderator: is_moderator, is_admin: is_admin}) do
158 moderator: is_moderator