2019-02-28 15:43:09 +01:00
|
|
|
# Pleroma: A lightweight social networking server
|
2020-03-03 23:44:49 +01:00
|
|
|
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
|
2019-02-28 15:43:09 +01:00
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2019-03-13 04:13:28 +01:00
|
|
|
defmodule Pleroma.Web.AdminAPI.AccountView do
|
2019-02-28 15:43:09 +01:00
|
|
|
use Pleroma.Web, :view
|
|
|
|
|
2019-06-28 14:15:32 +02:00
|
|
|
alias Pleroma.User
|
2019-03-13 04:13:28 +01:00
|
|
|
alias Pleroma.Web.AdminAPI.AccountView
|
2019-06-28 14:15:32 +02:00
|
|
|
alias Pleroma.Web.MediaProxy
|
2019-02-28 15:43:09 +01: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 16:03:54 +02:00
|
|
|
def render("index.json", %{users: users}) do
|
|
|
|
%{
|
|
|
|
users: render_many(users, AccountView, "show.json", as: :user)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2020-01-31 19:07:46 +01: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,
|
|
|
|
:locked,
|
|
|
|
: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,
|
|
|
|
:discoverable,
|
|
|
|
:actor_type
|
|
|
|
])
|
|
|
|
|> Map.merge(%{
|
|
|
|
"avatar" => avatar,
|
|
|
|
"banner" => banner,
|
|
|
|
"background" => background
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2019-02-28 15:43:09 +01:00
|
|
|
def render("show.json", %{user: user}) do
|
2019-06-28 14:15:32 +02:00
|
|
|
avatar = User.avatar_url(user) |> MediaProxy.url()
|
2020-03-15 17:00:54 +01:00
|
|
|
display_name = Pleroma.HTML.strip_tags(user.name || user.nickname)
|
|
|
|
user = User.sanitize_html(user, FastSanitize.Sanitizer.StripTags)
|
2019-06-28 14:15:32 +02:00
|
|
|
|
2019-02-28 15:43:09 +01:00
|
|
|
%{
|
|
|
|
"id" => user.id,
|
2019-06-28 14:15:32 +02:00
|
|
|
"avatar" => avatar,
|
2019-02-28 15:43:09 +01:00
|
|
|
"nickname" => user.nickname,
|
2019-06-28 14:15:32 +02:00
|
|
|
"display_name" => display_name,
|
2019-10-16 20:59:21 +02:00
|
|
|
"deactivated" => user.deactivated,
|
2019-03-13 14:04:44 +01:00
|
|
|
"local" => user.local,
|
2019-10-16 20:59:21 +02:00
|
|
|
"roles" => User.roles(user),
|
2019-11-19 12:14:02 +01:00
|
|
|
"tags" => user.tags || [],
|
|
|
|
"confirmation_pending" => user.confirmation_pending
|
2019-02-28 15:43:09 +01:00
|
|
|
}
|
|
|
|
end
|
2019-04-06 15:25:19 +02:00
|
|
|
|
|
|
|
def render("invite.json", %{invite: invite}) do
|
|
|
|
%{
|
|
|
|
"id" => invite.id,
|
|
|
|
"token" => invite.token,
|
|
|
|
"used" => invite.used,
|
2019-04-08 11:01:28 +02:00
|
|
|
"expires_at" => invite.expires_at,
|
2019-04-06 15:25:19 +02:00
|
|
|
"uses" => invite.uses,
|
|
|
|
"max_use" => invite.max_use,
|
|
|
|
"invite_type" => invite.invite_type
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("invites.json", %{invites: invites}) do
|
|
|
|
%{
|
|
|
|
invites: render_many(invites, AccountView, "invite.json", as: :invite)
|
|
|
|
}
|
|
|
|
end
|
2019-05-17 08:35:31 +02:00
|
|
|
|
|
|
|
def render("created.json", %{user: user}) do
|
|
|
|
%{
|
|
|
|
type: "success",
|
2019-06-01 07:32:53 +02:00
|
|
|
code: 200,
|
2019-05-17 08:35:31 +02:00
|
|
|
data: %{
|
|
|
|
nickname: user.nickname,
|
|
|
|
email: user.email
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def render("create-error.json", %{changeset: %Ecto.Changeset{changes: changes, errors: errors}}) do
|
|
|
|
%{
|
|
|
|
type: "error",
|
|
|
|
code: 409,
|
|
|
|
error: parse_error(errors),
|
|
|
|
data: %{
|
|
|
|
nickname: Map.get(changes, :nickname),
|
|
|
|
email: Map.get(changes, :email)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
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 19:07:46 +01:00
|
|
|
|
|
|
|
defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
|
|
|
|
defp image_url(_), do: nil
|
2019-02-28 15:43:09 +01:00
|
|
|
end
|