Merge branch 'update/oban' into 'develop'
[akkoma] / lib / pleroma / web / mastodon_api / views / account_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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.FollowingRelationship
9 alias Pleroma.User
10 alias Pleroma.UserRelationship
11 alias Pleroma.Web.CommonAPI.Utils
12 alias Pleroma.Web.MastodonAPI.AccountView
13 alias Pleroma.Web.MediaProxy
14
15 def render("index.json", %{users: users} = opts) do
16 reading_user = opts[:for]
17
18 relationships_opt =
19 cond do
20 Map.has_key?(opts, :relationships) ->
21 opts[:relationships]
22
23 is_nil(reading_user) || !opts[:embed_relationships] ->
24 UserRelationship.view_relationships_option(nil, [])
25
26 true ->
27 UserRelationship.view_relationships_option(reading_user, users)
28 end
29
30 opts =
31 opts
32 |> Map.merge(%{relationships: relationships_opt, as: :user})
33 |> Map.delete(:users)
34
35 users
36 |> render_many(AccountView, "show.json", opts)
37 |> Enum.filter(&Enum.any?/1)
38 end
39
40 @doc """
41 Renders specified user account.
42 :skip_visibility_check option skips visibility check and renders any user (local or remote)
43 regardless of [:pleroma, :restrict_unauthenticated] setting.
44 :for option specifies the requester and can be a User record or nil.
45 Only use `user: user, for: user` when `user` is the actual requester of own profile.
46 """
47 def render("show.json", %{user: _user, skip_visibility_check: true} = opts) do
48 do_render("show.json", opts)
49 end
50
51 def render("show.json", %{user: user, for: for_user_or_nil} = opts) do
52 if User.visible_for(user, for_user_or_nil) == :visible do
53 do_render("show.json", opts)
54 else
55 %{}
56 end
57 end
58
59 def render("show.json", _) do
60 raise "In order to prevent account accessibility issues, " <>
61 ":skip_visibility_check or :for option is required."
62 end
63
64 def render("mention.json", %{user: user}) do
65 %{
66 id: to_string(user.id),
67 acct: user.nickname,
68 username: username_from_nickname(user.nickname),
69 url: user.uri || user.ap_id
70 }
71 end
72
73 def render("relationship.json", %{user: nil, target: _target}) do
74 %{}
75 end
76
77 def render(
78 "relationship.json",
79 %{user: %User{} = reading_user, target: %User{} = target} = opts
80 ) do
81 user_relationships = get_in(opts, [:relationships, :user_relationships])
82 following_relationships = get_in(opts, [:relationships, :following_relationships])
83
84 follow_state =
85 if following_relationships do
86 user_to_target_following_relation =
87 FollowingRelationship.find(following_relationships, reading_user, target)
88
89 User.get_follow_state(reading_user, target, user_to_target_following_relation)
90 else
91 User.get_follow_state(reading_user, target)
92 end
93
94 followed_by =
95 if following_relationships do
96 case FollowingRelationship.find(following_relationships, target, reading_user) do
97 %{state: :follow_accept} -> true
98 _ -> false
99 end
100 else
101 User.following?(target, reading_user)
102 end
103
104 # NOTE: adjust UserRelationship.view_relationships_option/2 on new relation-related flags
105 %{
106 id: to_string(target.id),
107 following: follow_state == :follow_accept,
108 followed_by: followed_by,
109 blocking:
110 UserRelationship.exists?(
111 user_relationships,
112 :block,
113 reading_user,
114 target,
115 &User.blocks_user?(&1, &2)
116 ),
117 blocked_by:
118 UserRelationship.exists?(
119 user_relationships,
120 :block,
121 target,
122 reading_user,
123 &User.blocks_user?(&1, &2)
124 ),
125 muting:
126 UserRelationship.exists?(
127 user_relationships,
128 :mute,
129 reading_user,
130 target,
131 &User.mutes?(&1, &2)
132 ),
133 muting_notifications:
134 UserRelationship.exists?(
135 user_relationships,
136 :notification_mute,
137 reading_user,
138 target,
139 &User.muted_notifications?(&1, &2)
140 ),
141 subscribing:
142 UserRelationship.exists?(
143 user_relationships,
144 :inverse_subscription,
145 target,
146 reading_user,
147 &User.subscribed_to?(&2, &1)
148 ),
149 requested: follow_state == :follow_pending,
150 domain_blocking: User.blocks_domain?(reading_user, target),
151 showing_reblogs:
152 not UserRelationship.exists?(
153 user_relationships,
154 :reblog_mute,
155 reading_user,
156 target,
157 &User.muting_reblogs?(&1, &2)
158 ),
159 endorsed: false
160 }
161 end
162
163 def render("relationships.json", %{user: user, targets: targets} = opts) do
164 relationships_opt =
165 cond do
166 Map.has_key?(opts, :relationships) ->
167 opts[:relationships]
168
169 is_nil(user) ->
170 UserRelationship.view_relationships_option(nil, [])
171
172 true ->
173 UserRelationship.view_relationships_option(user, targets)
174 end
175
176 render_opts = %{as: :target, user: user, relationships: relationships_opt}
177 render_many(targets, AccountView, "relationship.json", render_opts)
178 end
179
180 defp do_render("show.json", %{user: user} = opts) do
181 user = User.sanitize_html(user, User.html_filter_policy(opts[:for]))
182 display_name = user.name || user.nickname
183
184 avatar = User.avatar_url(user) |> MediaProxy.url()
185 avatar_static = User.avatar_url(user) |> MediaProxy.preview_url(static: true)
186 header = User.banner_url(user) |> MediaProxy.url()
187 header_static = User.banner_url(user) |> MediaProxy.preview_url(static: true)
188
189 following_count =
190 if !user.hide_follows_count or !user.hide_follows or opts[:for] == user,
191 do: user.following_count,
192 else: 0
193
194 followers_count =
195 if !user.hide_followers_count or !user.hide_followers or opts[:for] == user,
196 do: user.follower_count,
197 else: 0
198
199 bot = user.actor_type == "Service"
200
201 emojis =
202 Enum.map(user.emoji, fn {shortcode, raw_url} ->
203 url = MediaProxy.url(raw_url)
204
205 %{
206 shortcode: shortcode,
207 url: url,
208 static_url: url,
209 visible_in_picker: false
210 }
211 end)
212
213 relationship =
214 if opts[:embed_relationships] do
215 render("relationship.json", %{
216 user: opts[:for],
217 target: user,
218 relationships: opts[:relationships]
219 })
220 else
221 %{}
222 end
223
224 favicon =
225 if Pleroma.Config.get([:instances_favicons, :enabled]) do
226 user
227 |> Map.get(:ap_id, "")
228 |> URI.parse()
229 |> URI.merge("/")
230 |> Pleroma.Instances.Instance.get_or_update_favicon()
231 |> MediaProxy.url()
232 else
233 nil
234 end
235
236 %{
237 id: to_string(user.id),
238 username: username_from_nickname(user.nickname),
239 acct: user.nickname,
240 display_name: display_name,
241 locked: user.is_locked,
242 created_at: Utils.to_masto_date(user.inserted_at),
243 followers_count: followers_count,
244 following_count: following_count,
245 statuses_count: user.note_count,
246 note: user.bio,
247 url: user.uri || user.ap_id,
248 avatar: avatar,
249 avatar_static: avatar_static,
250 header: header,
251 header_static: header_static,
252 emojis: emojis,
253 fields: user.fields,
254 bot: bot,
255 source: %{
256 note: user.raw_bio || "",
257 sensitive: false,
258 fields: user.raw_fields,
259 pleroma: %{
260 discoverable: user.is_discoverable,
261 actor_type: user.actor_type
262 }
263 },
264
265 # Pleroma extension
266 pleroma: %{
267 ap_id: user.ap_id,
268 also_known_as: user.also_known_as,
269 is_confirmed: user.is_confirmed,
270 tags: user.tags,
271 hide_followers_count: user.hide_followers_count,
272 hide_follows_count: user.hide_follows_count,
273 hide_followers: user.hide_followers,
274 hide_follows: user.hide_follows,
275 hide_favorites: user.hide_favorites,
276 relationship: relationship,
277 skip_thread_containment: user.skip_thread_containment,
278 background_image: image_url(user.background) |> MediaProxy.url(),
279 accepts_chat_messages: user.accepts_chat_messages,
280 favicon: favicon
281 }
282 }
283 |> maybe_put_role(user, opts[:for])
284 |> maybe_put_settings(user, opts[:for], opts)
285 |> maybe_put_notification_settings(user, opts[:for])
286 |> maybe_put_settings_store(user, opts[:for], opts)
287 |> maybe_put_chat_token(user, opts[:for], opts)
288 |> maybe_put_activation_status(user, opts[:for])
289 |> maybe_put_follow_requests_count(user, opts[:for])
290 |> maybe_put_allow_following_move(user, opts[:for])
291 |> maybe_put_unread_conversation_count(user, opts[:for])
292 |> maybe_put_unread_notification_count(user, opts[:for])
293 end
294
295 defp username_from_nickname(string) when is_binary(string) do
296 hd(String.split(string, "@"))
297 end
298
299 defp username_from_nickname(_), do: nil
300
301 defp maybe_put_follow_requests_count(
302 data,
303 %User{id: user_id} = user,
304 %User{id: user_id}
305 ) do
306 count =
307 User.get_follow_requests(user)
308 |> length()
309
310 data
311 |> Kernel.put_in([:follow_requests_count], count)
312 end
313
314 defp maybe_put_follow_requests_count(data, _, _), do: data
315
316 defp maybe_put_settings(
317 data,
318 %User{id: user_id} = user,
319 %User{id: user_id},
320 _opts
321 ) do
322 data
323 |> Kernel.put_in([:source, :privacy], user.default_scope)
324 |> Kernel.put_in([:source, :pleroma, :show_role], user.show_role)
325 |> Kernel.put_in([:source, :pleroma, :no_rich_text], user.no_rich_text)
326 end
327
328 defp maybe_put_settings(data, _, _, _), do: data
329
330 defp maybe_put_settings_store(data, %User{} = user, %User{}, %{
331 with_pleroma_settings: true
332 }) do
333 data
334 |> Kernel.put_in([:pleroma, :settings_store], user.pleroma_settings_store)
335 end
336
337 defp maybe_put_settings_store(data, _, _, _), do: data
338
339 defp maybe_put_chat_token(data, %User{id: id}, %User{id: id}, %{
340 with_chat_token: token
341 }) do
342 data
343 |> Kernel.put_in([:pleroma, :chat_token], token)
344 end
345
346 defp maybe_put_chat_token(data, _, _, _), do: data
347
348 defp maybe_put_role(data, %User{show_role: true} = user, _) do
349 data
350 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
351 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
352 end
353
354 defp maybe_put_role(data, %User{id: user_id} = user, %User{id: user_id}) do
355 data
356 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
357 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
358 end
359
360 defp maybe_put_role(data, _, _), do: data
361
362 defp maybe_put_notification_settings(data, %User{id: user_id} = user, %User{id: user_id}) do
363 Kernel.put_in(
364 data,
365 [:pleroma, :notification_settings],
366 Map.from_struct(user.notification_settings)
367 )
368 end
369
370 defp maybe_put_notification_settings(data, _, _), do: data
371
372 defp maybe_put_allow_following_move(data, %User{id: user_id} = user, %User{id: user_id}) do
373 Kernel.put_in(data, [:pleroma, :allow_following_move], user.allow_following_move)
374 end
375
376 defp maybe_put_allow_following_move(data, _, _), do: data
377
378 defp maybe_put_activation_status(data, user, %User{is_admin: true}) do
379 Kernel.put_in(data, [:pleroma, :deactivated], user.deactivated)
380 end
381
382 defp maybe_put_activation_status(data, _, _), do: data
383
384 defp maybe_put_unread_conversation_count(data, %User{id: user_id} = user, %User{id: user_id}) do
385 data
386 |> Kernel.put_in(
387 [:pleroma, :unread_conversation_count],
388 Pleroma.Conversation.Participation.unread_count(user)
389 )
390 end
391
392 defp maybe_put_unread_conversation_count(data, _, _), do: data
393
394 defp maybe_put_unread_notification_count(data, %User{id: user_id}, %User{id: user_id} = user) do
395 Kernel.put_in(
396 data,
397 [:pleroma, :unread_notifications_count],
398 Pleroma.Notification.unread_notifications_count(user)
399 )
400 end
401
402 defp maybe_put_unread_notification_count(data, _, _), do: data
403
404 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
405 defp image_url(_), do: nil
406 end