1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.MastodonAPI.AccountView do
10 alias Pleroma.Web.CommonAPI.Utils
11 alias Pleroma.Web.MastodonAPI.AccountView
12 alias Pleroma.Web.MediaProxy
14 def render("accounts.json", %{users: users} = opts) do
16 |> render_many(AccountView, "account.json", opts)
17 |> Enum.filter(&Enum.any?/1)
20 def render("account.json", %{user: user} = opts) do
21 if User.visible_for?(user, opts[:for]),
22 do: do_render("account.json", opts),
26 def render("mention.json", %{user: user}) do
28 id: to_string(user.id),
30 username: username_from_nickname(user.nickname),
31 url: User.profile_url(user)
35 def render("relationship.json", %{user: nil, target: _target}) do
39 def render("relationship.json", %{user: %User{} = user, target: %User{} = target}) do
40 follow_state = User.get_cached_follow_state(user, target)
43 if follow_state && !User.following?(user, target) do
44 follow_state == "pending"
50 id: to_string(target.id),
51 following: User.following?(user, target),
52 followed_by: User.following?(target, user),
53 blocking: User.blocks_ap_id?(user, target),
54 blocked_by: User.blocks_ap_id?(target, user),
55 muting: User.mutes?(user, target),
56 muting_notifications: User.muted_notifications?(user, target),
57 subscribing: User.subscribed_to?(user, target),
59 domain_blocking: User.blocks_domain?(user, target),
60 showing_reblogs: User.showing_reblogs?(user, target),
65 def render("relationships.json", %{user: user, targets: targets}) do
66 render_many(targets, AccountView, "relationship.json", user: user, as: :target)
69 defp do_render("account.json", %{user: user} = opts) do
70 display_name = HTML.strip_tags(user.name || user.nickname)
72 image = User.avatar_url(user) |> MediaProxy.url()
73 header = User.banner_url(user) |> MediaProxy.url()
74 user_info = User.get_cached_user_info(user)
77 if !user.info.hide_follows_count or !user.info.hide_follows or opts[:for] == user do
78 user_info.following_count
84 if !user.info.hide_followers_count or !user.info.hide_followers or opts[:for] == user do
85 user_info.follower_count
90 bot = (user.info.source_data["type"] || "Person") in ["Application", "Service"]
93 (user.info.source_data["tag"] || [])
94 |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
95 |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
97 "shortcode" => String.trim(name, ":"),
98 "url" => MediaProxy.url(url),
99 "static_url" => MediaProxy.url(url),
100 "visible_in_picker" => false
106 |> User.Info.fields()
107 |> Enum.map(fn %{"name" => name, "value" => value} ->
109 "name" => Pleroma.HTML.strip_tags(name),
110 "value" => Pleroma.HTML.filter_tags(value, Pleroma.HTML.Scrubber.LinksOnly)
114 raw_fields = Map.get(user.info, :raw_fields, [])
116 bio = HTML.filter_tags(user.bio, User.html_filter_policy(opts[:for]))
117 relationship = render("relationship.json", %{user: opts[:for], target: user})
120 id: to_string(user.id),
121 username: username_from_nickname(user.nickname),
123 display_name: display_name,
124 locked: user_info.locked,
125 created_at: Utils.to_masto_date(user.inserted_at),
126 followers_count: followers_count,
127 following_count: following_count,
128 statuses_count: user_info.note_count,
130 url: User.profile_url(user),
132 avatar_static: image,
134 header_static: header,
139 note: HTML.strip_tags((user.bio || "") |> String.replace("<br>", "\n")),
147 confirmation_pending: user_info.confirmation_pending,
149 hide_followers_count: user.info.hide_followers_count,
150 hide_follows_count: user.info.hide_follows_count,
151 hide_followers: user.info.hide_followers,
152 hide_follows: user.info.hide_follows,
153 hide_favorites: user.info.hide_favorites,
154 relationship: relationship,
155 skip_thread_containment: user.info.skip_thread_containment,
156 background_image: image_url(user.info.background) |> MediaProxy.url()
159 |> maybe_put_role(user, opts[:for])
160 |> maybe_put_settings(user, opts[:for], user_info)
161 |> maybe_put_notification_settings(user, opts[:for])
162 |> maybe_put_settings_store(user, opts[:for], opts)
163 |> maybe_put_chat_token(user, opts[:for], opts)
164 |> maybe_put_activation_status(user, opts[:for])
167 defp username_from_nickname(string) when is_binary(string) do
168 hd(String.split(string, "@"))
171 defp username_from_nickname(_), do: nil
173 defp maybe_put_settings(
175 %User{id: user_id} = user,
180 |> Kernel.put_in([:source, :privacy], user_info.default_scope)
181 |> Kernel.put_in([:source, :pleroma, :show_role], user.info.show_role)
182 |> Kernel.put_in([:source, :pleroma, :no_rich_text], user.info.no_rich_text)
185 defp maybe_put_settings(data, _, _, _), do: data
187 defp maybe_put_settings_store(data, %User{info: info, id: id}, %User{id: id}, %{
188 with_pleroma_settings: true
191 |> Kernel.put_in([:pleroma, :settings_store], info.pleroma_settings_store)
194 defp maybe_put_settings_store(data, _, _, _), do: data
196 defp maybe_put_chat_token(data, %User{id: id}, %User{id: id}, %{
197 with_chat_token: token
200 |> Kernel.put_in([:pleroma, :chat_token], token)
203 defp maybe_put_chat_token(data, _, _, _), do: data
205 defp maybe_put_role(data, %User{info: %{show_role: true}} = user, _) do
207 |> Kernel.put_in([:pleroma, :is_admin], user.info.is_admin)
208 |> Kernel.put_in([:pleroma, :is_moderator], user.info.is_moderator)
211 defp maybe_put_role(data, %User{id: user_id} = user, %User{id: user_id}) do
213 |> Kernel.put_in([:pleroma, :is_admin], user.info.is_admin)
214 |> Kernel.put_in([:pleroma, :is_moderator], user.info.is_moderator)
217 defp maybe_put_role(data, _, _), do: data
219 defp maybe_put_notification_settings(data, %User{id: user_id} = user, %User{id: user_id}) do
220 Kernel.put_in(data, [:pleroma, :notification_settings], user.info.notification_settings)
223 defp maybe_put_notification_settings(data, _, _), do: data
225 defp maybe_put_activation_status(data, user, %User{info: %{is_admin: true}}) do
226 Kernel.put_in(data, [:pleroma, :deactivated], user.info.deactivated)
229 defp maybe_put_activation_status(data, _, _), do: data
231 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
232 defp image_url(_), do: nil