1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ActivityPub do
7 alias Pleroma.Conversation
8 alias Pleroma.Notification
10 alias Pleroma.Object.Fetcher
11 alias Pleroma.Pagination
15 alias Pleroma.Web.ActivityPub.MRF
16 alias Pleroma.Web.ActivityPub.Transmogrifier
17 alias Pleroma.Web.WebFinger
20 import Pleroma.Web.ActivityPub.Utils
21 import Pleroma.Web.ActivityPub.Visibility
25 # For Announce activities, we filter the recipients based on following status for any actors
26 # that match actual users. See issue #164 for more information about why this is necessary.
27 defp get_recipients(%{"type" => "Announce"} = data) do
30 actor = User.get_cached_by_ap_id(data["actor"])
34 |> Enum.filter(fn recipient ->
35 case User.get_cached_by_ap_id(recipient) do
40 User.following?(user, actor)
47 defp get_recipients(%{"type" => "Create"} = data) do
50 actor = data["actor"] || []
51 recipients = (to ++ cc ++ [actor]) |> Enum.uniq()
55 defp get_recipients(data) do
62 defp check_actor_is_active(actor) do
63 if not is_nil(actor) do
64 with user <- User.get_cached_by_ap_id(actor),
65 false <- user.info.deactivated do
75 defp check_remote_limit(%{"object" => %{"content" => content}}) when not is_nil(content) do
76 limit = Pleroma.Config.get([:instance, :remote_limit])
77 String.length(content) <= limit
80 defp check_remote_limit(_), do: true
82 def increase_note_count_if_public(actor, object) do
83 if is_public?(object), do: User.increase_note_count(actor), else: {:ok, actor}
86 def decrease_note_count_if_public(actor, object) do
87 if is_public?(object), do: User.decrease_note_count(actor), else: {:ok, actor}
90 def increase_replies_count_if_reply(%{
91 "object" => %{"inReplyTo" => reply_ap_id} = object,
94 if is_public?(object) do
95 Object.increase_replies_count(reply_ap_id)
99 def increase_replies_count_if_reply(_create_data), do: :noop
101 def decrease_replies_count_if_reply(%Object{
102 data: %{"inReplyTo" => reply_ap_id} = object
104 if is_public?(object) do
105 Object.decrease_replies_count(reply_ap_id)
109 def decrease_replies_count_if_reply(_object), do: :noop
111 def insert(map, local \\ true, fake \\ false) when is_map(map) do
112 with nil <- Activity.normalize(map),
113 map <- lazy_put_activity_defaults(map, fake),
114 :ok <- check_actor_is_active(map["actor"]),
115 {_, true} <- {:remote_limit_error, check_remote_limit(map)},
116 {:ok, map} <- MRF.filter(map),
117 {recipients, _, _} = get_recipients(map),
118 {:fake, false, map, recipients} <- {:fake, fake, map, recipients},
119 {:ok, map, object} <- insert_full_object(map) do
121 Repo.insert(%Activity{
125 recipients: recipients
128 # Splice in the child object if we have one.
130 if !is_nil(object) do
131 Map.put(activity, :object, object)
136 PleromaJobQueue.enqueue(:background, Pleroma.Web.RichMedia.Helpers, [:fetch, activity])
138 Notification.create_notifications(activity)
142 |> Conversation.create_or_bump_for()
143 |> get_participations()
146 stream_out_participations(participations)
149 %Activity{} = activity ->
152 {:fake, true, map, recipients} ->
153 activity = %Activity{
157 recipients: recipients,
161 Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
169 defp get_participations({:ok, %{participations: participations}}), do: participations
170 defp get_participations(_), do: []
172 def stream_out_participations(participations) do
175 |> Repo.preload(:user)
177 Enum.each(participations, fn participation ->
178 Pleroma.Web.Streamer.stream("participation", participation)
182 def stream_out(activity) do
183 public = "https://www.w3.org/ns/activitystreams#Public"
185 if activity.data["type"] in ["Create", "Announce", "Delete"] do
186 Pleroma.Web.Streamer.stream("user", activity)
187 Pleroma.Web.Streamer.stream("list", activity)
189 if Enum.member?(activity.data["to"], public) do
190 Pleroma.Web.Streamer.stream("public", activity)
193 Pleroma.Web.Streamer.stream("public:local", activity)
196 if activity.data["type"] in ["Create"] do
197 object = Object.normalize(activity)
200 |> Map.get("tag", [])
201 |> Enum.filter(fn tag -> is_bitstring(tag) end)
202 |> Enum.each(fn tag -> Pleroma.Web.Streamer.stream("hashtag:" <> tag, activity) end)
204 if object.data["attachment"] != [] do
205 Pleroma.Web.Streamer.stream("public:media", activity)
208 Pleroma.Web.Streamer.stream("public:local:media", activity)
213 # TODO: Write test, replace with visibility test
214 if !Enum.member?(activity.data["cc"] || [], public) &&
217 User.get_cached_by_ap_id(activity.data["actor"]).follower_address
219 do: Pleroma.Web.Streamer.stream("direct", activity)
224 def create(%{to: to, actor: actor, context: context, object: object} = params, fake \\ false) do
225 additional = params[:additional] || %{}
226 # only accept false as false value
227 local = !(params[:local] == false)
228 published = params[:published]
232 %{to: to, actor: actor, published: published, context: context, object: object},
235 {:ok, activity} <- insert(create_data, local, fake),
236 {:fake, false, activity} <- {:fake, fake, activity},
237 _ <- increase_replies_count_if_reply(create_data),
238 # Changing note count prior to enqueuing federation task in order to avoid
239 # race conditions on updating user.info
240 {:ok, _actor} <- increase_note_count_if_public(actor, activity),
241 :ok <- maybe_federate(activity) do
244 {:fake, true, activity} ->
249 def accept(%{to: to, actor: actor, object: object} = params) do
250 # only accept false as false value
251 local = !(params[:local] == false)
253 with data <- %{"to" => to, "type" => "Accept", "actor" => actor.ap_id, "object" => object},
254 {:ok, activity} <- insert(data, local),
255 :ok <- maybe_federate(activity) do
260 def reject(%{to: to, actor: actor, object: object} = params) do
261 # only accept false as false value
262 local = !(params[:local] == false)
264 with data <- %{"to" => to, "type" => "Reject", "actor" => actor.ap_id, "object" => object},
265 {:ok, activity} <- insert(data, local),
266 :ok <- maybe_federate(activity) do
271 def update(%{to: to, cc: cc, actor: actor, object: object} = params) do
272 # only accept false as false value
273 local = !(params[:local] == false)
282 {:ok, activity} <- insert(data, local),
283 :ok <- maybe_federate(activity) do
288 # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
290 %User{ap_id: ap_id} = user,
291 %Object{data: %{"id" => _}} = object,
295 with nil <- get_existing_like(ap_id, object),
296 like_data <- make_like_data(user, object, activity_id),
297 {:ok, activity} <- insert(like_data, local),
298 {:ok, object} <- add_like_to_object(activity, object),
299 :ok <- maybe_federate(activity) do
300 {:ok, activity, object}
302 %Activity{} = activity -> {:ok, activity, object}
303 error -> {:error, error}
313 with %Activity{} = like_activity <- get_existing_like(actor.ap_id, object),
314 unlike_data <- make_unlike_data(actor, like_activity, activity_id),
315 {:ok, unlike_activity} <- insert(unlike_data, local),
316 {:ok, _activity} <- Repo.delete(like_activity),
317 {:ok, object} <- remove_like_from_object(like_activity, object),
318 :ok <- maybe_federate(unlike_activity) do
319 {:ok, unlike_activity, like_activity, object}
326 %User{ap_id: _} = user,
327 %Object{data: %{"id" => _}} = object,
332 with true <- is_public?(object),
333 announce_data <- make_announce_data(user, object, activity_id, public),
334 {:ok, activity} <- insert(announce_data, local),
335 {:ok, object} <- add_announce_to_object(activity, object),
336 :ok <- maybe_federate(activity) do
337 {:ok, activity, object}
339 error -> {:error, error}
349 with %Activity{} = announce_activity <- get_existing_announce(actor.ap_id, object),
350 unannounce_data <- make_unannounce_data(actor, announce_activity, activity_id),
351 {:ok, unannounce_activity} <- insert(unannounce_data, local),
352 :ok <- maybe_federate(unannounce_activity),
353 {:ok, _activity} <- Repo.delete(announce_activity),
354 {:ok, object} <- remove_announce_from_object(announce_activity, object) do
355 {:ok, unannounce_activity, object}
361 def follow(follower, followed, activity_id \\ nil, local \\ true) do
362 with data <- make_follow_data(follower, followed, activity_id),
363 {:ok, activity} <- insert(data, local),
364 :ok <- maybe_federate(activity) do
369 def unfollow(follower, followed, activity_id \\ nil, local \\ true) do
370 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
371 {:ok, follow_activity} <- update_follow_state(follow_activity, "cancelled"),
372 unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
373 {:ok, activity} <- insert(unfollow_data, local),
374 :ok <- maybe_federate(activity) do
379 def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
380 user = User.get_cached_by_ap_id(actor)
381 to = (object.data["to"] || []) ++ (object.data["cc"] || [])
383 with {:ok, object, activity} <- Object.delete(object),
389 "deleted_activity_id" => activity && activity.id
391 {:ok, activity} <- insert(data, local),
392 _ <- decrease_replies_count_if_reply(object),
393 # Changing note count prior to enqueuing federation task in order to avoid
394 # race conditions on updating user.info
395 {:ok, _actor} <- decrease_note_count_if_public(user, object),
396 :ok <- maybe_federate(activity) do
401 def block(blocker, blocked, activity_id \\ nil, local \\ true) do
402 outgoing_blocks = Pleroma.Config.get([:activitypub, :outgoing_blocks])
403 unfollow_blocked = Pleroma.Config.get([:activitypub, :unfollow_blocked])
405 if unfollow_blocked do
406 follow_activity = fetch_latest_follow(blocker, blocked)
407 if follow_activity, do: unfollow(blocker, blocked, nil, local)
410 with true <- outgoing_blocks,
411 block_data <- make_block_data(blocker, blocked, activity_id),
412 {:ok, activity} <- insert(block_data, local),
413 :ok <- maybe_federate(activity) do
420 def unblock(blocker, blocked, activity_id \\ nil, local \\ true) do
421 with %Activity{} = block_activity <- fetch_latest_block(blocker, blocked),
422 unblock_data <- make_unblock_data(blocker, blocked, block_activity, activity_id),
423 {:ok, activity} <- insert(unblock_data, local),
424 :ok <- maybe_federate(activity) do
438 # only accept false as false value
439 local = !(params[:local] == false)
440 forward = !(params[:forward] == false)
442 additional = params[:additional] || %{}
454 Map.merge(additional, %{"to" => [], "cc" => [account.ap_id]})
456 Map.merge(additional, %{"to" => [], "cc" => []})
459 with flag_data <- make_flag_data(params, additional),
460 {:ok, activity} <- insert(flag_data, local),
461 :ok <- maybe_federate(activity) do
462 Enum.each(User.all_superusers(), fn superuser ->
464 |> Pleroma.Emails.AdminEmail.report(actor, account, statuses, content)
465 |> Pleroma.Emails.Mailer.deliver_async()
472 defp fetch_activities_for_context_query(context, opts) do
473 public = ["https://www.w3.org/ns/activitystreams#Public"]
476 if opts["user"], do: [opts["user"].ap_id | opts["user"].following] ++ public, else: public
478 from(activity in Activity)
479 |> restrict_blocked(opts)
480 |> restrict_recipients(recipients, opts["user"])
484 "?->>'type' = ? and ?->>'context' = ?",
491 |> order_by([activity], desc: activity.id)
494 @spec fetch_activities_for_context(String.t(), keyword() | map()) :: [Activity.t()]
495 def fetch_activities_for_context(context, opts \\ %{}) do
497 |> fetch_activities_for_context_query(opts)
498 |> Activity.with_preloaded_object()
502 @spec fetch_latest_activity_id_for_context(String.t(), keyword() | map()) ::
503 Pleroma.FlakeId.t() | nil
504 def fetch_latest_activity_id_for_context(context, opts \\ %{}) do
506 |> fetch_activities_for_context_query(opts)
512 def fetch_public_activities(opts \\ %{}) do
513 q = fetch_activities_query(["https://www.w3.org/ns/activitystreams#Public"], opts)
516 |> restrict_unlisted()
517 |> Pagination.fetch_paginated(opts)
521 @valid_visibilities ~w[direct unlisted public private]
523 defp restrict_visibility(query, %{visibility: visibility})
524 when is_list(visibility) do
525 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
531 "activity_visibility(?, ?, ?) = ANY (?)",
541 Logger.error("Could not restrict visibility to #{visibility}")
545 defp restrict_visibility(query, %{visibility: visibility})
546 when visibility in @valid_visibilities do
551 fragment("activity_visibility(?, ?, ?) = ?", a.actor, a.recipients, a.data, ^visibility)
557 defp restrict_visibility(_query, %{visibility: visibility})
558 when visibility not in @valid_visibilities do
559 Logger.error("Could not restrict visibility to #{visibility}")
562 defp restrict_visibility(query, _visibility), do: query
564 defp restrict_thread_visibility(query, %{"user" => %User{ap_id: ap_id}}) do
568 where: fragment("thread_visibility(?, (?)->>'id') = true", ^ap_id, a.data)
574 defp restrict_thread_visibility(query, _), do: query
576 def fetch_user_activities(user, reading_user, params \\ %{}) do
579 |> Map.put("type", ["Create", "Announce"])
580 |> Map.put("actor_id", user.ap_id)
581 |> Map.put("whole_db", true)
582 |> Map.put("pinned_activity_ids", user.info.pinned_activities)
586 ["https://www.w3.org/ns/activitystreams#Public"] ++
587 [reading_user.ap_id | reading_user.following]
589 ["https://www.w3.org/ns/activitystreams#Public"]
592 fetch_activities(recipients, params)
596 defp restrict_since(query, %{"since_id" => ""}), do: query
598 defp restrict_since(query, %{"since_id" => since_id}) do
599 from(activity in query, where: activity.id > ^since_id)
602 defp restrict_since(query, _), do: query
604 defp restrict_tag_reject(_query, %{"tag_reject" => _tag_reject, "skip_preload" => true}) do
605 raise "Can't use the child object without preloading!"
608 defp restrict_tag_reject(query, %{"tag_reject" => tag_reject})
609 when is_list(tag_reject) and tag_reject != [] do
611 [_activity, object] in query,
612 where: fragment("not (?)->'tag' \\?| (?)", object.data, ^tag_reject)
616 defp restrict_tag_reject(query, _), do: query
618 defp restrict_tag_all(_query, %{"tag_all" => _tag_all, "skip_preload" => true}) do
619 raise "Can't use the child object without preloading!"
622 defp restrict_tag_all(query, %{"tag_all" => tag_all})
623 when is_list(tag_all) and tag_all != [] do
625 [_activity, object] in query,
626 where: fragment("(?)->'tag' \\?& (?)", object.data, ^tag_all)
630 defp restrict_tag_all(query, _), do: query
632 defp restrict_tag(_query, %{"tag" => _tag, "skip_preload" => true}) do
633 raise "Can't use the child object without preloading!"
636 defp restrict_tag(query, %{"tag" => tag}) when is_list(tag) do
638 [_activity, object] in query,
639 where: fragment("(?)->'tag' \\?| (?)", object.data, ^tag)
643 defp restrict_tag(query, %{"tag" => tag}) when is_binary(tag) do
645 [_activity, object] in query,
646 where: fragment("(?)->'tag' \\? (?)", object.data, ^tag)
650 defp restrict_tag(query, _), do: query
652 defp restrict_to_cc(query, recipients_to, recipients_cc) do
657 "(?->'to' \\?| ?) or (?->'cc' \\?| ?)",
666 defp restrict_recipients(query, [], _user), do: query
668 defp restrict_recipients(query, recipients, nil) do
669 from(activity in query, where: fragment("? && ?", ^recipients, activity.recipients))
672 defp restrict_recipients(query, recipients, user) do
675 where: fragment("? && ?", ^recipients, activity.recipients),
676 or_where: activity.actor == ^user.ap_id
680 defp restrict_local(query, %{"local_only" => true}) do
681 from(activity in query, where: activity.local == true)
684 defp restrict_local(query, _), do: query
686 defp restrict_actor(query, %{"actor_id" => actor_id}) do
687 from(activity in query, where: activity.actor == ^actor_id)
690 defp restrict_actor(query, _), do: query
692 defp restrict_type(query, %{"type" => type}) when is_binary(type) do
693 from(activity in query, where: fragment("?->>'type' = ?", activity.data, ^type))
696 defp restrict_type(query, %{"type" => type}) do
697 from(activity in query, where: fragment("?->>'type' = ANY(?)", activity.data, ^type))
700 defp restrict_type(query, _), do: query
702 defp restrict_state(query, %{"state" => state}) do
703 from(activity in query, where: fragment("?->>'state' = ?", activity.data, ^state))
706 defp restrict_state(query, _), do: query
708 defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
711 where: fragment(~s(? <@ (? #> '{"object","likes"}'\)), ^ap_id, activity.data)
715 defp restrict_favorited_by(query, _), do: query
717 defp restrict_media(_query, %{"only_media" => _val, "skip_preload" => true}) do
718 raise "Can't use the child object without preloading!"
721 defp restrict_media(query, %{"only_media" => val}) when val == "true" or val == "1" do
723 [_activity, object] in query,
724 where: fragment("not (?)->'attachment' = (?)", object.data, ^[])
728 defp restrict_media(query, _), do: query
730 defp restrict_replies(query, %{"exclude_replies" => val}) when val == "true" or val == "1" do
733 where: fragment("?->'object'->>'inReplyTo' is null", activity.data)
737 defp restrict_replies(query, _), do: query
739 defp restrict_reblogs(query, %{"exclude_reblogs" => val}) when val == "true" or val == "1" do
740 from(activity in query, where: fragment("?->>'type' != 'Announce'", activity.data))
743 defp restrict_reblogs(query, _), do: query
745 defp restrict_muted(query, %{"with_muted" => val}) when val in [true, "true", "1"], do: query
747 defp restrict_muted(query, %{"muting_user" => %User{info: info}}) do
752 where: fragment("not (? = ANY(?))", activity.actor, ^mutes),
753 where: fragment("not (?->'to' \\?| ?)", activity.data, ^mutes)
757 defp restrict_muted(query, _), do: query
759 defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
760 blocks = info.blocks || []
761 domain_blocks = info.domain_blocks || []
764 if has_named_binding?(query, :object), do: query, else: Activity.with_joined_object(query)
767 [activity, object: o] in query,
768 where: fragment("not (? = ANY(?))", activity.actor, ^blocks),
769 where: fragment("not (? && ?)", activity.recipients, ^blocks),
772 "not (?->>'type' = 'Announce' and ?->'to' \\?| ?)",
777 where: fragment("not (split_part(?, '/', 3) = ANY(?))", activity.actor, ^domain_blocks),
778 where: fragment("not (split_part(?->>'actor', '/', 3) = ANY(?))", o.data, ^domain_blocks)
782 defp restrict_blocked(query, _), do: query
784 defp restrict_unlisted(query) do
789 "not (coalesce(?->'cc', '{}'::jsonb) \\?| ?)",
791 ^["https://www.w3.org/ns/activitystreams#Public"]
796 defp restrict_pinned(query, %{"pinned" => "true", "pinned_activity_ids" => ids}) do
797 from(activity in query, where: activity.id in ^ids)
800 defp restrict_pinned(query, _), do: query
802 defp restrict_muted_reblogs(query, %{"muting_user" => %User{info: info}}) do
803 muted_reblogs = info.muted_reblogs || []
809 "not ( ?->>'type' = 'Announce' and ? = ANY(?))",
817 defp restrict_muted_reblogs(query, _), do: query
819 defp maybe_preload_objects(query, %{"skip_preload" => true}), do: query
821 defp maybe_preload_objects(query, _) do
823 |> Activity.with_preloaded_object()
826 defp maybe_preload_bookmarks(query, %{"skip_preload" => true}), do: query
828 defp maybe_preload_bookmarks(query, opts) do
830 |> Activity.with_preloaded_bookmark(opts["user"])
833 defp maybe_set_thread_muted_field(query, %{"skip_preload" => true}), do: query
835 defp maybe_set_thread_muted_field(query, opts) do
837 |> Activity.with_set_thread_muted_field(opts["user"])
840 defp maybe_order(query, %{order: :desc}) do
842 |> order_by(desc: :id)
845 defp maybe_order(query, %{order: :asc}) do
847 |> order_by(asc: :id)
850 defp maybe_order(query, _), do: query
852 def fetch_activities_query(recipients, opts \\ %{}) do
853 base_query = from(activity in Activity)
856 |> maybe_preload_objects(opts)
857 |> maybe_preload_bookmarks(opts)
858 |> maybe_set_thread_muted_field(opts)
860 |> restrict_recipients(recipients, opts["user"])
861 |> restrict_tag(opts)
862 |> restrict_tag_reject(opts)
863 |> restrict_tag_all(opts)
864 |> restrict_since(opts)
865 |> restrict_local(opts)
866 |> restrict_actor(opts)
867 |> restrict_type(opts)
868 |> restrict_state(opts)
869 |> restrict_favorited_by(opts)
870 |> restrict_blocked(opts)
871 |> restrict_muted(opts)
872 |> restrict_media(opts)
873 |> restrict_visibility(opts)
874 |> restrict_thread_visibility(opts)
875 |> restrict_replies(opts)
876 |> restrict_reblogs(opts)
877 |> restrict_pinned(opts)
878 |> restrict_muted_reblogs(opts)
879 |> Activity.restrict_deactivated_users()
882 def fetch_activities(recipients, opts \\ %{}) do
883 fetch_activities_query(recipients, opts)
884 |> Pagination.fetch_paginated(opts)
888 def fetch_activities_bounded(recipients_to, recipients_cc, opts \\ %{}) do
889 fetch_activities_query([], opts)
890 |> restrict_to_cc(recipients_to, recipients_cc)
891 |> Pagination.fetch_paginated(opts)
895 def upload(file, opts \\ []) do
896 with {:ok, data} <- Upload.store(file, opts) do
899 Map.put(data, "actor", opts[:actor])
904 Repo.insert(%Object{data: obj_data})
908 defp object_to_user_data(data) do
910 data["icon"]["url"] &&
913 "url" => [%{"href" => data["icon"]["url"]}]
917 data["image"]["url"] &&
920 "url" => [%{"href" => data["image"]["url"]}]
923 locked = data["manuallyApprovesFollowers"] || false
924 data = Transmogrifier.maybe_fix_user_object(data)
929 "ap_enabled" => true,
930 "source_data" => data,
936 follower_address: data["followers"],
940 # nickname can be nil because of virtual actors
942 if data["preferredUsername"] do
946 "#{data["preferredUsername"]}@#{URI.parse(data["id"]).host}"
949 Map.put(user_data, :nickname, nil)
955 def user_data_from_user_object(data) do
956 with {:ok, data} <- MRF.filter(data),
957 {:ok, data} <- object_to_user_data(data) do
964 def fetch_and_prepare_user_from_ap_id(ap_id) do
965 with {:ok, data} <- Fetcher.fetch_and_contain_remote_object_from_id(ap_id),
966 {:ok, data} <- user_data_from_user_object(data) do
969 e -> Logger.error("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
973 def make_user_from_ap_id(ap_id) do
974 if _user = User.get_cached_by_ap_id(ap_id) do
975 Transmogrifier.upgrade_user_from_ap_id(ap_id)
977 with {:ok, data} <- fetch_and_prepare_user_from_ap_id(ap_id) do
978 User.insert_or_update_user(data)
985 def make_user_from_nickname(nickname) do
986 with {:ok, %{"ap_id" => ap_id}} when not is_nil(ap_id) <- WebFinger.finger(nickname) do
987 make_user_from_ap_id(ap_id)
989 _e -> {:error, "No AP id in WebFinger"}
993 # filter out broken threads
994 def contain_broken_threads(%Activity{} = activity, %User{} = user) do
995 entire_thread_visible_for_user?(activity, user)
998 # do post-processing on a specific activity
999 def contain_activity(%Activity{} = activity, %User{} = user) do
1000 contain_broken_threads(activity, user)
1003 def fetch_direct_messages_query do
1005 |> restrict_type(%{"type" => "Create"})
1006 |> restrict_visibility(%{visibility: "direct"})
1007 |> order_by([activity], asc: activity.id)