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
9 alias Pleroma.UserRelationship
10 alias Pleroma.Web.CommonAPI.Utils
11 alias Pleroma.Web.MastodonAPI.AccountView
12 alias Pleroma.Web.MastodonAPI.StatusView
13 alias Pleroma.Web.MediaProxy
15 defp find_following_rel(following_relationships, follower, following) do
16 Enum.find(following_relationships, fn
17 fr -> fr.follower_id == follower.id and fr.following_id == following.id
21 def render("index.json", %{users: users} = opts) do
23 |> render_many(AccountView, "show.json", opts)
24 |> Enum.filter(&Enum.any?/1)
27 def render("show.json", %{user: user} = opts) do
28 if User.visible_for?(user, opts[:for]),
29 do: do_render("show.json", opts),
33 def render("mention.json", %{user: user}) do
35 id: to_string(user.id),
37 username: username_from_nickname(user.nickname),
38 url: User.profile_url(user)
42 def render("relationship.json", %{user: nil, target: _target}) do
48 %{user: %User{} = reading_user, target: %User{} = target} = opts
50 user_relationships = get_in(opts, [:relationships, :user_relationships])
51 following_relationships = get_in(opts, [:relationships, :following_relationships])
54 if following_relationships do
55 user_to_target_following_relation =
56 find_following_rel(following_relationships, reading_user, target)
58 User.get_follow_state(reading_user, target, user_to_target_following_relation)
60 User.get_follow_state(reading_user, target)
64 if following_relationships do
65 case find_following_rel(following_relationships, target, reading_user) do
66 %{state: "accept"} -> true
70 User.following?(target, reading_user)
73 # NOTE: adjust StatusView.relationships_opts/2 if adding new relation-related flags
75 id: to_string(target.id),
76 following: follow_state == "accept",
77 followed_by: followed_by,
79 UserRelationship.exists?(
84 &User.blocks_user?(&1, &2)
87 UserRelationship.exists?(
92 &User.blocks_user?(&1, &2)
95 UserRelationship.exists?(
102 muting_notifications:
103 UserRelationship.exists?(
108 &User.muted_notifications?(&1, &2)
111 UserRelationship.exists?(
113 :inverse_subscription,
116 &User.subscribed_to?(&2, &1)
118 requested: follow_state == "pending",
119 domain_blocking: User.blocks_domain?(reading_user, target),
121 not UserRelationship.exists?(
126 &User.muting_reblogs?(&1, &2)
132 def render("relationships.json", %{user: user, targets: targets}) do
133 relationships_opts = StatusView.relationships_opts(user, targets)
134 opts = %{as: :target, user: user, relationships: relationships_opts}
136 render_many(targets, AccountView, "relationship.json", opts)
139 defp do_render("show.json", %{user: user} = opts) do
140 user = User.sanitize_html(user, User.html_filter_policy(opts[:for]))
141 display_name = user.name || user.nickname
143 image = User.avatar_url(user) |> MediaProxy.url()
144 header = User.banner_url(user) |> MediaProxy.url()
147 if !user.hide_follows_count or !user.hide_follows or opts[:for] == user do
148 user.following_count || 0
154 if !user.hide_followers_count or !user.hide_followers or opts[:for] == user do
155 user.follower_count || 0
160 bot = user.actor_type in ["Application", "Service"]
163 (user.source_data["tag"] || [])
164 |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
165 |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
167 "shortcode" => String.trim(name, ":"),
168 "url" => MediaProxy.url(url),
169 "static_url" => MediaProxy.url(url),
170 "visible_in_picker" => false
175 render("relationship.json", %{
178 relationships: opts[:relationships]
182 id: to_string(user.id),
183 username: username_from_nickname(user.nickname),
185 display_name: display_name,
187 created_at: Utils.to_masto_date(user.inserted_at),
188 followers_count: followers_count,
189 following_count: following_count,
190 statuses_count: user.note_count,
191 note: user.bio || "",
192 url: User.profile_url(user),
194 avatar_static: image,
196 header_static: header,
201 note: (user.bio || "") |> String.replace(~r(<br */?>), "\n") |> Pleroma.HTML.strip_tags(),
203 fields: user.raw_fields,
205 discoverable: user.discoverable,
206 actor_type: user.actor_type
212 confirmation_pending: user.confirmation_pending,
214 hide_followers_count: user.hide_followers_count,
215 hide_follows_count: user.hide_follows_count,
216 hide_followers: user.hide_followers,
217 hide_follows: user.hide_follows,
218 hide_favorites: user.hide_favorites,
219 relationship: relationship,
220 skip_thread_containment: user.skip_thread_containment,
221 background_image: image_url(user.background) |> MediaProxy.url()
224 |> maybe_put_role(user, opts[:for])
225 |> maybe_put_settings(user, opts[:for], opts)
226 |> maybe_put_notification_settings(user, opts[:for])
227 |> maybe_put_settings_store(user, opts[:for], opts)
228 |> maybe_put_chat_token(user, opts[:for], opts)
229 |> maybe_put_activation_status(user, opts[:for])
230 |> maybe_put_follow_requests_count(user, opts[:for])
231 |> maybe_put_allow_following_move(user, opts[:for])
232 |> maybe_put_unread_conversation_count(user, opts[:for])
235 defp username_from_nickname(string) when is_binary(string) do
236 hd(String.split(string, "@"))
239 defp username_from_nickname(_), do: nil
241 defp maybe_put_follow_requests_count(
243 %User{id: user_id} = user,
247 User.get_follow_requests(user)
251 |> Kernel.put_in([:follow_requests_count], count)
254 defp maybe_put_follow_requests_count(data, _, _), do: data
256 defp maybe_put_settings(
258 %User{id: user_id} = user,
263 |> Kernel.put_in([:source, :privacy], user.default_scope)
264 |> Kernel.put_in([:source, :pleroma, :show_role], user.show_role)
265 |> Kernel.put_in([:source, :pleroma, :no_rich_text], user.no_rich_text)
268 defp maybe_put_settings(data, _, _, _), do: data
270 defp maybe_put_settings_store(data, %User{} = user, %User{}, %{
271 with_pleroma_settings: true
274 |> Kernel.put_in([:pleroma, :settings_store], user.pleroma_settings_store)
277 defp maybe_put_settings_store(data, _, _, _), do: data
279 defp maybe_put_chat_token(data, %User{id: id}, %User{id: id}, %{
280 with_chat_token: token
283 |> Kernel.put_in([:pleroma, :chat_token], token)
286 defp maybe_put_chat_token(data, _, _, _), do: data
288 defp maybe_put_role(data, %User{show_role: true} = user, _) do
290 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
291 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
294 defp maybe_put_role(data, %User{id: user_id} = user, %User{id: user_id}) do
296 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
297 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
300 defp maybe_put_role(data, _, _), do: data
302 defp maybe_put_notification_settings(data, %User{id: user_id} = user, %User{id: user_id}) do
303 Kernel.put_in(data, [:pleroma, :notification_settings], user.notification_settings)
306 defp maybe_put_notification_settings(data, _, _), do: data
308 defp maybe_put_allow_following_move(data, %User{id: user_id} = user, %User{id: user_id}) do
309 Kernel.put_in(data, [:pleroma, :allow_following_move], user.allow_following_move)
312 defp maybe_put_allow_following_move(data, _, _), do: data
314 defp maybe_put_activation_status(data, user, %User{is_admin: true}) do
315 Kernel.put_in(data, [:pleroma, :deactivated], user.deactivated)
318 defp maybe_put_activation_status(data, _, _), do: data
320 defp maybe_put_unread_conversation_count(data, %User{id: user_id} = user, %User{id: user_id}) do
323 [:pleroma, :unread_conversation_count],
324 user.unread_conversation_count
328 defp maybe_put_unread_conversation_count(data, _, _), do: data
330 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
331 defp image_url(_), do: nil