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.Web.CommonAPI.Utils
10 alias Pleroma.Web.MastodonAPI.AccountView
11 alias Pleroma.Web.MediaProxy
13 def render("index.json", %{users: users} = opts) do
15 |> render_many(AccountView, "show.json", opts)
16 |> Enum.filter(&Enum.any?/1)
19 def render("show.json", %{user: user} = opts) do
20 if User.visible_for?(user, opts[:for]),
21 do: do_render("show.json", opts),
25 def render("mention.json", %{user: user}) do
27 id: to_string(user.id),
29 username: username_from_nickname(user.nickname),
30 url: User.profile_url(user)
34 def render("relationship.json", %{user: nil, target: _target}) do
38 def render("relationship.json", %{user: %User{} = user, target: %User{} = target}) do
39 follow_state = User.get_cached_follow_state(user, target)
42 if follow_state && !User.following?(user, target) do
43 follow_state == "pending"
49 id: to_string(target.id),
50 following: User.following?(user, target),
51 followed_by: User.following?(target, user),
52 blocking: User.blocks_user?(user, target),
53 blocked_by: User.blocks_user?(target, user),
54 muting: User.mutes?(user, target),
55 muting_notifications: User.muted_notifications?(user, target),
56 subscribing: User.subscribed_to?(user, target),
58 domain_blocking: User.blocks_domain?(user, target),
59 showing_reblogs: User.showing_reblogs?(user, target),
64 def render("relationships.json", %{user: user, targets: targets}) do
65 render_many(targets, AccountView, "relationship.json", user: user, as: :target)
68 defp do_render("show.json", %{user: user} = opts) do
69 user = User.sanitize_html(user, User.html_filter_policy(opts[:for]))
70 display_name = user.name || user.nickname
72 image = User.avatar_url(user) |> MediaProxy.url()
73 header = User.banner_url(user) |> MediaProxy.url()
76 if !user.hide_follows_count or !user.hide_follows or opts[:for] == user do
77 user.following_count || 0
83 if !user.hide_followers_count or !user.hide_followers or opts[:for] == user do
84 user.follower_count || 0
89 bot = user.actor_type in ["Application", "Service"]
92 (user.source_data["tag"] || [])
93 |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
94 |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
96 "shortcode" => String.trim(name, ":"),
97 "url" => MediaProxy.url(url),
98 "static_url" => MediaProxy.url(url),
99 "visible_in_picker" => false
103 relationship = render("relationship.json", %{user: opts[:for], target: user})
106 id: to_string(user.id),
107 username: username_from_nickname(user.nickname),
109 display_name: display_name,
111 created_at: Utils.to_masto_date(user.inserted_at),
112 followers_count: followers_count,
113 following_count: following_count,
114 statuses_count: user.note_count,
115 note: user.bio || "",
116 url: User.profile_url(user),
118 avatar_static: image,
120 header_static: header,
125 note: (user.bio || "") |> String.replace(~r(<br */?>), "\n") |> Pleroma.HTML.strip_tags(),
127 fields: user.raw_fields,
129 discoverable: user.discoverable,
130 actor_type: user.actor_type
136 confirmation_pending: user.confirmation_pending,
138 hide_followers_count: user.hide_followers_count,
139 hide_follows_count: user.hide_follows_count,
140 hide_followers: user.hide_followers,
141 hide_follows: user.hide_follows,
142 hide_favorites: user.hide_favorites,
143 relationship: relationship,
144 skip_thread_containment: user.skip_thread_containment,
145 background_image: image_url(user.background) |> MediaProxy.url()
148 |> maybe_put_role(user, opts[:for])
149 |> maybe_put_settings(user, opts[:for], opts)
150 |> maybe_put_notification_settings(user, opts[:for])
151 |> maybe_put_settings_store(user, opts[:for], opts)
152 |> maybe_put_chat_token(user, opts[:for], opts)
153 |> maybe_put_activation_status(user, opts[:for])
154 |> maybe_put_follow_requests_count(user, opts[:for])
155 |> maybe_put_allow_following_move(user, opts[:for])
156 |> maybe_put_unread_conversation_count(user, opts[:for])
159 defp username_from_nickname(string) when is_binary(string) do
160 hd(String.split(string, "@"))
163 defp username_from_nickname(_), do: nil
165 defp maybe_put_follow_requests_count(
167 %User{id: user_id} = user,
171 User.get_follow_requests(user)
175 |> Kernel.put_in([:follow_requests_count], count)
178 defp maybe_put_follow_requests_count(data, _, _), do: data
180 defp maybe_put_settings(
182 %User{id: user_id} = user,
187 |> Kernel.put_in([:source, :privacy], user.default_scope)
188 |> Kernel.put_in([:source, :pleroma, :show_role], user.show_role)
189 |> Kernel.put_in([:source, :pleroma, :no_rich_text], user.no_rich_text)
192 defp maybe_put_settings(data, _, _, _), do: data
194 defp maybe_put_settings_store(data, %User{} = user, %User{}, %{
195 with_pleroma_settings: true
198 |> Kernel.put_in([:pleroma, :settings_store], user.pleroma_settings_store)
201 defp maybe_put_settings_store(data, _, _, _), do: data
203 defp maybe_put_chat_token(data, %User{id: id}, %User{id: id}, %{
204 with_chat_token: token
207 |> Kernel.put_in([:pleroma, :chat_token], token)
210 defp maybe_put_chat_token(data, _, _, _), do: data
212 defp maybe_put_role(data, %User{show_role: true} = user, _) do
214 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
215 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
218 defp maybe_put_role(data, %User{id: user_id} = user, %User{id: user_id}) do
220 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
221 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
224 defp maybe_put_role(data, _, _), do: data
226 defp maybe_put_notification_settings(data, %User{id: user_id} = user, %User{id: user_id}) do
227 Kernel.put_in(data, [:pleroma, :notification_settings], user.notification_settings)
230 defp maybe_put_notification_settings(data, _, _), do: data
232 defp maybe_put_allow_following_move(data, %User{id: user_id} = user, %User{id: user_id}) do
233 Kernel.put_in(data, [:pleroma, :allow_following_move], user.allow_following_move)
236 defp maybe_put_allow_following_move(data, _, _), do: data
238 defp maybe_put_activation_status(data, user, %User{is_admin: true}) do
239 Kernel.put_in(data, [:pleroma, :deactivated], user.deactivated)
242 defp maybe_put_activation_status(data, _, _), do: data
244 defp maybe_put_unread_conversation_count(data, %User{id: user_id} = user, %User{id: user_id}) do
247 [:pleroma, :unread_conversation_count],
248 user.unread_conversation_count
252 defp maybe_put_unread_conversation_count(data, _, _), do: data
254 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
255 defp image_url(_), do: nil