SideEffects: Move streaming of chats to after the transaction.
[akkoma] / lib / pleroma / web / activity_pub / activity_pub.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.ActivityPub.ActivityPub do
6 alias Pleroma.Activity
7 alias Pleroma.Activity.Ir.Topics
8 alias Pleroma.Config
9 alias Pleroma.Constants
10 alias Pleroma.Conversation
11 alias Pleroma.Conversation.Participation
12 alias Pleroma.Maps
13 alias Pleroma.Notification
14 alias Pleroma.Object
15 alias Pleroma.Object.Containment
16 alias Pleroma.Object.Fetcher
17 alias Pleroma.Pagination
18 alias Pleroma.Repo
19 alias Pleroma.Upload
20 alias Pleroma.User
21 alias Pleroma.Web.ActivityPub.MRF
22 alias Pleroma.Web.ActivityPub.Transmogrifier
23 alias Pleroma.Web.Streamer
24 alias Pleroma.Web.WebFinger
25 alias Pleroma.Workers.BackgroundWorker
26
27 import Ecto.Query
28 import Pleroma.Web.ActivityPub.Utils
29 import Pleroma.Web.ActivityPub.Visibility
30
31 require Logger
32 require Pleroma.Constants
33
34 # For Announce activities, we filter the recipients based on following status for any actors
35 # that match actual users. See issue #164 for more information about why this is necessary.
36 defp get_recipients(%{"type" => "Announce"} = data) do
37 to = Map.get(data, "to", [])
38 cc = Map.get(data, "cc", [])
39 bcc = Map.get(data, "bcc", [])
40 actor = User.get_cached_by_ap_id(data["actor"])
41
42 recipients =
43 Enum.filter(Enum.concat([to, cc, bcc]), fn recipient ->
44 case User.get_cached_by_ap_id(recipient) do
45 nil -> true
46 user -> User.following?(user, actor)
47 end
48 end)
49
50 {recipients, to, cc}
51 end
52
53 defp get_recipients(%{"type" => "Create"} = data) do
54 to = Map.get(data, "to", [])
55 cc = Map.get(data, "cc", [])
56 bcc = Map.get(data, "bcc", [])
57 actor = Map.get(data, "actor", [])
58 recipients = [to, cc, bcc, [actor]] |> Enum.concat() |> Enum.uniq()
59 {recipients, to, cc}
60 end
61
62 defp get_recipients(data) do
63 to = Map.get(data, "to", [])
64 cc = Map.get(data, "cc", [])
65 bcc = Map.get(data, "bcc", [])
66 recipients = Enum.concat([to, cc, bcc])
67 {recipients, to, cc}
68 end
69
70 defp check_actor_is_active(actor) do
71 if not is_nil(actor) do
72 with user <- User.get_cached_by_ap_id(actor),
73 false <- user.deactivated do
74 true
75 else
76 _e -> false
77 end
78 else
79 true
80 end
81 end
82
83 defp check_remote_limit(%{"object" => %{"content" => content}}) when not is_nil(content) do
84 limit = Config.get([:instance, :remote_limit])
85 String.length(content) <= limit
86 end
87
88 defp check_remote_limit(_), do: true
89
90 def increase_note_count_if_public(actor, object) do
91 if is_public?(object), do: User.increase_note_count(actor), else: {:ok, actor}
92 end
93
94 def decrease_note_count_if_public(actor, object) do
95 if is_public?(object), do: User.decrease_note_count(actor), else: {:ok, actor}
96 end
97
98 def increase_replies_count_if_reply(%{
99 "object" => %{"inReplyTo" => reply_ap_id} = object,
100 "type" => "Create"
101 }) do
102 if is_public?(object) do
103 Object.increase_replies_count(reply_ap_id)
104 end
105 end
106
107 def increase_replies_count_if_reply(_create_data), do: :noop
108
109 def decrease_replies_count_if_reply(%Object{
110 data: %{"inReplyTo" => reply_ap_id} = object
111 }) do
112 if is_public?(object) do
113 Object.decrease_replies_count(reply_ap_id)
114 end
115 end
116
117 def decrease_replies_count_if_reply(_object), do: :noop
118
119 def increase_poll_votes_if_vote(%{
120 "object" => %{"inReplyTo" => reply_ap_id, "name" => name},
121 "type" => "Create",
122 "actor" => actor
123 }) do
124 Object.increase_vote_count(reply_ap_id, name, actor)
125 end
126
127 def increase_poll_votes_if_vote(_create_data), do: :noop
128
129 @object_types ["ChatMessage"]
130 @spec persist(map(), keyword()) :: {:ok, Activity.t() | Object.t()}
131 def persist(%{"type" => type} = object, meta) when type in @object_types do
132 with {:ok, object} <- Object.create(object) do
133 {:ok, object, meta}
134 end
135 end
136
137 def persist(object, meta) do
138 with local <- Keyword.fetch!(meta, :local),
139 {recipients, _, _} <- get_recipients(object),
140 {:ok, activity} <-
141 Repo.insert(%Activity{
142 data: object,
143 local: local,
144 recipients: recipients,
145 actor: object["actor"]
146 }) do
147 {:ok, activity, meta}
148 end
149 end
150
151 @spec insert(map(), boolean(), boolean(), boolean()) :: {:ok, Activity.t()} | {:error, any()}
152 def insert(map, local \\ true, fake \\ false, bypass_actor_check \\ false) when is_map(map) do
153 with nil <- Activity.normalize(map),
154 map <- lazy_put_activity_defaults(map, fake),
155 true <- bypass_actor_check || check_actor_is_active(map["actor"]),
156 {_, true} <- {:remote_limit_error, check_remote_limit(map)},
157 {:ok, map} <- MRF.filter(map),
158 {recipients, _, _} = get_recipients(map),
159 {:fake, false, map, recipients} <- {:fake, fake, map, recipients},
160 {:containment, :ok} <- {:containment, Containment.contain_child(map)},
161 {:ok, map, object} <- insert_full_object(map) do
162 {:ok, activity} =
163 Repo.insert(%Activity{
164 data: map,
165 local: local,
166 actor: map["actor"],
167 recipients: recipients
168 })
169
170 # Splice in the child object if we have one.
171 activity = Maps.put_if_present(activity, :object, object)
172
173 BackgroundWorker.enqueue("fetch_data_for_activity", %{"activity_id" => activity.id})
174
175 {:ok, activity}
176 else
177 %Activity{} = activity ->
178 {:ok, activity}
179
180 {:fake, true, map, recipients} ->
181 activity = %Activity{
182 data: map,
183 local: local,
184 actor: map["actor"],
185 recipients: recipients,
186 id: "pleroma:fakeid"
187 }
188
189 Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
190 {:ok, activity}
191
192 error ->
193 {:error, error}
194 end
195 end
196
197 def notify_and_stream(activity) do
198 Notification.create_notifications(activity)
199
200 conversation = create_or_bump_conversation(activity, activity.actor)
201 participations = get_participations(conversation)
202 stream_out(activity)
203 stream_out_participations(participations)
204 end
205
206 defp create_or_bump_conversation(activity, actor) do
207 with {:ok, conversation} <- Conversation.create_or_bump_for(activity),
208 %User{} = user <- User.get_cached_by_ap_id(actor),
209 Participation.mark_as_read(user, conversation) do
210 {:ok, conversation}
211 end
212 end
213
214 defp get_participations({:ok, conversation}) do
215 conversation
216 |> Repo.preload(:participations, force: true)
217 |> Map.get(:participations)
218 end
219
220 defp get_participations(_), do: []
221
222 def stream_out_participations(participations) do
223 participations =
224 participations
225 |> Repo.preload(:user)
226
227 Streamer.stream("participation", participations)
228 end
229
230 def stream_out_participations(%Object{data: %{"context" => context}}, user) do
231 with %Conversation{} = conversation <- Conversation.get_for_ap_id(context),
232 conversation = Repo.preload(conversation, :participations),
233 last_activity_id =
234 fetch_latest_activity_id_for_context(conversation.ap_id, %{
235 "user" => user,
236 "blocking_user" => user
237 }) do
238 if last_activity_id do
239 stream_out_participations(conversation.participations)
240 end
241 end
242 end
243
244 def stream_out_participations(_, _), do: :noop
245
246 def stream_out(%Activity{data: %{"type" => data_type}} = activity)
247 when data_type in ["Create", "Announce", "Delete"] do
248 activity
249 |> Topics.get_activity_topics()
250 |> Streamer.stream(activity)
251 end
252
253 def stream_out(_activity) do
254 :noop
255 end
256
257 @spec create(map(), boolean()) :: {:ok, Activity.t()} | {:error, any()}
258 def create(params, fake \\ false) do
259 with {:ok, result} <- Repo.transaction(fn -> do_create(params, fake) end) do
260 result
261 end
262 end
263
264 defp do_create(%{to: to, actor: actor, context: context, object: object} = params, fake) do
265 additional = params[:additional] || %{}
266 # only accept false as false value
267 local = !(params[:local] == false)
268 published = params[:published]
269 quick_insert? = Config.get([:env]) == :benchmark
270
271 with create_data <-
272 make_create_data(
273 %{to: to, actor: actor, published: published, context: context, object: object},
274 additional
275 ),
276 {:ok, activity} <- insert(create_data, local, fake),
277 {:fake, false, activity} <- {:fake, fake, activity},
278 _ <- increase_replies_count_if_reply(create_data),
279 _ <- increase_poll_votes_if_vote(create_data),
280 {:quick_insert, false, activity} <- {:quick_insert, quick_insert?, activity},
281 {:ok, _actor} <- increase_note_count_if_public(actor, activity),
282 _ <- notify_and_stream(activity),
283 :ok <- maybe_federate(activity) do
284 {:ok, activity}
285 else
286 {:quick_insert, true, activity} ->
287 {:ok, activity}
288
289 {:fake, true, activity} ->
290 {:ok, activity}
291
292 {:error, message} ->
293 Repo.rollback(message)
294 end
295 end
296
297 @spec listen(map()) :: {:ok, Activity.t()} | {:error, any()}
298 def listen(%{to: to, actor: actor, context: context, object: object} = params) do
299 additional = params[:additional] || %{}
300 # only accept false as false value
301 local = !(params[:local] == false)
302 published = params[:published]
303
304 with listen_data <-
305 make_listen_data(
306 %{to: to, actor: actor, published: published, context: context, object: object},
307 additional
308 ),
309 {:ok, activity} <- insert(listen_data, local),
310 _ <- notify_and_stream(activity),
311 :ok <- maybe_federate(activity) do
312 {:ok, activity}
313 end
314 end
315
316 @spec accept(map()) :: {:ok, Activity.t()} | {:error, any()}
317 def accept(params) do
318 accept_or_reject("Accept", params)
319 end
320
321 @spec reject(map()) :: {:ok, Activity.t()} | {:error, any()}
322 def reject(params) do
323 accept_or_reject("Reject", params)
324 end
325
326 @spec accept_or_reject(String.t(), map()) :: {:ok, Activity.t()} | {:error, any()}
327 def accept_or_reject(type, %{to: to, actor: actor, object: object} = params) do
328 local = Map.get(params, :local, true)
329 activity_id = Map.get(params, :activity_id, nil)
330
331 with data <-
332 %{"to" => to, "type" => type, "actor" => actor.ap_id, "object" => object}
333 |> Maps.put_if_present("id", activity_id),
334 {:ok, activity} <- insert(data, local),
335 _ <- notify_and_stream(activity),
336 :ok <- maybe_federate(activity) do
337 {:ok, activity}
338 end
339 end
340
341 @spec update(map()) :: {:ok, Activity.t()} | {:error, any()}
342 def update(%{to: to, cc: cc, actor: actor, object: object} = params) do
343 local = !(params[:local] == false)
344 activity_id = params[:activity_id]
345
346 with data <- %{
347 "to" => to,
348 "cc" => cc,
349 "type" => "Update",
350 "actor" => actor,
351 "object" => object
352 },
353 data <- Maps.put_if_present(data, "id", activity_id),
354 {:ok, activity} <- insert(data, local),
355 _ <- notify_and_stream(activity),
356 :ok <- maybe_federate(activity) do
357 {:ok, activity}
358 end
359 end
360
361 @spec follow(User.t(), User.t(), String.t() | nil, boolean(), keyword()) ::
362 {:ok, Activity.t()} | {:error, any()}
363 def follow(follower, followed, activity_id \\ nil, local \\ true, opts \\ []) do
364 with {:ok, result} <-
365 Repo.transaction(fn -> do_follow(follower, followed, activity_id, local, opts) end) do
366 result
367 end
368 end
369
370 defp do_follow(follower, followed, activity_id, local, opts) do
371 skip_notify_and_stream = Keyword.get(opts, :skip_notify_and_stream, false)
372
373 with data <- make_follow_data(follower, followed, activity_id),
374 {:ok, activity} <- insert(data, local),
375 _ <- skip_notify_and_stream || notify_and_stream(activity),
376 :ok <- maybe_federate(activity) do
377 {:ok, activity}
378 else
379 {:error, error} -> Repo.rollback(error)
380 end
381 end
382
383 @spec unfollow(User.t(), User.t(), String.t() | nil, boolean()) ::
384 {:ok, Activity.t()} | nil | {:error, any()}
385 def unfollow(follower, followed, activity_id \\ nil, local \\ true) do
386 with {:ok, result} <-
387 Repo.transaction(fn -> do_unfollow(follower, followed, activity_id, local) end) do
388 result
389 end
390 end
391
392 defp do_unfollow(follower, followed, activity_id, local) do
393 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
394 {:ok, follow_activity} <- update_follow_state(follow_activity, "cancelled"),
395 unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
396 {:ok, activity} <- insert(unfollow_data, local),
397 _ <- notify_and_stream(activity),
398 :ok <- maybe_federate(activity) do
399 {:ok, activity}
400 else
401 nil -> nil
402 {:error, error} -> Repo.rollback(error)
403 end
404 end
405
406 @spec block(User.t(), User.t(), String.t() | nil, boolean()) ::
407 {:ok, Activity.t()} | {:error, any()}
408 def block(blocker, blocked, activity_id \\ nil, local \\ true) do
409 with {:ok, result} <-
410 Repo.transaction(fn -> do_block(blocker, blocked, activity_id, local) end) do
411 result
412 end
413 end
414
415 defp do_block(blocker, blocked, activity_id, local) do
416 unfollow_blocked = Config.get([:activitypub, :unfollow_blocked])
417
418 if unfollow_blocked do
419 follow_activity = fetch_latest_follow(blocker, blocked)
420 if follow_activity, do: unfollow(blocker, blocked, nil, local)
421 end
422
423 with block_data <- make_block_data(blocker, blocked, activity_id),
424 {:ok, activity} <- insert(block_data, local),
425 _ <- notify_and_stream(activity),
426 :ok <- maybe_federate(activity) do
427 {:ok, activity}
428 else
429 {:error, error} -> Repo.rollback(error)
430 end
431 end
432
433 @spec flag(map()) :: {:ok, Activity.t()} | {:error, any()}
434 def flag(
435 %{
436 actor: actor,
437 context: _context,
438 account: account,
439 statuses: statuses,
440 content: content
441 } = params
442 ) do
443 # only accept false as false value
444 local = !(params[:local] == false)
445 forward = !(params[:forward] == false)
446
447 additional = params[:additional] || %{}
448
449 additional =
450 if forward do
451 Map.merge(additional, %{"to" => [], "cc" => [account.ap_id]})
452 else
453 Map.merge(additional, %{"to" => [], "cc" => []})
454 end
455
456 with flag_data <- make_flag_data(params, additional),
457 {:ok, activity} <- insert(flag_data, local),
458 {:ok, stripped_activity} <- strip_report_status_data(activity),
459 _ <- notify_and_stream(activity),
460 :ok <- maybe_federate(stripped_activity) do
461 User.all_superusers()
462 |> Enum.filter(fn user -> not is_nil(user.email) end)
463 |> Enum.each(fn superuser ->
464 superuser
465 |> Pleroma.Emails.AdminEmail.report(actor, account, statuses, content)
466 |> Pleroma.Emails.Mailer.deliver_async()
467 end)
468
469 {:ok, activity}
470 end
471 end
472
473 @spec move(User.t(), User.t(), boolean()) :: {:ok, Activity.t()} | {:error, any()}
474 def move(%User{} = origin, %User{} = target, local \\ true) do
475 params = %{
476 "type" => "Move",
477 "actor" => origin.ap_id,
478 "object" => origin.ap_id,
479 "target" => target.ap_id
480 }
481
482 with true <- origin.ap_id in target.also_known_as,
483 {:ok, activity} <- insert(params, local),
484 _ <- notify_and_stream(activity) do
485 maybe_federate(activity)
486
487 BackgroundWorker.enqueue("move_following", %{
488 "origin_id" => origin.id,
489 "target_id" => target.id
490 })
491
492 {:ok, activity}
493 else
494 false -> {:error, "Target account must have the origin in `alsoKnownAs`"}
495 err -> err
496 end
497 end
498
499 def fetch_activities_for_context_query(context, opts) do
500 public = [Constants.as_public()]
501
502 recipients =
503 if opts["user"],
504 do: [opts["user"].ap_id | User.following(opts["user"])] ++ public,
505 else: public
506
507 from(activity in Activity)
508 |> maybe_preload_objects(opts)
509 |> maybe_preload_bookmarks(opts)
510 |> maybe_set_thread_muted_field(opts)
511 |> restrict_blocked(opts)
512 |> restrict_recipients(recipients, opts["user"])
513 |> where(
514 [activity],
515 fragment(
516 "?->>'type' = ? and ?->>'context' = ?",
517 activity.data,
518 "Create",
519 activity.data,
520 ^context
521 )
522 )
523 |> exclude_poll_votes(opts)
524 |> exclude_id(opts)
525 |> order_by([activity], desc: activity.id)
526 end
527
528 @spec fetch_activities_for_context(String.t(), keyword() | map()) :: [Activity.t()]
529 def fetch_activities_for_context(context, opts \\ %{}) do
530 context
531 |> fetch_activities_for_context_query(opts)
532 |> Repo.all()
533 end
534
535 @spec fetch_latest_activity_id_for_context(String.t(), keyword() | map()) ::
536 FlakeId.Ecto.CompatType.t() | nil
537 def fetch_latest_activity_id_for_context(context, opts \\ %{}) do
538 context
539 |> fetch_activities_for_context_query(Map.merge(%{"skip_preload" => true}, opts))
540 |> limit(1)
541 |> select([a], a.id)
542 |> Repo.one()
543 end
544
545 @spec fetch_public_or_unlisted_activities(map(), Pagination.type()) :: [Activity.t()]
546 def fetch_public_or_unlisted_activities(opts \\ %{}, pagination \\ :keyset) do
547 opts = Map.drop(opts, ["user"])
548
549 query = fetch_activities_query([Constants.as_public()], opts)
550
551 query =
552 if opts["restrict_unlisted"] do
553 restrict_unlisted(query)
554 else
555 query
556 end
557
558 Pagination.fetch_paginated(query, opts, pagination)
559 end
560
561 @spec fetch_public_activities(map(), Pagination.type()) :: [Activity.t()]
562 def fetch_public_activities(opts \\ %{}, pagination \\ :keyset) do
563 opts
564 |> Map.put("restrict_unlisted", true)
565 |> fetch_public_or_unlisted_activities(pagination)
566 end
567
568 @valid_visibilities ~w[direct unlisted public private]
569
570 defp restrict_visibility(query, %{visibility: visibility})
571 when is_list(visibility) do
572 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
573 query =
574 from(
575 a in query,
576 where:
577 fragment(
578 "activity_visibility(?, ?, ?) = ANY (?)",
579 a.actor,
580 a.recipients,
581 a.data,
582 ^visibility
583 )
584 )
585
586 query
587 else
588 Logger.error("Could not restrict visibility to #{visibility}")
589 end
590 end
591
592 defp restrict_visibility(query, %{visibility: visibility})
593 when visibility in @valid_visibilities do
594 from(
595 a in query,
596 where:
597 fragment("activity_visibility(?, ?, ?) = ?", a.actor, a.recipients, a.data, ^visibility)
598 )
599 end
600
601 defp restrict_visibility(_query, %{visibility: visibility})
602 when visibility not in @valid_visibilities do
603 Logger.error("Could not restrict visibility to #{visibility}")
604 end
605
606 defp restrict_visibility(query, _visibility), do: query
607
608 defp exclude_visibility(query, %{"exclude_visibilities" => visibility})
609 when is_list(visibility) do
610 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
611 from(
612 a in query,
613 where:
614 not fragment(
615 "activity_visibility(?, ?, ?) = ANY (?)",
616 a.actor,
617 a.recipients,
618 a.data,
619 ^visibility
620 )
621 )
622 else
623 Logger.error("Could not exclude visibility to #{visibility}")
624 query
625 end
626 end
627
628 defp exclude_visibility(query, %{"exclude_visibilities" => visibility})
629 when visibility in @valid_visibilities do
630 from(
631 a in query,
632 where:
633 not fragment(
634 "activity_visibility(?, ?, ?) = ?",
635 a.actor,
636 a.recipients,
637 a.data,
638 ^visibility
639 )
640 )
641 end
642
643 defp exclude_visibility(query, %{"exclude_visibilities" => visibility})
644 when visibility not in [nil | @valid_visibilities] do
645 Logger.error("Could not exclude visibility to #{visibility}")
646 query
647 end
648
649 defp exclude_visibility(query, _visibility), do: query
650
651 defp restrict_thread_visibility(query, _, %{skip_thread_containment: true} = _),
652 do: query
653
654 defp restrict_thread_visibility(
655 query,
656 %{"user" => %User{skip_thread_containment: true}},
657 _
658 ),
659 do: query
660
661 defp restrict_thread_visibility(query, %{"user" => %User{ap_id: ap_id}}, _) do
662 from(
663 a in query,
664 where: fragment("thread_visibility(?, (?)->>'id') = true", ^ap_id, a.data)
665 )
666 end
667
668 defp restrict_thread_visibility(query, _, _), do: query
669
670 def fetch_user_abstract_activities(user, reading_user, params \\ %{}) do
671 params =
672 params
673 |> Map.put("user", reading_user)
674 |> Map.put("actor_id", user.ap_id)
675
676 recipients =
677 user_activities_recipients(%{
678 "godmode" => params["godmode"],
679 "reading_user" => reading_user
680 })
681
682 fetch_activities(recipients, params)
683 |> Enum.reverse()
684 end
685
686 def fetch_user_activities(user, reading_user, params \\ %{}) do
687 params =
688 params
689 |> Map.put("type", ["Create", "Announce"])
690 |> Map.put("user", reading_user)
691 |> Map.put("actor_id", user.ap_id)
692 |> Map.put("pinned_activity_ids", user.pinned_activities)
693
694 params =
695 if User.blocks?(reading_user, user) do
696 params
697 else
698 params
699 |> Map.put("blocking_user", reading_user)
700 |> Map.put("muting_user", reading_user)
701 end
702
703 recipients =
704 user_activities_recipients(%{
705 "godmode" => params["godmode"],
706 "reading_user" => reading_user
707 })
708
709 fetch_activities(recipients, params)
710 |> Enum.reverse()
711 end
712
713 def fetch_statuses(reading_user, params) do
714 params =
715 params
716 |> Map.put("type", ["Create", "Announce"])
717
718 recipients =
719 user_activities_recipients(%{
720 "godmode" => params["godmode"],
721 "reading_user" => reading_user
722 })
723
724 fetch_activities(recipients, params, :offset)
725 |> Enum.reverse()
726 end
727
728 defp user_activities_recipients(%{"godmode" => true}) do
729 []
730 end
731
732 defp user_activities_recipients(%{"reading_user" => reading_user}) do
733 if reading_user do
734 [Constants.as_public()] ++ [reading_user.ap_id | User.following(reading_user)]
735 else
736 [Constants.as_public()]
737 end
738 end
739
740 defp restrict_since(query, %{"since_id" => ""}), do: query
741
742 defp restrict_since(query, %{"since_id" => since_id}) do
743 from(activity in query, where: activity.id > ^since_id)
744 end
745
746 defp restrict_since(query, _), do: query
747
748 defp restrict_tag_reject(_query, %{"tag_reject" => _tag_reject, "skip_preload" => true}) do
749 raise "Can't use the child object without preloading!"
750 end
751
752 defp restrict_tag_reject(query, %{"tag_reject" => tag_reject})
753 when is_list(tag_reject) and tag_reject != [] do
754 from(
755 [_activity, object] in query,
756 where: fragment("not (?)->'tag' \\?| (?)", object.data, ^tag_reject)
757 )
758 end
759
760 defp restrict_tag_reject(query, _), do: query
761
762 defp restrict_tag_all(_query, %{"tag_all" => _tag_all, "skip_preload" => true}) do
763 raise "Can't use the child object without preloading!"
764 end
765
766 defp restrict_tag_all(query, %{"tag_all" => tag_all})
767 when is_list(tag_all) and tag_all != [] do
768 from(
769 [_activity, object] in query,
770 where: fragment("(?)->'tag' \\?& (?)", object.data, ^tag_all)
771 )
772 end
773
774 defp restrict_tag_all(query, _), do: query
775
776 defp restrict_tag(_query, %{"tag" => _tag, "skip_preload" => true}) do
777 raise "Can't use the child object without preloading!"
778 end
779
780 defp restrict_tag(query, %{"tag" => tag}) when is_list(tag) do
781 from(
782 [_activity, object] in query,
783 where: fragment("(?)->'tag' \\?| (?)", object.data, ^tag)
784 )
785 end
786
787 defp restrict_tag(query, %{"tag" => tag}) when is_binary(tag) do
788 from(
789 [_activity, object] in query,
790 where: fragment("(?)->'tag' \\? (?)", object.data, ^tag)
791 )
792 end
793
794 defp restrict_tag(query, _), do: query
795
796 defp restrict_recipients(query, [], _user), do: query
797
798 defp restrict_recipients(query, recipients, nil) do
799 from(activity in query, where: fragment("? && ?", ^recipients, activity.recipients))
800 end
801
802 defp restrict_recipients(query, recipients, user) do
803 from(
804 activity in query,
805 where: fragment("? && ?", ^recipients, activity.recipients),
806 or_where: activity.actor == ^user.ap_id
807 )
808 end
809
810 defp restrict_local(query, %{"local_only" => true}) do
811 from(activity in query, where: activity.local == true)
812 end
813
814 defp restrict_local(query, _), do: query
815
816 defp restrict_actor(query, %{"actor_id" => actor_id}) do
817 from(activity in query, where: activity.actor == ^actor_id)
818 end
819
820 defp restrict_actor(query, _), do: query
821
822 defp restrict_type(query, %{"type" => type}) when is_binary(type) do
823 from(activity in query, where: fragment("?->>'type' = ?", activity.data, ^type))
824 end
825
826 defp restrict_type(query, %{"type" => type}) do
827 from(activity in query, where: fragment("?->>'type' = ANY(?)", activity.data, ^type))
828 end
829
830 defp restrict_type(query, _), do: query
831
832 defp restrict_state(query, %{"state" => state}) do
833 from(activity in query, where: fragment("?->>'state' = ?", activity.data, ^state))
834 end
835
836 defp restrict_state(query, _), do: query
837
838 defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
839 from(
840 [_activity, object] in query,
841 where: fragment("(?)->'likes' \\? (?)", object.data, ^ap_id)
842 )
843 end
844
845 defp restrict_favorited_by(query, _), do: query
846
847 defp restrict_media(_query, %{"only_media" => _val, "skip_preload" => true}) do
848 raise "Can't use the child object without preloading!"
849 end
850
851 defp restrict_media(query, %{"only_media" => val}) when val in [true, "true", "1"] do
852 from(
853 [_activity, object] in query,
854 where: fragment("not (?)->'attachment' = (?)", object.data, ^[])
855 )
856 end
857
858 defp restrict_media(query, _), do: query
859
860 defp restrict_replies(query, %{"exclude_replies" => val}) when val in [true, "true", "1"] do
861 from(
862 [_activity, object] in query,
863 where: fragment("?->>'inReplyTo' is null", object.data)
864 )
865 end
866
867 defp restrict_replies(query, %{
868 "reply_filtering_user" => user,
869 "reply_visibility" => "self"
870 }) do
871 from(
872 [activity, object] in query,
873 where:
874 fragment(
875 "?->>'inReplyTo' is null OR ? = ANY(?)",
876 object.data,
877 ^user.ap_id,
878 activity.recipients
879 )
880 )
881 end
882
883 defp restrict_replies(query, %{
884 "reply_filtering_user" => user,
885 "reply_visibility" => "following"
886 }) do
887 from(
888 [activity, object] in query,
889 where:
890 fragment(
891 "?->>'inReplyTo' is null OR ? && array_remove(?, ?) OR ? = ?",
892 object.data,
893 ^[user.ap_id | User.get_cached_user_friends_ap_ids(user)],
894 activity.recipients,
895 activity.actor,
896 activity.actor,
897 ^user.ap_id
898 )
899 )
900 end
901
902 defp restrict_replies(query, _), do: query
903
904 defp restrict_reblogs(query, %{"exclude_reblogs" => val}) when val in [true, "true", "1"] do
905 from(activity in query, where: fragment("?->>'type' != 'Announce'", activity.data))
906 end
907
908 defp restrict_reblogs(query, _), do: query
909
910 defp restrict_muted(query, %{"with_muted" => val}) when val in [true, "true", "1"], do: query
911
912 defp restrict_muted(query, %{"muting_user" => %User{} = user} = opts) do
913 mutes = opts["muted_users_ap_ids"] || User.muted_users_ap_ids(user)
914
915 query =
916 from([activity] in query,
917 where: fragment("not (? = ANY(?))", activity.actor, ^mutes),
918 where: fragment("not (?->'to' \\?| ?)", activity.data, ^mutes)
919 )
920
921 unless opts["skip_preload"] do
922 from([thread_mute: tm] in query, where: is_nil(tm.user_id))
923 else
924 query
925 end
926 end
927
928 defp restrict_muted(query, _), do: query
929
930 defp restrict_blocked(query, %{"blocking_user" => %User{} = user} = opts) do
931 blocked_ap_ids = opts["blocked_users_ap_ids"] || User.blocked_users_ap_ids(user)
932 domain_blocks = user.domain_blocks || []
933
934 following_ap_ids = User.get_friends_ap_ids(user)
935
936 query =
937 if has_named_binding?(query, :object), do: query, else: Activity.with_joined_object(query)
938
939 from(
940 [activity, object: o] in query,
941 where: fragment("not (? = ANY(?))", activity.actor, ^blocked_ap_ids),
942 where: fragment("not (? && ?)", activity.recipients, ^blocked_ap_ids),
943 where:
944 fragment(
945 "recipients_contain_blocked_domains(?, ?) = false",
946 activity.recipients,
947 ^domain_blocks
948 ),
949 where:
950 fragment(
951 "not (?->>'type' = 'Announce' and ?->'to' \\?| ?)",
952 activity.data,
953 activity.data,
954 ^blocked_ap_ids
955 ),
956 where:
957 fragment(
958 "(not (split_part(?, '/', 3) = ANY(?))) or ? = ANY(?)",
959 activity.actor,
960 ^domain_blocks,
961 activity.actor,
962 ^following_ap_ids
963 ),
964 where:
965 fragment(
966 "(not (split_part(?->>'actor', '/', 3) = ANY(?))) or (?->>'actor') = ANY(?)",
967 o.data,
968 ^domain_blocks,
969 o.data,
970 ^following_ap_ids
971 )
972 )
973 end
974
975 defp restrict_blocked(query, _), do: query
976
977 defp restrict_unlisted(query) do
978 from(
979 activity in query,
980 where:
981 fragment(
982 "not (coalesce(?->'cc', '{}'::jsonb) \\?| ?)",
983 activity.data,
984 ^[Constants.as_public()]
985 )
986 )
987 end
988
989 # TODO: when all endpoints migrated to OpenAPI compare `pinned` with `true` (boolean) only,
990 # the same for `restrict_media/2`, `restrict_replies/2`, 'restrict_reblogs/2'
991 # and `restrict_muted/2`
992
993 defp restrict_pinned(query, %{"pinned" => pinned, "pinned_activity_ids" => ids})
994 when pinned in [true, "true", "1"] do
995 from(activity in query, where: activity.id in ^ids)
996 end
997
998 defp restrict_pinned(query, _), do: query
999
1000 defp restrict_muted_reblogs(query, %{"muting_user" => %User{} = user} = opts) do
1001 muted_reblogs = opts["reblog_muted_users_ap_ids"] || User.reblog_muted_users_ap_ids(user)
1002
1003 from(
1004 activity in query,
1005 where:
1006 fragment(
1007 "not ( ?->>'type' = 'Announce' and ? = ANY(?))",
1008 activity.data,
1009 activity.actor,
1010 ^muted_reblogs
1011 )
1012 )
1013 end
1014
1015 defp restrict_muted_reblogs(query, _), do: query
1016
1017 defp restrict_instance(query, %{"instance" => instance}) do
1018 users =
1019 from(
1020 u in User,
1021 select: u.ap_id,
1022 where: fragment("? LIKE ?", u.nickname, ^"%@#{instance}")
1023 )
1024 |> Repo.all()
1025
1026 from(activity in query, where: activity.actor in ^users)
1027 end
1028
1029 defp restrict_instance(query, _), do: query
1030
1031 defp exclude_poll_votes(query, %{"include_poll_votes" => true}), do: query
1032
1033 defp exclude_poll_votes(query, _) do
1034 if has_named_binding?(query, :object) do
1035 from([activity, object: o] in query,
1036 where: fragment("not(?->>'type' = ?)", o.data, "Answer")
1037 )
1038 else
1039 query
1040 end
1041 end
1042
1043 defp exclude_chat_messages(query, %{"include_chat_messages" => true}), do: query
1044
1045 defp exclude_chat_messages(query, _) do
1046 if has_named_binding?(query, :object) do
1047 from([activity, object: o] in query,
1048 where: fragment("not(?->>'type' = ?)", o.data, "ChatMessage")
1049 )
1050 else
1051 query
1052 end
1053 end
1054
1055 defp exclude_invisible_actors(query, %{"invisible_actors" => true}), do: query
1056
1057 defp exclude_invisible_actors(query, _opts) do
1058 invisible_ap_ids =
1059 User.Query.build(%{invisible: true, select: [:ap_id]})
1060 |> Repo.all()
1061 |> Enum.map(fn %{ap_id: ap_id} -> ap_id end)
1062
1063 from([activity] in query, where: activity.actor not in ^invisible_ap_ids)
1064 end
1065
1066 defp exclude_id(query, %{"exclude_id" => id}) when is_binary(id) do
1067 from(activity in query, where: activity.id != ^id)
1068 end
1069
1070 defp exclude_id(query, _), do: query
1071
1072 defp maybe_preload_objects(query, %{"skip_preload" => true}), do: query
1073
1074 defp maybe_preload_objects(query, _) do
1075 query
1076 |> Activity.with_preloaded_object()
1077 end
1078
1079 defp maybe_preload_bookmarks(query, %{"skip_preload" => true}), do: query
1080
1081 defp maybe_preload_bookmarks(query, opts) do
1082 query
1083 |> Activity.with_preloaded_bookmark(opts["user"])
1084 end
1085
1086 defp maybe_preload_report_notes(query, %{"preload_report_notes" => true}) do
1087 query
1088 |> Activity.with_preloaded_report_notes()
1089 end
1090
1091 defp maybe_preload_report_notes(query, _), do: query
1092
1093 defp maybe_set_thread_muted_field(query, %{"skip_preload" => true}), do: query
1094
1095 defp maybe_set_thread_muted_field(query, opts) do
1096 query
1097 |> Activity.with_set_thread_muted_field(opts["muting_user"] || opts["user"])
1098 end
1099
1100 defp maybe_order(query, %{order: :desc}) do
1101 query
1102 |> order_by(desc: :id)
1103 end
1104
1105 defp maybe_order(query, %{order: :asc}) do
1106 query
1107 |> order_by(asc: :id)
1108 end
1109
1110 defp maybe_order(query, _), do: query
1111
1112 defp fetch_activities_query_ap_ids_ops(opts) do
1113 source_user = opts["muting_user"]
1114 ap_id_relationships = if source_user, do: [:mute, :reblog_mute], else: []
1115
1116 ap_id_relationships =
1117 ap_id_relationships ++
1118 if opts["blocking_user"] && opts["blocking_user"] == source_user do
1119 [:block]
1120 else
1121 []
1122 end
1123
1124 preloaded_ap_ids = User.outgoing_relationships_ap_ids(source_user, ap_id_relationships)
1125
1126 restrict_blocked_opts = Map.merge(%{"blocked_users_ap_ids" => preloaded_ap_ids[:block]}, opts)
1127 restrict_muted_opts = Map.merge(%{"muted_users_ap_ids" => preloaded_ap_ids[:mute]}, opts)
1128
1129 restrict_muted_reblogs_opts =
1130 Map.merge(%{"reblog_muted_users_ap_ids" => preloaded_ap_ids[:reblog_mute]}, opts)
1131
1132 {restrict_blocked_opts, restrict_muted_opts, restrict_muted_reblogs_opts}
1133 end
1134
1135 def fetch_activities_query(recipients, opts \\ %{}) do
1136 {restrict_blocked_opts, restrict_muted_opts, restrict_muted_reblogs_opts} =
1137 fetch_activities_query_ap_ids_ops(opts)
1138
1139 config = %{
1140 skip_thread_containment: Config.get([:instance, :skip_thread_containment])
1141 }
1142
1143 Activity
1144 |> maybe_preload_objects(opts)
1145 |> maybe_preload_bookmarks(opts)
1146 |> maybe_preload_report_notes(opts)
1147 |> maybe_set_thread_muted_field(opts)
1148 |> maybe_order(opts)
1149 |> restrict_recipients(recipients, opts["user"])
1150 |> restrict_replies(opts)
1151 |> restrict_tag(opts)
1152 |> restrict_tag_reject(opts)
1153 |> restrict_tag_all(opts)
1154 |> restrict_since(opts)
1155 |> restrict_local(opts)
1156 |> restrict_actor(opts)
1157 |> restrict_type(opts)
1158 |> restrict_state(opts)
1159 |> restrict_favorited_by(opts)
1160 |> restrict_blocked(restrict_blocked_opts)
1161 |> restrict_muted(restrict_muted_opts)
1162 |> restrict_media(opts)
1163 |> restrict_visibility(opts)
1164 |> restrict_thread_visibility(opts, config)
1165 |> restrict_reblogs(opts)
1166 |> restrict_pinned(opts)
1167 |> restrict_muted_reblogs(restrict_muted_reblogs_opts)
1168 |> restrict_instance(opts)
1169 |> Activity.restrict_deactivated_users()
1170 |> exclude_poll_votes(opts)
1171 |> exclude_chat_messages(opts)
1172 |> exclude_invisible_actors(opts)
1173 |> exclude_visibility(opts)
1174 end
1175
1176 def fetch_activities(recipients, opts \\ %{}, pagination \\ :keyset) do
1177 list_memberships = Pleroma.List.memberships(opts["user"])
1178
1179 fetch_activities_query(recipients ++ list_memberships, opts)
1180 |> Pagination.fetch_paginated(opts, pagination)
1181 |> Enum.reverse()
1182 |> maybe_update_cc(list_memberships, opts["user"])
1183 end
1184
1185 @doc """
1186 Fetch favorites activities of user with order by sort adds to favorites
1187 """
1188 @spec fetch_favourites(User.t(), map(), Pagination.type()) :: list(Activity.t())
1189 def fetch_favourites(user, params \\ %{}, pagination \\ :keyset) do
1190 user.ap_id
1191 |> Activity.Queries.by_actor()
1192 |> Activity.Queries.by_type("Like")
1193 |> Activity.with_joined_object()
1194 |> Object.with_joined_activity()
1195 |> select([_like, object, activity], %{activity | object: object})
1196 |> order_by([like, _, _], desc_nulls_last: like.id)
1197 |> Pagination.fetch_paginated(
1198 Map.merge(params, %{"skip_order" => true}),
1199 pagination,
1200 :object_activity
1201 )
1202 end
1203
1204 defp maybe_update_cc(activities, list_memberships, %User{ap_id: user_ap_id})
1205 when is_list(list_memberships) and length(list_memberships) > 0 do
1206 Enum.map(activities, fn
1207 %{data: %{"bcc" => bcc}} = activity when is_list(bcc) and length(bcc) > 0 ->
1208 if Enum.any?(bcc, &(&1 in list_memberships)) do
1209 update_in(activity.data["cc"], &[user_ap_id | &1])
1210 else
1211 activity
1212 end
1213
1214 activity ->
1215 activity
1216 end)
1217 end
1218
1219 defp maybe_update_cc(activities, _, _), do: activities
1220
1221 def fetch_activities_bounded_query(query, recipients, recipients_with_public) do
1222 from(activity in query,
1223 where:
1224 fragment("? && ?", activity.recipients, ^recipients) or
1225 (fragment("? && ?", activity.recipients, ^recipients_with_public) and
1226 ^Constants.as_public() in activity.recipients)
1227 )
1228 end
1229
1230 def fetch_activities_bounded(
1231 recipients,
1232 recipients_with_public,
1233 opts \\ %{},
1234 pagination \\ :keyset
1235 ) do
1236 fetch_activities_query([], opts)
1237 |> fetch_activities_bounded_query(recipients, recipients_with_public)
1238 |> Pagination.fetch_paginated(opts, pagination)
1239 |> Enum.reverse()
1240 end
1241
1242 @spec upload(Upload.source(), keyword()) :: {:ok, Object.t()} | {:error, any()}
1243 def upload(file, opts \\ []) do
1244 with {:ok, data} <- Upload.store(file, opts) do
1245 obj_data = Maps.put_if_present(data, "actor", opts[:actor])
1246
1247 Repo.insert(%Object{data: obj_data})
1248 end
1249 end
1250
1251 @spec get_actor_url(any()) :: binary() | nil
1252 defp get_actor_url(url) when is_binary(url), do: url
1253 defp get_actor_url(%{"href" => href}) when is_binary(href), do: href
1254
1255 defp get_actor_url(url) when is_list(url) do
1256 url
1257 |> List.first()
1258 |> get_actor_url()
1259 end
1260
1261 defp get_actor_url(_url), do: nil
1262
1263 defp object_to_user_data(data) do
1264 avatar =
1265 data["icon"]["url"] &&
1266 %{
1267 "type" => "Image",
1268 "url" => [%{"href" => data["icon"]["url"]}]
1269 }
1270
1271 banner =
1272 data["image"]["url"] &&
1273 %{
1274 "type" => "Image",
1275 "url" => [%{"href" => data["image"]["url"]}]
1276 }
1277
1278 fields =
1279 data
1280 |> Map.get("attachment", [])
1281 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
1282 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
1283
1284 emojis =
1285 data
1286 |> Map.get("tag", [])
1287 |> Enum.filter(fn
1288 %{"type" => "Emoji"} -> true
1289 _ -> false
1290 end)
1291 |> Enum.reduce(%{}, fn %{"icon" => %{"url" => url}, "name" => name}, acc ->
1292 Map.put(acc, String.trim(name, ":"), url)
1293 end)
1294
1295 locked = data["manuallyApprovesFollowers"] || false
1296 data = Transmogrifier.maybe_fix_user_object(data)
1297 discoverable = data["discoverable"] || false
1298 invisible = data["invisible"] || false
1299 actor_type = data["type"] || "Person"
1300
1301 public_key =
1302 if is_map(data["publicKey"]) && is_binary(data["publicKey"]["publicKeyPem"]) do
1303 data["publicKey"]["publicKeyPem"]
1304 else
1305 nil
1306 end
1307
1308 shared_inbox =
1309 if is_map(data["endpoints"]) && is_binary(data["endpoints"]["sharedInbox"]) do
1310 data["endpoints"]["sharedInbox"]
1311 else
1312 nil
1313 end
1314
1315 user_data = %{
1316 ap_id: data["id"],
1317 uri: get_actor_url(data["url"]),
1318 ap_enabled: true,
1319 banner: banner,
1320 fields: fields,
1321 emoji: emojis,
1322 locked: locked,
1323 discoverable: discoverable,
1324 invisible: invisible,
1325 avatar: avatar,
1326 name: data["name"],
1327 follower_address: data["followers"],
1328 following_address: data["following"],
1329 bio: data["summary"],
1330 actor_type: actor_type,
1331 also_known_as: Map.get(data, "alsoKnownAs", []),
1332 public_key: public_key,
1333 inbox: data["inbox"],
1334 shared_inbox: shared_inbox
1335 }
1336
1337 # nickname can be nil because of virtual actors
1338 user_data =
1339 if data["preferredUsername"] do
1340 Map.put(
1341 user_data,
1342 :nickname,
1343 "#{data["preferredUsername"]}@#{URI.parse(data["id"]).host}"
1344 )
1345 else
1346 Map.put(user_data, :nickname, nil)
1347 end
1348
1349 {:ok, user_data}
1350 end
1351
1352 def fetch_follow_information_for_user(user) do
1353 with {:ok, following_data} <-
1354 Fetcher.fetch_and_contain_remote_object_from_id(user.following_address),
1355 {:ok, hide_follows} <- collection_private(following_data),
1356 {:ok, followers_data} <-
1357 Fetcher.fetch_and_contain_remote_object_from_id(user.follower_address),
1358 {:ok, hide_followers} <- collection_private(followers_data) do
1359 {:ok,
1360 %{
1361 hide_follows: hide_follows,
1362 follower_count: normalize_counter(followers_data["totalItems"]),
1363 following_count: normalize_counter(following_data["totalItems"]),
1364 hide_followers: hide_followers
1365 }}
1366 else
1367 {:error, _} = e -> e
1368 e -> {:error, e}
1369 end
1370 end
1371
1372 defp normalize_counter(counter) when is_integer(counter), do: counter
1373 defp normalize_counter(_), do: 0
1374
1375 def maybe_update_follow_information(user_data) do
1376 with {:enabled, true} <- {:enabled, Config.get([:instance, :external_user_synchronization])},
1377 {_, true} <- {:user_type_check, user_data[:type] in ["Person", "Service"]},
1378 {_, true} <-
1379 {:collections_available,
1380 !!(user_data[:following_address] && user_data[:follower_address])},
1381 {:ok, info} <-
1382 fetch_follow_information_for_user(user_data) do
1383 info = Map.merge(user_data[:info] || %{}, info)
1384
1385 user_data
1386 |> Map.put(:info, info)
1387 else
1388 {:user_type_check, false} ->
1389 user_data
1390
1391 {:collections_available, false} ->
1392 user_data
1393
1394 {:enabled, false} ->
1395 user_data
1396
1397 e ->
1398 Logger.error(
1399 "Follower/Following counter update for #{user_data.ap_id} failed.\n" <> inspect(e)
1400 )
1401
1402 user_data
1403 end
1404 end
1405
1406 defp collection_private(%{"first" => %{"type" => type}})
1407 when type in ["CollectionPage", "OrderedCollectionPage"],
1408 do: {:ok, false}
1409
1410 defp collection_private(%{"first" => first}) do
1411 with {:ok, %{"type" => type}} when type in ["CollectionPage", "OrderedCollectionPage"] <-
1412 Fetcher.fetch_and_contain_remote_object_from_id(first) do
1413 {:ok, false}
1414 else
1415 {:error, {:ok, %{status: code}}} when code in [401, 403] -> {:ok, true}
1416 {:error, _} = e -> e
1417 e -> {:error, e}
1418 end
1419 end
1420
1421 defp collection_private(_data), do: {:ok, true}
1422
1423 def user_data_from_user_object(data) do
1424 with {:ok, data} <- MRF.filter(data),
1425 {:ok, data} <- object_to_user_data(data) do
1426 {:ok, data}
1427 else
1428 e -> {:error, e}
1429 end
1430 end
1431
1432 def fetch_and_prepare_user_from_ap_id(ap_id) do
1433 with {:ok, data} <- Fetcher.fetch_and_contain_remote_object_from_id(ap_id),
1434 {:ok, data} <- user_data_from_user_object(data),
1435 data <- maybe_update_follow_information(data) do
1436 {:ok, data}
1437 else
1438 {:error, "Object has been deleted"} = e ->
1439 Logger.debug("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
1440 {:error, e}
1441
1442 e ->
1443 Logger.error("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
1444 {:error, e}
1445 end
1446 end
1447
1448 def make_user_from_ap_id(ap_id) do
1449 user = User.get_cached_by_ap_id(ap_id)
1450
1451 if user && !User.ap_enabled?(user) do
1452 Transmogrifier.upgrade_user_from_ap_id(ap_id)
1453 else
1454 with {:ok, data} <- fetch_and_prepare_user_from_ap_id(ap_id) do
1455 if user do
1456 user
1457 |> User.remote_user_changeset(data)
1458 |> User.update_and_set_cache()
1459 else
1460 data
1461 |> User.remote_user_changeset()
1462 |> Repo.insert()
1463 |> User.set_cache()
1464 end
1465 else
1466 e -> {:error, e}
1467 end
1468 end
1469 end
1470
1471 def make_user_from_nickname(nickname) do
1472 with {:ok, %{"ap_id" => ap_id}} when not is_nil(ap_id) <- WebFinger.finger(nickname) do
1473 make_user_from_ap_id(ap_id)
1474 else
1475 _e -> {:error, "No AP id in WebFinger"}
1476 end
1477 end
1478
1479 # filter out broken threads
1480 def contain_broken_threads(%Activity{} = activity, %User{} = user) do
1481 entire_thread_visible_for_user?(activity, user)
1482 end
1483
1484 # do post-processing on a specific activity
1485 def contain_activity(%Activity{} = activity, %User{} = user) do
1486 contain_broken_threads(activity, user)
1487 end
1488
1489 def fetch_direct_messages_query do
1490 Activity
1491 |> restrict_type(%{"type" => "Create"})
1492 |> restrict_visibility(%{visibility: "direct"})
1493 |> order_by([activity], asc: activity.id)
1494 end
1495 end