Remove User.user_info/2
[akkoma] / lib / pleroma / web / mastodon_api / views / account_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.AccountView do
6 use Pleroma.Web, :view
7
8 alias Pleroma.HTML
9 alias Pleroma.User
10 alias Pleroma.Web.CommonAPI.Utils
11 alias Pleroma.Web.MastodonAPI.AccountView
12 alias Pleroma.Web.MediaProxy
13
14 def render("index.json", %{users: users} = opts) do
15 users
16 |> render_many(AccountView, "show.json", opts)
17 |> Enum.filter(&Enum.any?/1)
18 end
19
20 def render("show.json", %{user: user} = opts) do
21 if User.visible_for?(user, opts[:for]),
22 do: do_render("show.json", opts),
23 else: %{}
24 end
25
26 def render("mention.json", %{user: user}) do
27 %{
28 id: to_string(user.id),
29 acct: user.nickname,
30 username: username_from_nickname(user.nickname),
31 url: User.profile_url(user)
32 }
33 end
34
35 def render("relationship.json", %{user: nil, target: _target}) do
36 %{}
37 end
38
39 def render("relationship.json", %{user: %User{} = user, target: %User{} = target}) do
40 follow_state = User.get_cached_follow_state(user, target)
41
42 requested =
43 if follow_state && !User.following?(user, target) do
44 follow_state == "pending"
45 else
46 false
47 end
48
49 %{
50 id: to_string(target.id),
51 following: User.following?(user, target),
52 followed_by: User.following?(target, user),
53 blocking: User.blocks_ap_id?(user, target),
54 blocked_by: User.blocks_ap_id?(target, user),
55 muting: User.mutes?(user, target),
56 muting_notifications: User.muted_notifications?(user, target),
57 subscribing: User.subscribed_to?(user, target),
58 requested: requested,
59 domain_blocking: User.blocks_domain?(user, target),
60 showing_reblogs: User.showing_reblogs?(user, target),
61 endorsed: false
62 }
63 end
64
65 def render("relationships.json", %{user: user, targets: targets}) do
66 render_many(targets, AccountView, "relationship.json", user: user, as: :target)
67 end
68
69 defp do_render("show.json", %{user: user} = opts) do
70 display_name = HTML.strip_tags(user.name || user.nickname)
71
72 image = User.avatar_url(user) |> MediaProxy.url()
73 header = User.banner_url(user) |> MediaProxy.url()
74
75 following_count =
76 if !user.hide_follows_count or !user.hide_follows or opts[:for] == user do
77 user.following_count
78 else
79 0
80 end
81
82 followers_count =
83 if !user.hide_followers_count or !user.hide_followers or opts[:for] == user do
84 user.follower_count
85 else
86 0
87 end
88
89 bot = (user.source_data["type"] || "Person") in ["Application", "Service"]
90
91 emojis =
92 (user.source_data["tag"] || [])
93 |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
94 |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
95 %{
96 "shortcode" => String.trim(name, ":"),
97 "url" => MediaProxy.url(url),
98 "static_url" => MediaProxy.url(url),
99 "visible_in_picker" => false
100 }
101 end)
102
103 fields =
104 user
105 |> User.fields()
106 |> Enum.map(fn %{"name" => name, "value" => value} ->
107 %{
108 "name" => Pleroma.HTML.strip_tags(name),
109 "value" => Pleroma.HTML.filter_tags(value, Pleroma.HTML.Scrubber.LinksOnly)
110 }
111 end)
112
113 bio = HTML.filter_tags(user.bio, User.html_filter_policy(opts[:for]))
114 relationship = render("relationship.json", %{user: opts[:for], target: user})
115
116 %{
117 id: to_string(user.id),
118 username: username_from_nickname(user.nickname),
119 acct: user.nickname,
120 display_name: display_name,
121 locked: user.locked,
122 created_at: Utils.to_masto_date(user.inserted_at),
123 followers_count: followers_count,
124 following_count: following_count,
125 statuses_count: user.note_count,
126 note: bio || "",
127 url: User.profile_url(user),
128 avatar: image,
129 avatar_static: image,
130 header: header,
131 header_static: header,
132 emojis: emojis,
133 fields: fields,
134 bot: bot,
135 source: %{
136 note: HTML.strip_tags((user.bio || "") |> String.replace("<br>", "\n")),
137 sensitive: false,
138 fields: user.raw_fields,
139 pleroma: %{
140 discoverable: user.discoverable
141 }
142 },
143
144 # Pleroma extension
145 pleroma: %{
146 confirmation_pending: user.confirmation_pending,
147 tags: user.tags,
148 hide_followers_count: user.hide_followers_count,
149 hide_follows_count: user.hide_follows_count,
150 hide_followers: user.hide_followers,
151 hide_follows: user.hide_follows,
152 hide_favorites: user.hide_favorites,
153 relationship: relationship,
154 skip_thread_containment: user.skip_thread_containment,
155 background_image: image_url(user.background) |> MediaProxy.url()
156 }
157 }
158 |> maybe_put_role(user, opts[:for])
159 |> maybe_put_settings(user, opts[:for], opts)
160 |> maybe_put_notification_settings(user, opts[:for])
161 |> maybe_put_settings_store(user, opts[:for], opts)
162 |> maybe_put_chat_token(user, opts[:for], opts)
163 |> maybe_put_activation_status(user, opts[:for])
164 |> maybe_put_follow_requests_count(user, opts[:for])
165 |> maybe_put_unread_conversation_count(user, opts[:for])
166 end
167
168 defp username_from_nickname(string) when is_binary(string) do
169 hd(String.split(string, "@"))
170 end
171
172 defp username_from_nickname(_), do: nil
173
174 defp maybe_put_follow_requests_count(
175 data,
176 %User{id: user_id} = user,
177 %User{id: user_id}
178 ) do
179 count =
180 User.get_follow_requests(user)
181 |> length()
182
183 data
184 |> Kernel.put_in([:follow_requests_count], count)
185 end
186
187 defp maybe_put_follow_requests_count(data, _, _), do: data
188
189 defp maybe_put_settings(
190 data,
191 %User{id: user_id} = user,
192 %User{id: user_id},
193 _opts
194 ) do
195 data
196 |> Kernel.put_in([:source, :privacy], user.default_scope)
197 |> Kernel.put_in([:source, :pleroma, :show_role], user.show_role)
198 |> Kernel.put_in([:source, :pleroma, :no_rich_text], user.no_rich_text)
199 end
200
201 defp maybe_put_settings(data, _, _, _), do: data
202
203 defp maybe_put_settings_store(data, %User{} = user, %User{}, %{
204 with_pleroma_settings: true
205 }) do
206 data
207 |> Kernel.put_in([:pleroma, :settings_store], user.pleroma_settings_store)
208 end
209
210 defp maybe_put_settings_store(data, _, _, _), do: data
211
212 defp maybe_put_chat_token(data, %User{id: id}, %User{id: id}, %{
213 with_chat_token: token
214 }) do
215 data
216 |> Kernel.put_in([:pleroma, :chat_token], token)
217 end
218
219 defp maybe_put_chat_token(data, _, _, _), do: data
220
221 defp maybe_put_role(data, %User{show_role: true} = user, _) do
222 data
223 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
224 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
225 end
226
227 defp maybe_put_role(data, %User{id: user_id} = user, %User{id: user_id}) do
228 data
229 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
230 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
231 end
232
233 defp maybe_put_role(data, _, _), do: data
234
235 defp maybe_put_notification_settings(data, %User{id: user_id} = user, %User{id: user_id}) do
236 Kernel.put_in(data, [:pleroma, :notification_settings], user.notification_settings)
237 end
238
239 defp maybe_put_notification_settings(data, _, _), do: data
240
241 defp maybe_put_activation_status(data, user, %User{is_admin: true}) do
242 Kernel.put_in(data, [:pleroma, :deactivated], user.deactivated)
243 end
244
245 defp maybe_put_activation_status(data, _, _), do: data
246
247 defp maybe_put_unread_conversation_count(data, %User{id: user_id} = user, %User{id: user_id}) do
248 data
249 |> Kernel.put_in(
250 [:pleroma, :unread_conversation_count],
251 user.unread_conversation_count
252 )
253 end
254
255 defp maybe_put_unread_conversation_count(data, _, _), do: data
256
257 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
258 defp image_url(_), do: nil
259 end