Merge branch 'develop' into tests/mastodon_api_controller.ex
[akkoma] / lib / pleroma / web / mastodon_api / views / account_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.MastodonAPI.AccountView do
6 use Pleroma.Web, :view
7
8 alias Pleroma.HTML
9 alias Pleroma.User
10 alias Pleroma.Web.CommonAPI.Utils
11 alias Pleroma.Web.MastodonAPI.AccountView
12 alias Pleroma.Web.MediaProxy
13
14 def render("accounts.json", %{users: users} = opts) do
15 users
16 |> render_many(AccountView, "account.json", opts)
17 |> Enum.filter(&Enum.any?/1)
18 end
19
20 def render("account.json", %{user: user} = opts) do
21 if User.visible_for?(user, opts[:for]),
22 do: do_render("account.json", opts),
23 else: %{}
24 end
25
26 def render("mention.json", %{user: user}) do
27 %{
28 id: to_string(user.id),
29 acct: user.nickname,
30 username: username_from_nickname(user.nickname),
31 url: User.profile_url(user)
32 }
33 end
34
35 def render("relationship.json", %{user: nil, target: _target}) do
36 %{}
37 end
38
39 def render("relationship.json", %{user: %User{} = user, target: %User{} = target}) do
40 follow_state = User.get_cached_follow_state(user, target)
41
42 requested =
43 if follow_state && !User.following?(user, target) do
44 follow_state == "pending"
45 else
46 false
47 end
48
49 %{
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),
58 requested: requested,
59 domain_blocking: User.blocks_domain?(user, target),
60 showing_reblogs: User.showing_reblogs?(user, target),
61 endorsed: false
62 }
63 end
64
65 def render("relationships.json", %{user: user, targets: targets}) do
66 render_many(targets, AccountView, "relationship.json", user: user, as: :target)
67 end
68
69 defp do_render("account.json", %{user: user} = opts) do
70 display_name = HTML.strip_tags(user.name || user.nickname)
71
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)
75
76 following_count =
77 if !user.info.hide_follows_count or !user.info.hide_follows or opts[:for] == user do
78 user_info.following_count
79 else
80 0
81 end
82
83 followers_count =
84 if !user.info.hide_followers_count or !user.info.hide_followers or opts[:for] == user do
85 user_info.follower_count
86 else
87 0
88 end
89
90 bot = (user.info.source_data["type"] || "Person") in ["Application", "Service"]
91
92 emojis =
93 (user.info.source_data["tag"] || [])
94 |> Enum.filter(fn %{"type" => t} -> t == "Emoji" end)
95 |> Enum.map(fn %{"icon" => %{"url" => url}, "name" => name} ->
96 %{
97 "shortcode" => String.trim(name, ":"),
98 "url" => MediaProxy.url(url),
99 "static_url" => MediaProxy.url(url),
100 "visible_in_picker" => false
101 }
102 end)
103
104 fields =
105 user.info
106 |> User.Info.fields()
107 |> Enum.map(fn %{"name" => name, "value" => value} ->
108 %{
109 "name" => Pleroma.HTML.strip_tags(name),
110 "value" => Pleroma.HTML.filter_tags(value, Pleroma.HTML.Scrubber.LinksOnly)
111 }
112 end)
113
114 raw_fields = Map.get(user.info, :raw_fields, [])
115
116 bio = HTML.filter_tags(user.bio, User.html_filter_policy(opts[:for]))
117 relationship = render("relationship.json", %{user: opts[:for], target: user})
118
119 %{
120 id: to_string(user.id),
121 username: username_from_nickname(user.nickname),
122 acct: 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,
129 note: bio || "",
130 url: User.profile_url(user),
131 avatar: image,
132 avatar_static: image,
133 header: header,
134 header_static: header,
135 emojis: emojis,
136 fields: fields,
137 bot: bot,
138 source: %{
139 note: HTML.strip_tags((user.bio || "") |> String.replace("<br>", "\n")),
140 sensitive: false,
141 fields: raw_fields,
142 pleroma: %{}
143 },
144
145 # Pleroma extension
146 pleroma: %{
147 confirmation_pending: user_info.confirmation_pending,
148 tags: user.tags,
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()
157 }
158 }
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])
165 end
166
167 defp username_from_nickname(string) when is_binary(string) do
168 hd(String.split(string, "@"))
169 end
170
171 defp username_from_nickname(_), do: nil
172
173 defp maybe_put_settings(
174 data,
175 %User{id: user_id} = user,
176 %User{id: user_id},
177 user_info
178 ) do
179 data
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)
183 end
184
185 defp maybe_put_settings(data, _, _, _), do: data
186
187 defp maybe_put_settings_store(data, %User{info: info, id: id}, %User{id: id}, %{
188 with_pleroma_settings: true
189 }) do
190 data
191 |> Kernel.put_in([:pleroma, :settings_store], info.pleroma_settings_store)
192 end
193
194 defp maybe_put_settings_store(data, _, _, _), do: data
195
196 defp maybe_put_chat_token(data, %User{id: id}, %User{id: id}, %{
197 with_chat_token: token
198 }) do
199 data
200 |> Kernel.put_in([:pleroma, :chat_token], token)
201 end
202
203 defp maybe_put_chat_token(data, _, _, _), do: data
204
205 defp maybe_put_role(data, %User{info: %{show_role: true}} = user, _) do
206 data
207 |> Kernel.put_in([:pleroma, :is_admin], user.info.is_admin)
208 |> Kernel.put_in([:pleroma, :is_moderator], user.info.is_moderator)
209 end
210
211 defp maybe_put_role(data, %User{id: user_id} = user, %User{id: user_id}) do
212 data
213 |> Kernel.put_in([:pleroma, :is_admin], user.info.is_admin)
214 |> Kernel.put_in([:pleroma, :is_moderator], user.info.is_moderator)
215 end
216
217 defp maybe_put_role(data, _, _), do: data
218
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)
221 end
222
223 defp maybe_put_notification_settings(data, _, _), do: data
224
225 defp maybe_put_activation_status(data, user, %User{info: %{is_admin: true}}) do
226 Kernel.put_in(data, [:pleroma, :deactivated], user.info.deactivated)
227 end
228
229 defp maybe_put_activation_status(data, _, _), do: data
230
231 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
232 defp image_url(_), do: nil
233 end