account_view.ex 4.13 KB
Newer Older
1
# Pleroma: A lightweight social networking server
Haelwenn's avatar
Haelwenn committed
2
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3
4
# SPDX-License-Identifier: AGPL-3.0-only

5
defmodule Pleroma.Web.AdminAPI.AccountView do
6
7
  use Pleroma.Web, :view

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
15
16
17
18
19
20
21

  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

22
23
24
25
26
27
  def render("index.json", %{users: users}) do
    %{
      users: render_many(users, AccountView, "show.json", as: :user)
    }
  end

28
29
30
31
32
33
34
35
36
37
38
39
40
41
  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,
42
      :is_locked,
43
44
45
46
47
48
49
50
51
52
53
54
      :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,
55
      :is_discoverable,
56
57
58
59
60
61
62
63
64
      :actor_type
    ])
    |> Map.merge(%{
      "avatar" => avatar,
      "banner" => banner,
      "background" => background
    })
  end

65
  def render("show.json", %{user: user}) do
66
    avatar = User.avatar_url(user) |> MediaProxy.url()
Haelwenn's avatar
Haelwenn committed
67
68
    display_name = Pleroma.HTML.strip_tags(user.name || user.nickname)
    user = User.sanitize_html(user, FastSanitize.Sanitizer.StripTags)
69

70
71
    %{
      "id" => user.id,
Alex Gleason's avatar
Alex Gleason committed
72
      "email" => user.email,
73
      "avatar" => avatar,
74
      "nickname" => user.nickname,
75
      "display_name" => display_name,
76
      "is_active" => user.is_active,
77
      "local" => user.local,
78
      "roles" => roles(user),
79
      "tags" => user.tags || [],
80
      "is_confirmed" => user.is_confirmed,
81
      "is_approved" => user.is_approved,
82
      "url" => user.uri || user.ap_id,
83
84
      "registration_reason" => user.registration_reason,
      "actor_type" => user.actor_type
85
86
    }
  end
87

88
89
90
91
  def render("created_many.json", %{users: users}) do
    render_many(users, AccountView, "created.json", as: :user)
  end

92
93
94
  def render("created.json", %{user: user}) do
    %{
      type: "success",
95
      code: 200,
96
97
98
99
100
101
102
      data: %{
        nickname: user.nickname,
        email: user.email
      }
    }
  end

103
104
105
106
107
  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
108
109
110
111
112
113
114
115
116
117
118
    %{
      type: "error",
      code: 409,
      error: parse_error(errors),
      data: %{
        nickname: Map.get(changes, :nickname),
        email: Map.get(changes, :email)
      }
    }
  end

119
  def merge_account_views(%User{} = user) do
120
    MastodonAPI.AccountView.render("show.json", %{user: user, skip_visibility_check: true})
121
122
123
124
125
    |> Map.merge(AdminAPI.AccountView.render("show.json", %{user: user}))
  end

  def merge_account_views(_), do: %{}

126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
  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
148
149
150

  defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
  defp image_url(_), do: nil
151
152
153
154
155
156
157

  defp roles(%{is_moderator: is_moderator, is_admin: is_admin}) do
    %{
      admin: is_admin,
      moderator: is_moderator
    }
  end
158
end