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