Merge branch 'develop' of git.pleroma.social:pleroma/pleroma into remake-remodel-dms
[akkoma] / lib / pleroma / web / mastodon_api / views / account_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.FollowingRelationship
9 alias Pleroma.User
10 alias Pleroma.UserRelationship
11 alias Pleroma.Web.CommonAPI.Utils
12 alias Pleroma.Web.MastodonAPI.AccountView
13 alias Pleroma.Web.MediaProxy
14
15 def render("index.json", %{users: users} = opts) do
16 reading_user = opts[:for]
17
18 # Note: :skip_relationships option is currently intentionally not supported for accounts
19 relationships_opt =
20 cond do
21 Map.has_key?(opts, :relationships) ->
22 opts[:relationships]
23
24 is_nil(reading_user) ->
25 UserRelationship.view_relationships_option(nil, [])
26
27 true ->
28 UserRelationship.view_relationships_option(reading_user, users)
29 end
30
31 opts = Map.put(opts, :relationships, relationships_opt)
32
33 users
34 |> render_many(AccountView, "show.json", opts)
35 |> Enum.filter(&Enum.any?/1)
36 end
37
38 def render("show.json", %{user: user} = opts) do
39 if User.visible_for?(user, opts[:for]),
40 do: do_render("show.json", opts),
41 else: %{}
42 end
43
44 def render("mention.json", %{user: user}) do
45 %{
46 id: to_string(user.id),
47 acct: user.nickname,
48 username: username_from_nickname(user.nickname),
49 url: user.uri || user.ap_id
50 }
51 end
52
53 def render("relationship.json", %{user: nil, target: _target}) do
54 %{}
55 end
56
57 def render(
58 "relationship.json",
59 %{user: %User{} = reading_user, target: %User{} = target} = opts
60 ) do
61 user_relationships = get_in(opts, [:relationships, :user_relationships])
62 following_relationships = get_in(opts, [:relationships, :following_relationships])
63
64 follow_state =
65 if following_relationships do
66 user_to_target_following_relation =
67 FollowingRelationship.find(following_relationships, reading_user, target)
68
69 User.get_follow_state(reading_user, target, user_to_target_following_relation)
70 else
71 User.get_follow_state(reading_user, target)
72 end
73
74 followed_by =
75 if following_relationships do
76 case FollowingRelationship.find(following_relationships, target, reading_user) do
77 %{state: :follow_accept} -> true
78 _ -> false
79 end
80 else
81 User.following?(target, reading_user)
82 end
83
84 # NOTE: adjust UserRelationship.view_relationships_option/2 on new relation-related flags
85 %{
86 id: to_string(target.id),
87 following: follow_state == :follow_accept,
88 followed_by: followed_by,
89 blocking:
90 UserRelationship.exists?(
91 user_relationships,
92 :block,
93 reading_user,
94 target,
95 &User.blocks_user?(&1, &2)
96 ),
97 blocked_by:
98 UserRelationship.exists?(
99 user_relationships,
100 :block,
101 target,
102 reading_user,
103 &User.blocks_user?(&1, &2)
104 ),
105 muting:
106 UserRelationship.exists?(
107 user_relationships,
108 :mute,
109 reading_user,
110 target,
111 &User.mutes?(&1, &2)
112 ),
113 muting_notifications:
114 UserRelationship.exists?(
115 user_relationships,
116 :notification_mute,
117 reading_user,
118 target,
119 &User.muted_notifications?(&1, &2)
120 ),
121 subscribing:
122 UserRelationship.exists?(
123 user_relationships,
124 :inverse_subscription,
125 target,
126 reading_user,
127 &User.subscribed_to?(&2, &1)
128 ),
129 requested: follow_state == :follow_pending,
130 domain_blocking: User.blocks_domain?(reading_user, target),
131 showing_reblogs:
132 not UserRelationship.exists?(
133 user_relationships,
134 :reblog_mute,
135 reading_user,
136 target,
137 &User.muting_reblogs?(&1, &2)
138 ),
139 endorsed: false
140 }
141 end
142
143 def render("relationships.json", %{user: user, targets: targets} = opts) do
144 relationships_opt =
145 cond do
146 Map.has_key?(opts, :relationships) ->
147 opts[:relationships]
148
149 is_nil(user) ->
150 UserRelationship.view_relationships_option(nil, [])
151
152 true ->
153 UserRelationship.view_relationships_option(user, targets)
154 end
155
156 render_opts = %{as: :target, user: user, relationships: relationships_opt}
157 render_many(targets, AccountView, "relationship.json", render_opts)
158 end
159
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
163
164 image = User.avatar_url(user) |> MediaProxy.url()
165 header = User.banner_url(user) |> MediaProxy.url()
166
167 following_count =
168 if !user.hide_follows_count or !user.hide_follows or opts[:for] == user do
169 user.following_count || 0
170 else
171 0
172 end
173
174 followers_count =
175 if !user.hide_followers_count or !user.hide_followers or opts[:for] == user do
176 user.follower_count || 0
177 else
178 0
179 end
180
181 bot = user.actor_type in ["Application", "Service"]
182
183 emojis =
184 Enum.map(user.emoji, fn {shortcode, url} ->
185 %{
186 "shortcode" => shortcode,
187 "url" => url,
188 "static_url" => url,
189 "visible_in_picker" => false
190 }
191 end)
192
193 relationship =
194 if opts[:skip_relationships] do
195 %{}
196 else
197 render("relationship.json", %{
198 user: opts[:for],
199 target: user,
200 relationships: opts[:relationships]
201 })
202 end
203
204 %{
205 id: to_string(user.id),
206 username: username_from_nickname(user.nickname),
207 acct: user.nickname,
208 display_name: display_name,
209 locked: user.locked,
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,
216 avatar: image,
217 avatar_static: image,
218 header: header,
219 header_static: header,
220 emojis: emojis,
221 fields: user.fields,
222 bot: bot,
223 source: %{
224 note: (user.bio || "") |> String.replace(~r(<br */?>), "\n") |> Pleroma.HTML.strip_tags(),
225 sensitive: false,
226 fields: user.raw_fields,
227 pleroma: %{
228 discoverable: user.discoverable,
229 actor_type: user.actor_type
230 }
231 },
232
233 # Pleroma extension
234 pleroma: %{
235 ap_id: user.ap_id,
236 confirmation_pending: user.confirmation_pending,
237 tags: user.tags,
238 hide_followers_count: user.hide_followers_count,
239 hide_follows_count: user.hide_follows_count,
240 hide_followers: user.hide_followers,
241 hide_follows: user.hide_follows,
242 hide_favorites: user.hide_favorites,
243 relationship: relationship,
244 skip_thread_containment: user.skip_thread_containment,
245 background_image: image_url(user.background) |> MediaProxy.url()
246 }
247 }
248 |> maybe_put_role(user, opts[:for])
249 |> maybe_put_settings(user, opts[:for], opts)
250 |> maybe_put_notification_settings(user, opts[:for])
251 |> maybe_put_settings_store(user, opts[:for], opts)
252 |> maybe_put_chat_token(user, opts[:for], opts)
253 |> maybe_put_activation_status(user, opts[:for])
254 |> maybe_put_follow_requests_count(user, opts[:for])
255 |> maybe_put_allow_following_move(user, opts[:for])
256 |> maybe_put_unread_conversation_count(user, opts[:for])
257 end
258
259 defp username_from_nickname(string) when is_binary(string) do
260 hd(String.split(string, "@"))
261 end
262
263 defp username_from_nickname(_), do: nil
264
265 defp maybe_put_follow_requests_count(
266 data,
267 %User{id: user_id} = user,
268 %User{id: user_id}
269 ) do
270 count =
271 User.get_follow_requests(user)
272 |> length()
273
274 data
275 |> Kernel.put_in([:follow_requests_count], count)
276 end
277
278 defp maybe_put_follow_requests_count(data, _, _), do: data
279
280 defp maybe_put_settings(
281 data,
282 %User{id: user_id} = user,
283 %User{id: user_id},
284 _opts
285 ) do
286 data
287 |> Kernel.put_in([:source, :privacy], user.default_scope)
288 |> Kernel.put_in([:source, :pleroma, :show_role], user.show_role)
289 |> Kernel.put_in([:source, :pleroma, :no_rich_text], user.no_rich_text)
290 end
291
292 defp maybe_put_settings(data, _, _, _), do: data
293
294 defp maybe_put_settings_store(data, %User{} = user, %User{}, %{
295 with_pleroma_settings: true
296 }) do
297 data
298 |> Kernel.put_in([:pleroma, :settings_store], user.pleroma_settings_store)
299 end
300
301 defp maybe_put_settings_store(data, _, _, _), do: data
302
303 defp maybe_put_chat_token(data, %User{id: id}, %User{id: id}, %{
304 with_chat_token: token
305 }) do
306 data
307 |> Kernel.put_in([:pleroma, :chat_token], token)
308 end
309
310 defp maybe_put_chat_token(data, _, _, _), do: data
311
312 defp maybe_put_role(data, %User{show_role: true} = user, _) do
313 data
314 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
315 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
316 end
317
318 defp maybe_put_role(data, %User{id: user_id} = user, %User{id: user_id}) do
319 data
320 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
321 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
322 end
323
324 defp maybe_put_role(data, _, _), do: data
325
326 defp maybe_put_notification_settings(data, %User{id: user_id} = user, %User{id: user_id}) do
327 Kernel.put_in(data, [:pleroma, :notification_settings], user.notification_settings)
328 end
329
330 defp maybe_put_notification_settings(data, _, _), do: data
331
332 defp maybe_put_allow_following_move(data, %User{id: user_id} = user, %User{id: user_id}) do
333 Kernel.put_in(data, [:pleroma, :allow_following_move], user.allow_following_move)
334 end
335
336 defp maybe_put_allow_following_move(data, _, _), do: data
337
338 defp maybe_put_activation_status(data, user, %User{is_admin: true}) do
339 Kernel.put_in(data, [:pleroma, :deactivated], user.deactivated)
340 end
341
342 defp maybe_put_activation_status(data, _, _), do: data
343
344 defp maybe_put_unread_conversation_count(data, %User{id: user_id} = user, %User{id: user_id}) do
345 data
346 |> Kernel.put_in(
347 [:pleroma, :unread_conversation_count],
348 user.unread_conversation_count
349 )
350 end
351
352 defp maybe_put_unread_conversation_count(data, _, _), do: data
353
354 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
355 defp image_url(_), do: nil
356 end