1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.AccountView do
8 alias Pleroma.FollowingRelationship
10 alias Pleroma.UserNote
11 alias Pleroma.UserRelationship
12 alias Pleroma.Web.CommonAPI.Utils
13 alias Pleroma.Web.MastodonAPI.AccountView
14 alias Pleroma.Web.MediaProxy
16 def render("index.json", %{users: users} = opts) do
17 reading_user = opts[:for]
21 Map.has_key?(opts, :relationships) ->
24 is_nil(reading_user) || !opts[:embed_relationships] ->
25 UserRelationship.view_relationships_option(nil, [])
28 UserRelationship.view_relationships_option(reading_user, users)
33 |> Map.merge(%{relationships: relationships_opt, as: :user})
37 |> render_many(AccountView, "show.json", opts)
38 |> Enum.filter(&Enum.any?/1)
42 Renders specified user account.
43 :skip_visibility_check option skips visibility check and renders any user (local or remote)
44 regardless of [:pleroma, :restrict_unauthenticated] setting.
45 :for option specifies the requester and can be a User record or nil.
46 Only use `user: user, for: user` when `user` is the actual requester of own profile.
48 def render("show.json", %{user: _user, skip_visibility_check: true} = opts) do
49 do_render("show.json", opts)
52 def render("show.json", %{user: user, for: for_user_or_nil} = opts) do
53 if User.visible_for(user, for_user_or_nil) == :visible do
54 do_render("show.json", opts)
60 def render("show.json", _) do
61 raise "In order to prevent account accessibility issues, " <>
62 ":skip_visibility_check or :for option is required."
65 def render("mention.json", %{user: user}) do
67 id: to_string(user.id),
69 username: username_from_nickname(user.nickname),
70 url: user.uri || user.ap_id
74 def render("relationship.json", %{user: nil, target: _target}) do
80 %{user: %User{} = reading_user, target: %User{} = target} = opts
82 user_relationships = get_in(opts, [:relationships, :user_relationships])
83 following_relationships = get_in(opts, [:relationships, :following_relationships])
86 if following_relationships do
87 user_to_target_following_relation =
88 FollowingRelationship.find(following_relationships, reading_user, target)
90 User.get_follow_state(reading_user, target, user_to_target_following_relation)
92 User.get_follow_state(reading_user, target)
96 if following_relationships do
97 target_to_user_following_relation =
98 FollowingRelationship.find(following_relationships, target, reading_user)
100 User.get_follow_state(target, reading_user, target_to_user_following_relation)
102 User.get_follow_state(target, reading_user)
106 UserRelationship.exists?(
108 :inverse_subscription,
111 &User.subscribed_to?(&2, &1)
114 # NOTE: adjust UserRelationship.view_relationships_option/2 on new relation-related flags
116 id: to_string(target.id),
117 following: follow_state == :follow_accept,
118 followed_by: followed_by == :follow_accept,
120 UserRelationship.exists?(
125 &User.blocks_user?(&1, &2)
128 UserRelationship.exists?(
133 &User.blocks_user?(&1, &2)
136 UserRelationship.exists?(
143 muting_notifications:
144 UserRelationship.exists?(
149 &User.muted_notifications?(&1, &2)
151 subscribing: subscribing,
152 notifying: subscribing,
153 requested: follow_state == :follow_pending,
154 requested_by: followed_by == :follow_pending,
155 domain_blocking: User.blocks_domain?(reading_user, target),
157 not UserRelationship.exists?(
162 &User.muting_reblogs?(&1, &2)
173 def render("relationships.json", %{user: user, targets: targets} = opts) do
176 Map.has_key?(opts, :relationships) ->
180 UserRelationship.view_relationships_option(nil, [])
183 UserRelationship.view_relationships_option(user, targets)
186 render_opts = %{as: :target, user: user, relationships: relationships_opt}
187 render_many(targets, AccountView, "relationship.json", render_opts)
190 def render("instance.json", %{instance: %Pleroma.Instances.Instance{} = instance}) do
193 favicon: instance.favicon |> MediaProxy.url(),
194 nodeinfo: instance.nodeinfo
198 def render("instance.json", _), do: nil
200 defp do_render("show.json", %{user: user} = opts) do
201 user = User.sanitize_html(user, User.html_filter_policy(opts[:for]))
202 display_name = user.name || user.nickname
204 avatar = User.avatar_url(user) |> MediaProxy.url()
205 avatar_static = User.avatar_url(user) |> MediaProxy.preview_url(static: true)
206 header = User.banner_url(user) |> MediaProxy.url()
207 header_static = User.banner_url(user) |> MediaProxy.preview_url(static: true)
210 if !user.hide_follows_count or !user.hide_follows or opts[:for] == user,
211 do: user.following_count,
215 if !user.hide_followers_count or !user.hide_followers or opts[:for] == user,
216 do: user.follower_count,
219 bot = user.actor_type == "Service"
222 Enum.map(user.emoji, fn {shortcode, raw_url} ->
223 url = MediaProxy.url(raw_url)
226 shortcode: shortcode,
229 visible_in_picker: false
234 if opts[:embed_relationships] do
235 render("relationship.json", %{
238 relationships: opts[:relationships]
245 with {:ok, instance} <- Pleroma.Instances.Instance.get_cached_by_url(user.ap_id) do
253 if is_nil(instance) do
261 id: to_string(user.id),
262 username: username_from_nickname(user.nickname),
264 display_name: display_name,
265 locked: user.is_locked,
266 created_at: Utils.to_masto_date(user.inserted_at),
267 followers_count: followers_count,
268 following_count: following_count,
269 statuses_count: user.note_count,
271 url: user.uri || user.ap_id,
273 avatar_static: avatar_static,
275 header_static: header_static,
280 note: user.raw_bio || "",
282 fields: user.raw_fields,
284 discoverable: user.is_discoverable,
285 actor_type: user.actor_type
288 last_status_at: user.last_status_at,
290 instance: render("instance.json", %{instance: instance}),
291 status_ttl_days: user.status_ttl_days
294 # Note: it's insecure to output :email but fully-qualified nickname may serve as safe stub
295 fqn: User.full_nickname(user),
298 also_known_as: user.also_known_as,
299 is_confirmed: user.is_confirmed,
300 is_suggested: user.is_suggested,
302 hide_followers_count: user.hide_followers_count,
303 hide_follows_count: user.hide_follows_count,
304 hide_followers: user.hide_followers,
305 hide_follows: user.hide_follows,
306 hide_favorites: user.hide_favorites,
307 relationship: relationship,
308 skip_thread_containment: user.skip_thread_containment,
309 background_image: image_url(user.background) |> MediaProxy.url(),
313 |> maybe_put_role(user, opts[:for])
314 |> maybe_put_settings(user, opts[:for], opts)
315 |> maybe_put_notification_settings(user, opts[:for])
316 |> maybe_put_settings_store(user, opts[:for], opts)
317 |> maybe_put_activation_status(user, opts[:for])
318 |> maybe_put_follow_requests_count(user, opts[:for])
319 |> maybe_put_allow_following_move(user, opts[:for])
320 |> maybe_put_unread_conversation_count(user, opts[:for])
321 |> maybe_put_unread_notification_count(user, opts[:for])
322 |> maybe_put_email_address(user, opts[:for])
325 defp username_from_nickname(string) when is_binary(string) do
326 hd(String.split(string, "@"))
329 defp username_from_nickname(_), do: nil
331 defp maybe_put_follow_requests_count(
333 %User{id: user_id} = user,
337 User.get_follow_requests(user)
341 |> Kernel.put_in([:follow_requests_count], count)
344 defp maybe_put_follow_requests_count(data, _, _), do: data
346 defp maybe_put_settings(
348 %User{id: user_id} = user,
353 |> Kernel.put_in([:source, :privacy], user.default_scope)
354 |> Kernel.put_in([:source, :pleroma, :show_role], user.show_role)
355 |> Kernel.put_in([:source, :pleroma, :no_rich_text], user.no_rich_text)
358 defp maybe_put_settings(data, _, _, _), do: data
360 defp maybe_put_settings_store(data, %User{} = user, %User{}, %{
361 with_pleroma_settings: true
364 |> Kernel.put_in([:pleroma, :settings_store], user.pleroma_settings_store)
367 defp maybe_put_settings_store(data, _, _, _), do: data
369 defp maybe_put_role(data, %User{show_role: true} = user, _) do
371 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
372 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
375 defp maybe_put_role(data, %User{id: user_id} = user, %User{id: user_id}) do
377 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
378 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
381 defp maybe_put_role(data, _, _), do: data
383 defp maybe_put_notification_settings(data, %User{id: user_id} = user, %User{id: user_id}) do
386 [:pleroma, :notification_settings],
387 Map.from_struct(user.notification_settings)
391 defp maybe_put_notification_settings(data, _, _), do: data
393 defp maybe_put_allow_following_move(data, %User{id: user_id} = user, %User{id: user_id}) do
394 Kernel.put_in(data, [:pleroma, :allow_following_move], user.allow_following_move)
397 defp maybe_put_allow_following_move(data, _, _), do: data
399 defp maybe_put_activation_status(data, user, %User{is_admin: true}) do
400 Kernel.put_in(data, [:pleroma, :deactivated], !user.is_active)
403 defp maybe_put_activation_status(data, _, _), do: data
405 defp maybe_put_unread_conversation_count(data, %User{id: user_id} = user, %User{id: user_id}) do
408 [:pleroma, :unread_conversation_count],
409 Pleroma.Conversation.Participation.unread_count(user)
413 defp maybe_put_unread_conversation_count(data, _, _), do: data
415 defp maybe_put_unread_notification_count(data, %User{id: user_id}, %User{id: user_id} = user) do
418 [:pleroma, :unread_notifications_count],
419 Pleroma.Notification.unread_notifications_count(user)
423 defp maybe_put_unread_notification_count(data, _, _), do: data
425 defp maybe_put_email_address(data, %User{id: user_id}, %User{id: user_id} = user) do
433 defp maybe_put_email_address(data, _, _), do: data
435 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
436 defp image_url(_), do: nil