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]
18 # Note: :skip_relationships option is currently intentionally not supported for accounts
21 Map.has_key?(opts, :relationships) ->
24 is_nil(reading_user) ->
25 UserRelationship.view_relationships_option(nil, [])
28 UserRelationship.view_relationships_option(reading_user, users)
31 opts = Map.put(opts, :relationships, relationships_opt)
34 |> render_many(AccountView, "show.json", opts)
35 |> Enum.filter(&Enum.any?/1)
38 def render("show.json", %{user: user} = opts) do
39 if User.visible_for?(user, opts[:for]),
40 do: do_render("show.json", opts),
44 def render("mention.json", %{user: user}) do
46 id: to_string(user.id),
48 username: username_from_nickname(user.nickname),
49 url: user.uri || user.ap_id
53 def render("relationship.json", %{user: nil, target: _target}) do
59 %{user: %User{} = reading_user, target: %User{} = target} = opts
61 user_relationships = get_in(opts, [:relationships, :user_relationships])
62 following_relationships = get_in(opts, [:relationships, :following_relationships])
65 if following_relationships do
66 user_to_target_following_relation =
67 FollowingRelationship.find(following_relationships, reading_user, target)
69 User.get_follow_state(reading_user, target, user_to_target_following_relation)
71 User.get_follow_state(reading_user, target)
75 if following_relationships do
76 case FollowingRelationship.find(following_relationships, target, reading_user) do
77 %{state: :follow_accept} -> true
81 User.following?(target, reading_user)
84 # NOTE: adjust UserRelationship.view_relationships_option/2 on new relation-related flags
86 id: to_string(target.id),
87 following: follow_state == :follow_accept,
88 followed_by: followed_by,
90 UserRelationship.exists?(
95 &User.blocks_user?(&1, &2)
98 UserRelationship.exists?(
103 &User.blocks_user?(&1, &2)
106 UserRelationship.exists?(
113 muting_notifications:
114 UserRelationship.exists?(
119 &User.muted_notifications?(&1, &2)
122 UserRelationship.exists?(
124 :inverse_subscription,
127 &User.subscribed_to?(&2, &1)
129 requested: follow_state == :follow_pending,
130 domain_blocking: User.blocks_domain?(reading_user, target),
132 not UserRelationship.exists?(
137 &User.muting_reblogs?(&1, &2)
143 def render("relationships.json", %{user: user, targets: targets} = opts) do
146 Map.has_key?(opts, :relationships) ->
150 UserRelationship.view_relationships_option(nil, [])
153 UserRelationship.view_relationships_option(user, targets)
156 render_opts = %{as: :target, user: user, relationships: relationships_opt}
157 render_many(targets, AccountView, "relationship.json", render_opts)
160 defp do_render("show.json", %{user: user} = opts) do
161 user = User.sanitize_html(user, User.html_filter_policy(opts[:for]))
162 display_name = user.name || user.nickname
164 image = User.avatar_url(user) |> MediaProxy.url()
165 header = User.banner_url(user) |> MediaProxy.url()
168 if !user.hide_follows_count or !user.hide_follows or opts[:for] == user do
169 user.following_count || 0
175 if !user.hide_followers_count or !user.hide_followers or opts[:for] == user do
176 user.follower_count || 0
181 bot = user.actor_type in ["Application", "Service"]
184 Enum.map(user.emoji, fn {shortcode, url} ->
186 "shortcode" => shortcode,
189 "visible_in_picker" => false
194 if opts[:skip_relationships] do
197 render("relationship.json", %{
200 relationships: opts[:relationships]
205 id: to_string(user.id),
206 username: username_from_nickname(user.nickname),
208 display_name: display_name,
210 created_at: Utils.to_masto_date(user.inserted_at),
211 followers_count: followers_count,
212 following_count: following_count,
213 statuses_count: user.note_count,
214 note: user.bio || "",
215 url: user.uri || user.ap_id,
217 avatar_static: image,
219 header_static: header,
224 note: (user.bio || "") |> String.replace(~r(<br */?>), "\n") |> Pleroma.HTML.strip_tags(),
226 fields: user.raw_fields,
228 discoverable: user.discoverable,
229 actor_type: user.actor_type
235 confirmation_pending: user.confirmation_pending,
237 hide_followers_count: user.hide_followers_count,
238 hide_follows_count: user.hide_follows_count,
239 hide_followers: user.hide_followers,
240 hide_follows: user.hide_follows,
241 hide_favorites: user.hide_favorites,
242 relationship: relationship,
243 skip_thread_containment: user.skip_thread_containment,
244 background_image: image_url(user.background) |> MediaProxy.url()
247 |> maybe_put_role(user, opts[:for])
248 |> maybe_put_settings(user, opts[:for], opts)
249 |> maybe_put_notification_settings(user, opts[:for])
250 |> maybe_put_settings_store(user, opts[:for], opts)
251 |> maybe_put_chat_token(user, opts[:for], opts)
252 |> maybe_put_activation_status(user, opts[:for])
253 |> maybe_put_follow_requests_count(user, opts[:for])
254 |> maybe_put_allow_following_move(user, opts[:for])
255 |> maybe_put_unread_conversation_count(user, opts[:for])
258 defp username_from_nickname(string) when is_binary(string) do
259 hd(String.split(string, "@"))
262 defp username_from_nickname(_), do: nil
264 defp maybe_put_follow_requests_count(
266 %User{id: user_id} = user,
270 User.get_follow_requests(user)
274 |> Kernel.put_in([:follow_requests_count], count)
277 defp maybe_put_follow_requests_count(data, _, _), do: data
279 defp maybe_put_settings(
281 %User{id: user_id} = user,
286 |> Kernel.put_in([:source, :privacy], user.default_scope)
287 |> Kernel.put_in([:source, :pleroma, :show_role], user.show_role)
288 |> Kernel.put_in([:source, :pleroma, :no_rich_text], user.no_rich_text)
291 defp maybe_put_settings(data, _, _, _), do: data
293 defp maybe_put_settings_store(data, %User{} = user, %User{}, %{
294 with_pleroma_settings: true
297 |> Kernel.put_in([:pleroma, :settings_store], user.pleroma_settings_store)
300 defp maybe_put_settings_store(data, _, _, _), do: data
302 defp maybe_put_chat_token(data, %User{id: id}, %User{id: id}, %{
303 with_chat_token: token
306 |> Kernel.put_in([:pleroma, :chat_token], token)
309 defp maybe_put_chat_token(data, _, _, _), do: data
311 defp maybe_put_role(data, %User{show_role: true} = user, _) do
313 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
314 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
317 defp maybe_put_role(data, %User{id: user_id} = user, %User{id: user_id}) do
319 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
320 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
323 defp maybe_put_role(data, _, _), do: data
325 defp maybe_put_notification_settings(data, %User{id: user_id} = user, %User{id: user_id}) do
326 Kernel.put_in(data, [:pleroma, :notification_settings], user.notification_settings)
329 defp maybe_put_notification_settings(data, _, _), do: data
331 defp maybe_put_allow_following_move(data, %User{id: user_id} = user, %User{id: user_id}) do
332 Kernel.put_in(data, [:pleroma, :allow_following_move], user.allow_following_move)
335 defp maybe_put_allow_following_move(data, _, _), do: data
337 defp maybe_put_activation_status(data, user, %User{is_admin: true}) do
338 Kernel.put_in(data, [:pleroma, :deactivated], user.deactivated)
341 defp maybe_put_activation_status(data, _, _), do: data
343 defp maybe_put_unread_conversation_count(data, %User{id: user_id} = user, %User{id: user_id}) do
346 [:pleroma, :unread_conversation_count],
347 user.unread_conversation_count
351 defp maybe_put_unread_conversation_count(data, _, _), do: data
353 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
354 defp image_url(_), do: nil