1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.UserRelationship
11 alias Pleroma.Web.CommonAPI.Utils
12 alias Pleroma.Web.MastodonAPI.AccountView
13 alias Pleroma.Web.MediaProxy
15 def render("index.json", %{users: users} = opts) do
16 reading_user = opts[:for]
20 Map.has_key?(opts, :relationships) ->
23 is_nil(reading_user) || !opts[:embed_relationships] ->
24 UserRelationship.view_relationships_option(nil, [])
27 UserRelationship.view_relationships_option(reading_user, users)
32 |> Map.merge(%{relationships: relationships_opt, as: :user})
36 |> render_many(AccountView, "show.json", opts)
37 |> Enum.filter(&Enum.any?/1)
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.
47 def render("show.json", %{user: _user, skip_visibility_check: true} = opts) do
48 do_render("show.json", opts)
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)
59 def render("show.json", _) do
60 raise "In order to prevent account accessibility issues, " <>
61 ":skip_visibility_check or :for option is required."
64 def render("mention.json", %{user: user}) do
66 id: to_string(user.id),
68 username: username_from_nickname(user.nickname),
69 url: user.uri || user.ap_id
73 def render("relationship.json", %{user: nil, target: _target}) do
79 %{user: %User{} = reading_user, target: %User{} = target} = opts
81 user_relationships = get_in(opts, [:relationships, :user_relationships])
82 following_relationships = get_in(opts, [:relationships, :following_relationships])
85 if following_relationships do
86 user_to_target_following_relation =
87 FollowingRelationship.find(following_relationships, reading_user, target)
89 User.get_follow_state(reading_user, target, user_to_target_following_relation)
91 User.get_follow_state(reading_user, target)
95 if following_relationships do
96 case FollowingRelationship.find(following_relationships, target, reading_user) do
97 %{state: :follow_accept} -> true
101 User.following?(target, reading_user)
104 # NOTE: adjust UserRelationship.view_relationships_option/2 on new relation-related flags
106 id: to_string(target.id),
107 following: follow_state == :follow_accept,
108 followed_by: followed_by,
110 UserRelationship.exists?(
115 &User.blocks_user?(&1, &2)
118 UserRelationship.exists?(
123 &User.blocks_user?(&1, &2)
126 UserRelationship.exists?(
133 muting_notifications:
134 UserRelationship.exists?(
139 &User.muted_notifications?(&1, &2)
142 UserRelationship.exists?(
144 :inverse_subscription,
147 &User.subscribed_to?(&2, &1)
149 requested: follow_state == :follow_pending,
150 domain_blocking: User.blocks_domain?(reading_user, target),
152 not UserRelationship.exists?(
157 &User.muting_reblogs?(&1, &2)
163 def render("relationships.json", %{user: user, targets: targets} = opts) do
166 Map.has_key?(opts, :relationships) ->
170 UserRelationship.view_relationships_option(nil, [])
173 UserRelationship.view_relationships_option(user, targets)
176 render_opts = %{as: :target, user: user, relationships: relationships_opt}
177 render_many(targets, AccountView, "relationship.json", render_opts)
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
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)
190 if !user.hide_follows_count or !user.hide_follows or opts[:for] == user do
191 user.following_count || 0
197 if !user.hide_followers_count or !user.hide_followers or opts[:for] == user do
198 user.follower_count || 0
203 bot = user.actor_type == "Service"
206 Enum.map(user.emoji, fn {shortcode, raw_url} ->
207 url = MediaProxy.url(raw_url)
210 shortcode: shortcode,
213 visible_in_picker: false
218 if opts[:embed_relationships] do
219 render("relationship.json", %{
222 relationships: opts[:relationships]
229 if Pleroma.Config.get([:instances_favicons, :enabled]) do
231 |> Map.get(:ap_id, "")
234 |> Pleroma.Instances.Instance.get_or_update_favicon()
241 id: to_string(user.id),
242 username: username_from_nickname(user.nickname),
244 display_name: display_name,
245 locked: user.is_locked,
246 created_at: Utils.to_masto_date(user.inserted_at),
247 followers_count: followers_count,
248 following_count: following_count,
249 statuses_count: user.note_count,
251 url: user.uri || user.ap_id,
253 avatar_static: avatar_static,
255 header_static: header_static,
260 note: user.raw_bio || "",
262 fields: user.raw_fields,
264 discoverable: user.is_discoverable,
265 actor_type: user.actor_type
272 confirmation_pending: user.confirmation_pending,
274 hide_followers_count: user.hide_followers_count,
275 hide_follows_count: user.hide_follows_count,
276 hide_followers: user.hide_followers,
277 hide_follows: user.hide_follows,
278 hide_favorites: user.hide_favorites,
279 relationship: relationship,
280 skip_thread_containment: user.skip_thread_containment,
281 background_image: image_url(user.background) |> MediaProxy.url(),
282 accepts_chat_messages: user.accepts_chat_messages,
286 |> maybe_put_role(user, opts[:for])
287 |> maybe_put_settings(user, opts[:for], opts)
288 |> maybe_put_notification_settings(user, opts[:for])
289 |> maybe_put_settings_store(user, opts[:for], opts)
290 |> maybe_put_chat_token(user, opts[:for], opts)
291 |> maybe_put_activation_status(user, opts[:for])
292 |> maybe_put_follow_requests_count(user, opts[:for])
293 |> maybe_put_allow_following_move(user, opts[:for])
294 |> maybe_put_unread_conversation_count(user, opts[:for])
295 |> maybe_put_unread_notification_count(user, opts[:for])
298 defp username_from_nickname(string) when is_binary(string) do
299 hd(String.split(string, "@"))
302 defp username_from_nickname(_), do: nil
304 defp maybe_put_follow_requests_count(
306 %User{id: user_id} = user,
310 User.get_follow_requests(user)
314 |> Kernel.put_in([:follow_requests_count], count)
317 defp maybe_put_follow_requests_count(data, _, _), do: data
319 defp maybe_put_settings(
321 %User{id: user_id} = user,
326 |> Kernel.put_in([:source, :privacy], user.default_scope)
327 |> Kernel.put_in([:source, :pleroma, :show_role], user.show_role)
328 |> Kernel.put_in([:source, :pleroma, :no_rich_text], user.no_rich_text)
331 defp maybe_put_settings(data, _, _, _), do: data
333 defp maybe_put_settings_store(data, %User{} = user, %User{}, %{
334 with_pleroma_settings: true
337 |> Kernel.put_in([:pleroma, :settings_store], user.pleroma_settings_store)
340 defp maybe_put_settings_store(data, _, _, _), do: data
342 defp maybe_put_chat_token(data, %User{id: id}, %User{id: id}, %{
343 with_chat_token: token
346 |> Kernel.put_in([:pleroma, :chat_token], token)
349 defp maybe_put_chat_token(data, _, _, _), do: data
351 defp maybe_put_role(data, %User{show_role: true} = user, _) do
353 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
354 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
357 defp maybe_put_role(data, %User{id: user_id} = user, %User{id: user_id}) do
359 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
360 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
363 defp maybe_put_role(data, _, _), do: data
365 defp maybe_put_notification_settings(data, %User{id: user_id} = user, %User{id: user_id}) do
368 [:pleroma, :notification_settings],
369 Map.from_struct(user.notification_settings)
373 defp maybe_put_notification_settings(data, _, _), do: data
375 defp maybe_put_allow_following_move(data, %User{id: user_id} = user, %User{id: user_id}) do
376 Kernel.put_in(data, [:pleroma, :allow_following_move], user.allow_following_move)
379 defp maybe_put_allow_following_move(data, _, _), do: data
381 defp maybe_put_activation_status(data, user, %User{is_admin: true}) do
382 Kernel.put_in(data, [:pleroma, :deactivated], user.deactivated)
385 defp maybe_put_activation_status(data, _, _), do: data
387 defp maybe_put_unread_conversation_count(data, %User{id: user_id} = user, %User{id: user_id}) do
390 [:pleroma, :unread_conversation_count],
391 Pleroma.Conversation.Participation.unread_count(user)
395 defp maybe_put_unread_conversation_count(data, _, _), do: data
397 defp maybe_put_unread_notification_count(data, %User{id: user_id}, %User{id: user_id} = user) do
400 [:pleroma, :unread_notifications_count],
401 Pleroma.Notification.unread_notifications_count(user)
405 defp maybe_put_unread_notification_count(data, _, _), do: data
407 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
408 defp image_url(_), do: nil