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)
30 opts = Map.put(opts, :relationships, relationships_opt)
33 |> render_many(AccountView, "show.json", opts)
34 |> Enum.filter(&Enum.any?/1)
37 def render("show.json", %{user: user} = opts) do
38 if User.visible_for(user, opts[:for]) == :visible do
39 do_render("show.json", opts)
45 def render("mention.json", %{user: user}) do
47 id: to_string(user.id),
49 username: username_from_nickname(user.nickname),
50 url: user.uri || user.ap_id
54 def render("relationship.json", %{user: nil, target: _target}) do
60 %{user: %User{} = reading_user, target: %User{} = target} = opts
62 user_relationships = get_in(opts, [:relationships, :user_relationships])
63 following_relationships = get_in(opts, [:relationships, :following_relationships])
66 if following_relationships do
67 user_to_target_following_relation =
68 FollowingRelationship.find(following_relationships, reading_user, target)
70 User.get_follow_state(reading_user, target, user_to_target_following_relation)
72 User.get_follow_state(reading_user, target)
76 if following_relationships do
77 case FollowingRelationship.find(following_relationships, target, reading_user) do
78 %{state: :follow_accept} -> true
82 User.following?(target, reading_user)
85 # NOTE: adjust UserRelationship.view_relationships_option/2 on new relation-related flags
87 id: to_string(target.id),
88 following: follow_state == :follow_accept,
89 followed_by: followed_by,
91 UserRelationship.exists?(
96 &User.blocks_user?(&1, &2)
99 UserRelationship.exists?(
104 &User.blocks_user?(&1, &2)
107 UserRelationship.exists?(
114 muting_notifications:
115 UserRelationship.exists?(
120 &User.muted_notifications?(&1, &2)
123 UserRelationship.exists?(
125 :inverse_subscription,
128 &User.subscribed_to?(&2, &1)
130 requested: follow_state == :follow_pending,
131 domain_blocking: User.blocks_domain?(reading_user, target),
133 not UserRelationship.exists?(
138 &User.muting_reblogs?(&1, &2)
144 def render("relationships.json", %{user: user, targets: targets} = opts) do
147 Map.has_key?(opts, :relationships) ->
151 UserRelationship.view_relationships_option(nil, [])
154 UserRelationship.view_relationships_option(user, targets)
157 render_opts = %{as: :target, user: user, relationships: relationships_opt}
158 render_many(targets, AccountView, "relationship.json", render_opts)
161 defp do_render("show.json", %{user: user} = opts) do
162 user = User.sanitize_html(user, User.html_filter_policy(opts[:for]))
163 display_name = user.name || user.nickname
165 image = User.avatar_url(user) |> MediaProxy.url()
166 header = User.banner_url(user) |> MediaProxy.url()
169 if !user.hide_follows_count or !user.hide_follows or opts[:for] == user do
170 user.following_count || 0
176 if !user.hide_followers_count or !user.hide_followers or opts[:for] == user do
177 user.follower_count || 0
182 bot = user.actor_type == "Service"
185 Enum.map(user.emoji, fn {shortcode, raw_url} ->
186 url = MediaProxy.url(raw_url)
189 shortcode: shortcode,
192 visible_in_picker: false
197 if opts[:embed_relationships] do
198 render("relationship.json", %{
201 relationships: opts[:relationships]
208 if Pleroma.Config.get([:instances_favicons, :enabled]) do
210 |> Map.get(:ap_id, "")
213 |> Pleroma.Instances.Instance.get_or_update_favicon()
220 id: to_string(user.id),
221 username: username_from_nickname(user.nickname),
223 display_name: display_name,
225 created_at: Utils.to_masto_date(user.inserted_at),
226 followers_count: followers_count,
227 following_count: following_count,
228 statuses_count: user.note_count,
229 note: user.bio || "",
230 url: user.uri || user.ap_id,
232 avatar_static: image,
234 header_static: header,
239 note: user.raw_bio || "",
241 fields: user.raw_fields,
243 discoverable: user.discoverable,
244 actor_type: user.actor_type
251 confirmation_pending: user.confirmation_pending,
253 hide_followers_count: user.hide_followers_count,
254 hide_follows_count: user.hide_follows_count,
255 hide_followers: user.hide_followers,
256 hide_follows: user.hide_follows,
257 hide_favorites: user.hide_favorites,
258 relationship: relationship,
259 skip_thread_containment: user.skip_thread_containment,
260 background_image: image_url(user.background) |> MediaProxy.url(),
261 accepts_chat_messages: user.accepts_chat_messages,
265 |> maybe_put_role(user, opts[:for])
266 |> maybe_put_settings(user, opts[:for], opts)
267 |> maybe_put_notification_settings(user, opts[:for])
268 |> maybe_put_settings_store(user, opts[:for], opts)
269 |> maybe_put_chat_token(user, opts[:for], opts)
270 |> maybe_put_activation_status(user, opts[:for])
271 |> maybe_put_follow_requests_count(user, opts[:for])
272 |> maybe_put_allow_following_move(user, opts[:for])
273 |> maybe_put_unread_conversation_count(user, opts[:for])
274 |> maybe_put_unread_notification_count(user, opts[:for])
277 defp username_from_nickname(string) when is_binary(string) do
278 hd(String.split(string, "@"))
281 defp username_from_nickname(_), do: nil
283 defp maybe_put_follow_requests_count(
285 %User{id: user_id} = user,
289 User.get_follow_requests(user)
293 |> Kernel.put_in([:follow_requests_count], count)
296 defp maybe_put_follow_requests_count(data, _, _), do: data
298 defp maybe_put_settings(
300 %User{id: user_id} = user,
305 |> Kernel.put_in([:source, :privacy], user.default_scope)
306 |> Kernel.put_in([:source, :pleroma, :show_role], user.show_role)
307 |> Kernel.put_in([:source, :pleroma, :no_rich_text], user.no_rich_text)
310 defp maybe_put_settings(data, _, _, _), do: data
312 defp maybe_put_settings_store(data, %User{} = user, %User{}, %{
313 with_pleroma_settings: true
316 |> Kernel.put_in([:pleroma, :settings_store], user.pleroma_settings_store)
319 defp maybe_put_settings_store(data, _, _, _), do: data
321 defp maybe_put_chat_token(data, %User{id: id}, %User{id: id}, %{
322 with_chat_token: token
325 |> Kernel.put_in([:pleroma, :chat_token], token)
328 defp maybe_put_chat_token(data, _, _, _), do: data
330 defp maybe_put_role(data, %User{show_role: true} = user, _) do
332 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
333 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
336 defp maybe_put_role(data, %User{id: user_id} = user, %User{id: user_id}) do
338 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
339 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
342 defp maybe_put_role(data, _, _), do: data
344 defp maybe_put_notification_settings(data, %User{id: user_id} = user, %User{id: user_id}) do
347 [:pleroma, :notification_settings],
348 Map.from_struct(user.notification_settings)
352 defp maybe_put_notification_settings(data, _, _), do: data
354 defp maybe_put_allow_following_move(data, %User{id: user_id} = user, %User{id: user_id}) do
355 Kernel.put_in(data, [:pleroma, :allow_following_move], user.allow_following_move)
358 defp maybe_put_allow_following_move(data, _, _), do: data
360 defp maybe_put_activation_status(data, user, %User{is_admin: true}) do
361 Kernel.put_in(data, [:pleroma, :deactivated], user.deactivated)
364 defp maybe_put_activation_status(data, _, _), do: data
366 defp maybe_put_unread_conversation_count(data, %User{id: user_id} = user, %User{id: user_id}) do
369 [:pleroma, :unread_conversation_count],
370 user.unread_conversation_count
374 defp maybe_put_unread_conversation_count(data, _, _), do: data
376 defp maybe_put_unread_notification_count(data, %User{id: user_id}, %User{id: user_id} = user) do
379 [:pleroma, :unread_notifications_count],
380 Pleroma.Notification.unread_notifications_count(user)
384 defp maybe_put_unread_notification_count(data, _, _), do: data
386 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
387 defp image_url(_), do: nil