Language code on mastoapi statuses (#433)
[akkoma] / lib / pleroma / web / mastodon_api / views / account_view.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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.UserNote
11 alias Pleroma.UserRelationship
12 alias Pleroma.Web.CommonAPI.Utils
13 alias Pleroma.Web.MastodonAPI.AccountView
14 alias Pleroma.Web.MediaProxy
15
16 def render("index.json", %{users: users} = opts) do
17 reading_user = opts[:for]
18
19 relationships_opt =
20 cond do
21 Map.has_key?(opts, :relationships) ->
22 opts[:relationships]
23
24 is_nil(reading_user) || !opts[:embed_relationships] ->
25 UserRelationship.view_relationships_option(nil, [])
26
27 true ->
28 UserRelationship.view_relationships_option(reading_user, users)
29 end
30
31 opts =
32 opts
33 |> Map.merge(%{relationships: relationships_opt, as: :user})
34 |> Map.delete(:users)
35
36 users
37 |> render_many(AccountView, "show.json", opts)
38 |> Enum.filter(&Enum.any?/1)
39 end
40
41 @doc """
42 Renders specified user account.
43 :skip_visibility_check option skips visibility check and renders any user (local or remote)
44 regardless of [:pleroma, :restrict_unauthenticated] setting.
45 :for option specifies the requester and can be a User record or nil.
46 Only use `user: user, for: user` when `user` is the actual requester of own profile.
47 """
48 def render("show.json", %{user: _user, skip_visibility_check: true} = opts) do
49 do_render("show.json", opts)
50 end
51
52 def render("show.json", %{user: user, for: for_user_or_nil} = opts) do
53 if User.visible_for(user, for_user_or_nil) == :visible do
54 do_render("show.json", opts)
55 else
56 %{}
57 end
58 end
59
60 def render("show.json", _) do
61 raise "In order to prevent account accessibility issues, " <>
62 ":skip_visibility_check or :for option is required."
63 end
64
65 def render("mention.json", %{user: user}) do
66 %{
67 id: to_string(user.id),
68 acct: user.nickname,
69 username: username_from_nickname(user.nickname),
70 url: user.uri || user.ap_id
71 }
72 end
73
74 def render("relationship.json", %{user: nil, target: _target}) do
75 %{}
76 end
77
78 def render(
79 "relationship.json",
80 %{user: %User{} = reading_user, target: %User{} = target} = opts
81 ) do
82 user_relationships = get_in(opts, [:relationships, :user_relationships])
83 following_relationships = get_in(opts, [:relationships, :following_relationships])
84
85 follow_state =
86 if following_relationships do
87 user_to_target_following_relation =
88 FollowingRelationship.find(following_relationships, reading_user, target)
89
90 User.get_follow_state(reading_user, target, user_to_target_following_relation)
91 else
92 User.get_follow_state(reading_user, target)
93 end
94
95 followed_by =
96 if following_relationships do
97 target_to_user_following_relation =
98 FollowingRelationship.find(following_relationships, target, reading_user)
99
100 User.get_follow_state(target, reading_user, target_to_user_following_relation)
101 else
102 User.get_follow_state(target, reading_user)
103 end
104
105 subscribing =
106 UserRelationship.exists?(
107 user_relationships,
108 :inverse_subscription,
109 target,
110 reading_user,
111 &User.subscribed_to?(&2, &1)
112 )
113
114 # NOTE: adjust UserRelationship.view_relationships_option/2 on new relation-related flags
115 %{
116 id: to_string(target.id),
117 following: follow_state == :follow_accept,
118 followed_by: followed_by == :follow_accept,
119 blocking:
120 UserRelationship.exists?(
121 user_relationships,
122 :block,
123 reading_user,
124 target,
125 &User.blocks_user?(&1, &2)
126 ),
127 blocked_by:
128 UserRelationship.exists?(
129 user_relationships,
130 :block,
131 target,
132 reading_user,
133 &User.blocks_user?(&1, &2)
134 ),
135 muting:
136 UserRelationship.exists?(
137 user_relationships,
138 :mute,
139 reading_user,
140 target,
141 &User.mutes?(&1, &2)
142 ),
143 muting_notifications:
144 UserRelationship.exists?(
145 user_relationships,
146 :notification_mute,
147 reading_user,
148 target,
149 &User.muted_notifications?(&1, &2)
150 ),
151 subscribing: subscribing,
152 notifying: subscribing,
153 requested: follow_state == :follow_pending,
154 requested_by: followed_by == :follow_pending,
155 domain_blocking: User.blocks_domain?(reading_user, target),
156 showing_reblogs:
157 not UserRelationship.exists?(
158 user_relationships,
159 :reblog_mute,
160 reading_user,
161 target,
162 &User.muting_reblogs?(&1, &2)
163 ),
164 endorsed: false,
165 note:
166 UserNote.show(
167 reading_user,
168 target
169 )
170 }
171 end
172
173 def render("relationships.json", %{user: user, targets: targets} = opts) do
174 relationships_opt =
175 cond do
176 Map.has_key?(opts, :relationships) ->
177 opts[:relationships]
178
179 is_nil(user) ->
180 UserRelationship.view_relationships_option(nil, [])
181
182 true ->
183 UserRelationship.view_relationships_option(user, targets)
184 end
185
186 render_opts = %{as: :target, user: user, relationships: relationships_opt}
187 render_many(targets, AccountView, "relationship.json", render_opts)
188 end
189
190 def render("instance.json", %{instance: %Pleroma.Instances.Instance{} = instance}) do
191 %{
192 name: instance.host,
193 favicon: instance.favicon |> MediaProxy.url(),
194 nodeinfo: instance.nodeinfo
195 }
196 end
197
198 def render("instance.json", _), do: nil
199
200 defp do_render("show.json", %{user: user} = opts) do
201 user = User.sanitize_html(user, User.html_filter_policy(opts[:for]))
202 display_name = user.name || user.nickname
203
204 avatar = User.avatar_url(user) |> MediaProxy.url()
205 avatar_static = User.avatar_url(user) |> MediaProxy.preview_url(static: true)
206 header = User.banner_url(user) |> MediaProxy.url()
207 header_static = User.banner_url(user) |> MediaProxy.preview_url(static: true)
208
209 following_count =
210 if !user.hide_follows_count or !user.hide_follows or opts[:for] == user,
211 do: user.following_count,
212 else: 0
213
214 followers_count =
215 if !user.hide_followers_count or !user.hide_followers or opts[:for] == user,
216 do: user.follower_count,
217 else: 0
218
219 bot = user.actor_type == "Service"
220
221 emojis =
222 Enum.map(user.emoji, fn {shortcode, raw_url} ->
223 url = MediaProxy.url(raw_url)
224
225 %{
226 shortcode: shortcode,
227 url: url,
228 static_url: url,
229 visible_in_picker: false
230 }
231 end)
232
233 relationship =
234 if opts[:embed_relationships] do
235 render("relationship.json", %{
236 user: opts[:for],
237 target: user,
238 relationships: opts[:relationships]
239 })
240 else
241 %{}
242 end
243
244 instance =
245 with {:ok, instance} <- Pleroma.Instances.Instance.get_cached_by_url(user.ap_id) do
246 instance
247 else
248 _ ->
249 nil
250 end
251
252 favicon =
253 if is_nil(instance) do
254 nil
255 else
256 instance.favicon
257 |> MediaProxy.url()
258 end
259
260 %{
261 id: to_string(user.id),
262 username: username_from_nickname(user.nickname),
263 acct: user.nickname,
264 display_name: display_name,
265 locked: user.is_locked,
266 created_at: Utils.to_masto_date(user.inserted_at),
267 followers_count: followers_count,
268 following_count: following_count,
269 statuses_count: user.note_count,
270 note: user.bio,
271 url: user.uri || user.ap_id,
272 avatar: avatar,
273 avatar_static: avatar_static,
274 header: header,
275 header_static: header_static,
276 emojis: emojis,
277 fields: user.fields,
278 bot: bot,
279 source: %{
280 note: user.raw_bio || "",
281 sensitive: false,
282 fields: user.raw_fields,
283 pleroma: %{
284 discoverable: user.is_discoverable,
285 actor_type: user.actor_type
286 }
287 },
288 last_status_at: user.last_status_at,
289 akkoma: %{
290 instance: render("instance.json", %{instance: instance}),
291 status_ttl_days: user.status_ttl_days
292 },
293 # Pleroma extensions
294 # Note: it's insecure to output :email but fully-qualified nickname may serve as safe stub
295 fqn: User.full_nickname(user),
296 pleroma: %{
297 ap_id: user.ap_id,
298 also_known_as: user.also_known_as,
299 is_confirmed: user.is_confirmed,
300 is_suggested: user.is_suggested,
301 tags: user.tags,
302 hide_followers_count: user.hide_followers_count,
303 hide_follows_count: user.hide_follows_count,
304 hide_followers: user.hide_followers,
305 hide_follows: user.hide_follows,
306 hide_favorites: user.hide_favorites,
307 relationship: relationship,
308 skip_thread_containment: user.skip_thread_containment,
309 background_image: image_url(user.background) |> MediaProxy.url(),
310 favicon: favicon
311 }
312 }
313 |> maybe_put_role(user, opts[:for])
314 |> maybe_put_settings(user, opts[:for], opts)
315 |> maybe_put_notification_settings(user, opts[:for])
316 |> maybe_put_settings_store(user, opts[:for], opts)
317 |> maybe_put_activation_status(user, opts[:for])
318 |> maybe_put_follow_requests_count(user, opts[:for])
319 |> maybe_put_allow_following_move(user, opts[:for])
320 |> maybe_put_unread_conversation_count(user, opts[:for])
321 |> maybe_put_unread_notification_count(user, opts[:for])
322 |> maybe_put_email_address(user, opts[:for])
323 end
324
325 defp username_from_nickname(string) when is_binary(string) do
326 hd(String.split(string, "@"))
327 end
328
329 defp username_from_nickname(_), do: nil
330
331 defp maybe_put_follow_requests_count(
332 data,
333 %User{id: user_id} = user,
334 %User{id: user_id}
335 ) do
336 count =
337 User.get_follow_requests(user)
338 |> length()
339
340 data
341 |> Kernel.put_in([:follow_requests_count], count)
342 end
343
344 defp maybe_put_follow_requests_count(data, _, _), do: data
345
346 defp maybe_put_settings(
347 data,
348 %User{id: user_id} = user,
349 %User{id: user_id},
350 _opts
351 ) do
352 data
353 |> Kernel.put_in([:source, :privacy], user.default_scope)
354 |> Kernel.put_in([:source, :pleroma, :show_role], user.show_role)
355 |> Kernel.put_in([:source, :pleroma, :no_rich_text], user.no_rich_text)
356 end
357
358 defp maybe_put_settings(data, _, _, _), do: data
359
360 defp maybe_put_settings_store(data, %User{} = user, %User{}, %{
361 with_pleroma_settings: true
362 }) do
363 data
364 |> Kernel.put_in([:pleroma, :settings_store], user.pleroma_settings_store)
365 end
366
367 defp maybe_put_settings_store(data, _, _, _), do: data
368
369 defp maybe_put_role(data, %User{show_role: true} = user, _) do
370 data
371 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
372 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
373 end
374
375 defp maybe_put_role(data, %User{id: user_id} = user, %User{id: user_id}) do
376 data
377 |> Kernel.put_in([:pleroma, :is_admin], user.is_admin)
378 |> Kernel.put_in([:pleroma, :is_moderator], user.is_moderator)
379 end
380
381 defp maybe_put_role(data, _, _), do: data
382
383 defp maybe_put_notification_settings(data, %User{id: user_id} = user, %User{id: user_id}) do
384 Kernel.put_in(
385 data,
386 [:pleroma, :notification_settings],
387 Map.from_struct(user.notification_settings)
388 )
389 end
390
391 defp maybe_put_notification_settings(data, _, _), do: data
392
393 defp maybe_put_allow_following_move(data, %User{id: user_id} = user, %User{id: user_id}) do
394 Kernel.put_in(data, [:pleroma, :allow_following_move], user.allow_following_move)
395 end
396
397 defp maybe_put_allow_following_move(data, _, _), do: data
398
399 defp maybe_put_activation_status(data, user, %User{is_admin: true}) do
400 Kernel.put_in(data, [:pleroma, :deactivated], !user.is_active)
401 end
402
403 defp maybe_put_activation_status(data, _, _), do: data
404
405 defp maybe_put_unread_conversation_count(data, %User{id: user_id} = user, %User{id: user_id}) do
406 data
407 |> Kernel.put_in(
408 [:pleroma, :unread_conversation_count],
409 Pleroma.Conversation.Participation.unread_count(user)
410 )
411 end
412
413 defp maybe_put_unread_conversation_count(data, _, _), do: data
414
415 defp maybe_put_unread_notification_count(data, %User{id: user_id}, %User{id: user_id} = user) do
416 Kernel.put_in(
417 data,
418 [:pleroma, :unread_notifications_count],
419 Pleroma.Notification.unread_notifications_count(user)
420 )
421 end
422
423 defp maybe_put_unread_notification_count(data, _, _), do: data
424
425 defp maybe_put_email_address(data, %User{id: user_id}, %User{id: user_id} = user) do
426 Kernel.put_in(
427 data,
428 [:pleroma, :email],
429 user.email
430 )
431 end
432
433 defp maybe_put_email_address(data, _, _), do: data
434
435 defp image_url(%{"url" => [%{"href" => href} | _]}), do: href
436 defp image_url(_), do: nil
437 end