2019-02-28 14:43:09 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
2021-01-13 06:49:20 +00:00
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
2019-02-28 14:43:09 +00:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-03-13 03:13:28 +00:00
|
|
|
defmodule Pleroma.Web.AdminAPI.AccountView do
|
2019-02-28 14:43:09 +00:00
|
|
|
use Pleroma.Web, :view
|
|
|
|
|
2019-06-28 12:15:32 +00:00
|
|
|
alias Pleroma.User
|
2020-05-09 15:05:44 +00:00
|
|
|
alias Pleroma.Web.AdminAPI
|
2019-03-13 03:13:28 +00:00
|
|
|
alias Pleroma.Web.AdminAPI.AccountView
|
2021-07-14 04:51:32 +00:00
|
|
|
alias Pleroma.Web.CommonAPI
|
2020-05-09 15:05:44 +00:00
|
|
|
alias Pleroma.Web.MastodonAPI
|
2019-06-28 12:15:32 +00:00
|
|
|
alias Pleroma.Web.MediaProxy
|
2019-02-28 14:43:09 +00:00
|
|
|
|
|
|
|
def render("index.json", %{users: users, count: count, page_size: page_size}) do
|
|
|
|
%{
|
|
|
|
users: render_many(users, AccountView, "show.json", as: :user),
|
|
|
|
count: count,
|
|
|
|
page_size: page_size
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2019-10-09 14:03:54 +00:00
|
|
|
def render("index.json", %{users: users}) do
|
|
|
|
%{
|
|
|
|
users: render_many(users, AccountView, "show.json", as: :user)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-01-31 18:07:46 +00:00
|
|
|
def render("credentials.json", %{user: user, for: for_user}) do
|
|
|
|
user = User.sanitize_html(user, User.html_filter_policy(for_user))
|
|
|
|
avatar = User.avatar_url(user) |> MediaProxy.url()
|
|
|
|
banner = User.banner_url(user) |> MediaProxy.url()
|
|
|
|
background = image_url(user.background) |> MediaProxy.url()
|
|
|
|
|
|
|
|
user
|
|
|
|
|> Map.take([
|
|
|
|
:id,
|
|
|
|
:bio,
|
|
|
|
:email,
|
|
|
|
:fields,
|
|
|
|
:name,
|
|
|
|
:nickname,
|
2020-10-13 14:31:13 +00:00
|
|
|
:is_locked,
|
2020-01-31 18:07:46 +00:00
|
|
|
:no_rich_text,
|
|
|
|
:default_scope,
|
|
|
|
:hide_follows,
|
|
|
|
:hide_followers_count,
|
|
|
|
:hide_follows_count,
|
|
|
|
:hide_followers,
|
|
|
|
:hide_favorites,
|
|
|
|
:allow_following_move,
|
|
|
|
:show_role,
|
|
|
|
:skip_thread_containment,
|
|
|
|
:pleroma_settings_store,
|
|
|
|
:raw_fields,
|
2020-10-28 19:29:52 +00:00
|
|
|
:is_discoverable,
|
2020-01-31 18:07:46 +00:00
|
|
|
:actor_type
|
|
|
|
])
|
|
|
|
|> Map.merge(%{
|
|
|
|
"avatar" => avatar,
|
|
|
|
"banner" => banner,
|
|
|
|
"background" => background
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2019-02-28 14:43:09 +00:00
|
|
|
def render("show.json", %{user: user}) do
|
2019-06-28 12:15:32 +00:00
|
|
|
avatar = User.avatar_url(user) |> MediaProxy.url()
|
2020-03-15 16:00:54 +00:00
|
|
|
display_name = Pleroma.HTML.strip_tags(user.name || user.nickname)
|
|
|
|
user = User.sanitize_html(user, FastSanitize.Sanitizer.StripTags)
|
2019-06-28 12:15:32 +00:00
|
|
|
|
2019-02-28 14:43:09 +00:00
|
|
|
%{
|
|
|
|
"id" => user.id,
|
2021-01-06 17:30:32 +00:00
|
|
|
"email" => user.email,
|
2019-06-28 12:15:32 +00:00
|
|
|
"avatar" => avatar,
|
2019-02-28 14:43:09 +00:00
|
|
|
"nickname" => user.nickname,
|
2019-06-28 12:15:32 +00:00
|
|
|
"display_name" => display_name,
|
2020-10-12 22:42:27 +00:00
|
|
|
"is_active" => user.is_active,
|
2019-03-13 13:04:44 +00:00
|
|
|
"local" => user.local,
|
2021-03-02 16:49:17 +00:00
|
|
|
"roles" => roles(user),
|
2019-11-19 11:14:02 +00:00
|
|
|
"tags" => user.tags || [],
|
2020-10-13 19:29:34 +00:00
|
|
|
"is_confirmed" => user.is_confirmed,
|
2020-10-16 21:44:25 +00:00
|
|
|
"is_approved" => user.is_approved,
|
2021-11-27 05:10:01 +00:00
|
|
|
"is_suggested" => user.is_suggested,
|
2020-07-14 18:04:57 +00:00
|
|
|
"url" => user.uri || user.ap_id,
|
2020-08-21 07:38:56 +00:00
|
|
|
"registration_reason" => user.registration_reason,
|
2021-07-14 04:51:32 +00:00
|
|
|
"actor_type" => user.actor_type,
|
|
|
|
"created_at" => CommonAPI.Utils.to_masto_date(user.inserted_at)
|
2019-02-28 14:43:09 +00:00
|
|
|
}
|
|
|
|
end
|
2019-04-06 13:25:19 +00:00
|
|
|
|
2021-03-02 16:49:17 +00:00
|
|
|
def render("created_many.json", %{users: users}) do
|
|
|
|
render_many(users, AccountView, "created.json", as: :user)
|
|
|
|
end
|
|
|
|
|
2019-05-17 06:35:31 +00:00
|
|
|
def render("created.json", %{user: user}) do
|
|
|
|
%{
|
|
|
|
type: "success",
|
2019-06-01 05:32:53 +00:00
|
|
|
code: 200,
|
2019-05-17 06:35:31 +00:00
|
|
|
data: %{
|
|
|
|
nickname: user.nickname,
|
|
|
|
email: user.email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2021-03-02 16:49:17 +00:00
|
|
|
def render("create_errors.json", %{changesets: changesets}) do
|
|
|
|
render_many(changesets, AccountView, "create_error.json", as: :changeset)
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("create_error.json", %{changeset: %Ecto.Changeset{changes: changes, errors: errors}}) do
|
2019-05-17 06:35:31 +00:00
|
|
|
%{
|
|
|
|
type: "error",
|
|
|
|
code: 409,
|
|
|
|
error: parse_error(errors),
|
|
|
|
data: %{
|
|
|
|
nickname: Map.get(changes, :nickname),
|
|
|
|
email: Map.get(changes, :email)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-05-09 15:05:44 +00:00
|
|
|
def merge_account_views(%User{} = user) do
|
2020-07-23 12:08:30 +00:00
|
|
|
MastodonAPI.AccountView.render("show.json", %{user: user, skip_visibility_check: true})
|
2020-05-09 15:05:44 +00:00
|
|
|
|> Map.merge(AdminAPI.AccountView.render("show.json", %{user: user}))
|
|
|
|
end
|
|
|
|
|
|
|
|
def merge_account_views(_), do: %{}
|
|
|
|
|
2019-05-17 06:35:31 +00:00
|
|
|
defp parse_error([]), do: ""
|
|
|
|
|
|
|
|
defp parse_error(errors) do
|
|
|
|
## when nickname is duplicate ap_id constraint error is raised
|
|
|
|
nickname_error = Keyword.get(errors, :nickname) || Keyword.get(errors, :ap_id)
|
|
|
|
email_error = Keyword.get(errors, :email)
|
|
|
|
password_error = Keyword.get(errors, :password)
|
|
|
|
|
|
|
|
cond do
|
|
|
|
nickname_error ->
|
|
|
|
"nickname #{elem(nickname_error, 0)}"
|
|
|
|
|
|
|
|
email_error ->
|
|
|
|
"email #{elem(email_error, 0)}"
|
|
|
|
|
|
|
|
password_error ->
|
|
|
|
"password #{elem(password_error, 0)}"
|
|
|
|
|
|
|
|
true ->
|
|
|
|
""
|
|
|
|
end
|
|
|
|
end
|
2020-01-31 18:07:46 +00:00
|
|
|
|
|
|
|
defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
|
|
|
|
defp image_url(_), do: nil
|
2021-03-02 16:49:17 +00:00
|
|
|
|
|
|
|
defp roles(%{is_moderator: is_moderator, is_admin: is_admin}) do
|
|
|
|
%{
|
|
|
|
admin: is_admin,
|
|
|
|
moderator: is_moderator
|
|
|
|
}
|
|
|
|
end
|
2019-02-28 14:43:09 +00:00
|
|
|
end
|