1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ActivityPub do
6 alias Akkoma.Collections
8 alias Pleroma.Activity.Ir.Topics
10 alias Pleroma.Constants
11 alias Pleroma.Conversation
12 alias Pleroma.Conversation.Participation
16 alias Pleroma.Notification
18 alias Pleroma.Object.Containment
19 alias Pleroma.Object.Fetcher
20 alias Pleroma.Pagination
24 alias Pleroma.Web.ActivityPub.MRF
25 alias Pleroma.Web.ActivityPub.Transmogrifier
26 alias Pleroma.Web.Streamer
27 alias Pleroma.Web.WebFinger
28 alias Pleroma.Workers.BackgroundWorker
29 alias Pleroma.Workers.PollWorker
32 import Pleroma.Web.ActivityPub.Utils
33 import Pleroma.Web.ActivityPub.Visibility
36 require Pleroma.Constants
38 @behaviour Pleroma.Web.ActivityPub.ActivityPub.Persisting
39 @behaviour Pleroma.Web.ActivityPub.ActivityPub.Streaming
41 defp get_recipients(%{"type" => "Create"} = data) do
42 to = Map.get(data, "to", [])
43 cc = Map.get(data, "cc", [])
44 bcc = Map.get(data, "bcc", [])
45 actor = Map.get(data, "actor", [])
46 recipients = [to, cc, bcc, [actor]] |> Enum.concat() |> Enum.uniq()
50 defp get_recipients(data) do
51 to = Map.get(data, "to", [])
52 cc = Map.get(data, "cc", [])
53 bcc = Map.get(data, "bcc", [])
54 recipients = Enum.concat([to, cc, bcc])
58 defp check_actor_can_insert(%{"type" => "Delete"}), do: true
59 defp check_actor_can_insert(%{"type" => "Undo"}), do: true
61 defp check_actor_can_insert(%{"actor" => actor}) when is_binary(actor) do
62 case User.get_cached_by_ap_id(actor) do
63 %User{is_active: true} -> true
68 defp check_actor_can_insert(_), do: true
70 defp check_remote_limit(%{"object" => %{"content" => content}}) when not is_nil(content) do
71 limit = Config.get([:instance, :remote_limit])
72 String.length(content) <= limit
75 defp check_remote_limit(_), do: true
77 def increase_note_count_if_public(actor, object) do
78 if is_public?(object), do: User.increase_note_count(actor), else: {:ok, actor}
81 def decrease_note_count_if_public(actor, object) do
82 if is_public?(object), do: User.decrease_note_count(actor), else: {:ok, actor}
85 def update_last_status_at_if_public(actor, object) do
86 if is_public?(object), do: User.update_last_status_at(actor), else: {:ok, actor}
89 defp increase_replies_count_if_reply(%{
90 "object" => %{"inReplyTo" => reply_ap_id} = object,
93 if is_public?(object) do
94 Object.increase_replies_count(reply_ap_id)
98 defp increase_replies_count_if_reply(_create_data), do: :noop
100 @object_types ~w[Question Answer Audio Video Event Article Note Page]
102 def persist(%{"type" => type} = object, meta) when type in @object_types do
103 with {:ok, object} <- Object.create(object) do
108 @unpersisted_activity_types ~w[Undo Delete Remove Accept Reject]
110 def persist(%{"type" => type} = object, [local: false] = meta)
111 when type in @unpersisted_activity_types do
113 {recipients, _, _} = get_recipients(object)
115 unpersisted = %Activity{
118 recipients: recipients,
119 actor: object["actor"]
122 {:ok, unpersisted, meta}
126 def persist(object, meta) do
127 with local <- Keyword.fetch!(meta, :local),
128 {recipients, _, _} <- get_recipients(object),
130 Repo.insert(%Activity{
133 recipients: recipients,
134 actor: object["actor"]
136 # TODO: add tests for expired activities, when Note type will be supported in new pipeline
137 {:ok, _} <- maybe_create_activity_expiration(activity) do
138 {:ok, activity, meta}
142 @spec insert(map(), boolean(), boolean(), boolean()) :: {:ok, Activity.t()} | {:error, any()}
143 def insert(map, local \\ true, fake \\ false, bypass_actor_check \\ false) when is_map(map) do
144 with nil <- Activity.normalize(map),
145 map <- lazy_put_activity_defaults(map, fake),
146 {_, true} <- {:actor_check, bypass_actor_check || check_actor_can_insert(map)},
147 {_, true} <- {:remote_limit_pass, check_remote_limit(map)},
148 {:ok, map} <- MRF.filter(map),
149 {recipients, _, _} = get_recipients(map),
150 {:fake, false, map, recipients} <- {:fake, fake, map, recipients},
151 {:containment, :ok} <- {:containment, Containment.contain_child(map)},
152 {:ok, map, object} <- insert_full_object(map),
153 {:ok, activity} <- insert_activity_with_expiration(map, local, recipients) do
154 # Splice in the child object if we have one.
155 activity = Maps.put_if_present(activity, :object, object)
157 ConcurrentLimiter.limit(Pleroma.Web.RichMedia.Helpers, fn ->
158 Task.start(fn -> Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity) end)
161 # Add local posts to search index
162 if local, do: Pleroma.Search.add_to_index(activity)
166 %Activity{} = activity ->
172 {:containment, _} = error ->
175 {:error, _} = error ->
178 {:fake, true, map, recipients} ->
179 activity = %Activity{
183 recipients: recipients,
187 Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
190 {:remote_limit_pass, _} ->
191 {:error, :remote_limit}
198 defp insert_activity_with_expiration(data, local, recipients) do
202 actor: data["actor"],
203 recipients: recipients
206 with {:ok, activity} <- Repo.insert(struct) do
207 maybe_create_activity_expiration(activity)
211 def notify_and_stream(activity) do
212 Notification.create_notifications(activity)
216 %{data: %{"type" => "Update"}, object: %{data: %{"id" => id}}} ->
217 Activity.get_create_by_object_ap_id_with_object(id)
223 conversation = create_or_bump_conversation(original_activity, original_activity.actor)
224 participations = get_participations(conversation)
226 stream_out_participations(participations)
229 defp maybe_create_activity_expiration(
230 %{data: %{"expires_at" => %DateTime{} = expires_at}} = activity
233 Pleroma.Workers.PurgeExpiredActivity.enqueue(%{
234 activity_id: activity.id,
235 expires_at: expires_at
241 defp maybe_create_activity_expiration(activity), do: {:ok, activity}
243 defp create_or_bump_conversation(activity, actor) do
244 with {:ok, conversation} <- Conversation.create_or_bump_for(activity),
245 %User{} = user <- User.get_cached_by_ap_id(actor) do
246 Participation.mark_as_read(user, conversation)
251 defp get_participations({:ok, conversation}) do
253 |> Repo.preload(:participations, force: true)
254 |> Map.get(:participations)
257 defp get_participations(_), do: []
259 def stream_out_participations(participations) do
262 |> Repo.preload(:user)
264 Streamer.stream("participation", participations)
268 def stream_out_participations(%Object{data: %{"context" => context}}, user) do
269 with %Conversation{} = conversation <- Conversation.get_for_ap_id(context) do
270 conversation = Repo.preload(conversation, :participations)
273 fetch_latest_direct_activity_id_for_context(conversation.ap_id, %{
278 if last_activity_id do
279 stream_out_participations(conversation.participations)
285 def stream_out_participations(_, _), do: :noop
288 def stream_out(%Activity{data: %{"type" => data_type}} = activity)
289 when data_type in ["Create", "Announce", "Delete", "Update"] do
291 |> Topics.get_activity_topics()
292 |> Streamer.stream(activity)
296 def stream_out(_activity) do
300 @spec create(map(), boolean()) :: {:ok, Activity.t()} | {:error, any()}
301 def create(params, fake \\ false) do
302 with {:ok, result} <- Repo.transaction(fn -> do_create(params, fake) end) do
307 defp do_create(%{to: to, actor: actor, context: context, object: object} = params, fake) do
308 additional = params[:additional] || %{}
309 # only accept false as false value
310 local = !(params[:local] == false)
311 published = params[:published]
312 quick_insert? = Config.get([:env]) == :benchmark
316 %{to: to, actor: actor, published: published, context: context, object: object},
320 with {:ok, activity} <- insert(create_data, local, fake),
321 {:fake, false, activity} <- {:fake, fake, activity},
322 _ <- increase_replies_count_if_reply(create_data),
323 {:quick_insert, false, activity} <- {:quick_insert, quick_insert?, activity},
324 {:ok, _actor} <- increase_note_count_if_public(actor, activity),
325 {:ok, _actor} <- update_last_status_at_if_public(actor, activity),
326 _ <- notify_and_stream(activity),
327 :ok <- maybe_schedule_poll_notifications(activity),
328 :ok <- maybe_federate(activity) do
331 {:quick_insert, true, activity} ->
334 {:fake, true, activity} ->
338 Repo.rollback(message)
342 defp maybe_schedule_poll_notifications(activity) do
343 PollWorker.schedule_poll_end(activity)
347 @spec unfollow(User.t(), User.t(), String.t() | nil, boolean()) ::
348 {:ok, Activity.t()} | nil | {:error, any()}
349 def unfollow(follower, followed, activity_id \\ nil, local \\ true) do
350 with {:ok, result} <-
351 Repo.transaction(fn -> do_unfollow(follower, followed, activity_id, local) end) do
356 defp do_unfollow(follower, followed, activity_id, local)
358 defp do_unfollow(follower, followed, activity_id, local) when local == true do
359 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
360 unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
361 {:ok, activity} <- insert(unfollow_data, local),
362 {:ok, _activity} <- Repo.delete(follow_activity),
363 _ <- notify_and_stream(activity),
364 :ok <- maybe_federate(activity) do
368 {:error, error} -> Repo.rollback(error)
372 defp do_unfollow(follower, followed, activity_id, false) do
373 # On a remote unfollow, _remove_ their activity from the database, since some software (MISSKEEEEY)
374 # uses deterministic ids for follows.
375 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
376 {:ok, _activity} <- Repo.delete(follow_activity),
377 unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
378 unfollow_activity <- make_unfollow_activity(unfollow_data, false),
379 _ <- notify_and_stream(unfollow_activity) do
380 {:ok, unfollow_activity}
383 {:error, error} -> Repo.rollback(error)
387 defp make_unfollow_activity(data, local) do
388 {recipients, _, _} = get_recipients(data)
393 actor: data["actor"],
394 recipients: recipients
398 @spec flag(map()) :: {:ok, Activity.t()} | {:error, any()}
400 with {:ok, result} <- Repo.transaction(fn -> do_flag(params) end) do
414 # only accept false as false value
415 local = !(params[:local] == false)
416 forward = !(params[:forward] == false)
418 additional = params[:additional] || %{}
422 Map.merge(additional, %{"to" => [], "cc" => [account.ap_id]})
424 Map.merge(additional, %{"to" => [], "cc" => []})
427 with flag_data <- make_flag_data(params, additional),
428 {:ok, activity} <- insert(flag_data, local),
429 {:ok, stripped_activity} <- strip_report_status_data(activity),
430 _ <- notify_and_stream(activity),
432 maybe_federate(stripped_activity) do
433 User.all_superusers()
434 |> Enum.filter(fn user -> user.ap_id != actor end)
435 |> Enum.filter(fn user -> not is_nil(user.email) end)
436 |> Enum.each(fn superuser ->
438 |> Pleroma.Emails.AdminEmail.report(actor, account, statuses, content)
439 |> Pleroma.Emails.Mailer.deliver_async()
444 {:error, error} -> Repo.rollback(error)
448 @spec move(User.t(), User.t(), boolean()) :: {:ok, Activity.t()} | {:error, any()}
449 def move(%User{} = origin, %User{} = target, local \\ true) do
452 "actor" => origin.ap_id,
453 "object" => origin.ap_id,
454 "target" => target.ap_id,
455 "to" => [origin.follower_address]
458 with true <- origin.ap_id in target.also_known_as,
459 {:ok, activity} <- insert(params, local),
460 _ <- notify_and_stream(activity) do
461 maybe_federate(activity)
463 BackgroundWorker.enqueue("move_following", %{
464 "origin_id" => origin.id,
465 "target_id" => target.id
470 false -> {:error, "Target account must have the origin in `alsoKnownAs`"}
475 def fetch_activities_for_context_query(context, opts) do
476 public = [Constants.as_public()]
480 do: [opts[:user].ap_id | User.following(opts[:user])] ++ public,
483 from(activity in Activity)
484 |> maybe_preload_objects(opts)
485 |> maybe_preload_bookmarks(opts)
486 |> maybe_set_thread_muted_field(opts)
487 |> restrict_blocked(opts)
488 |> restrict_blockers_visibility(opts)
489 |> restrict_recipients(recipients, opts[:user])
490 |> restrict_filtered(opts)
494 "?->>'type' = ? and ?->>'context' = ?",
501 |> exclude_poll_votes(opts)
503 |> order_by([activity], desc: activity.id)
506 @spec fetch_activities_for_context(String.t(), keyword() | map()) :: [Activity.t()]
507 def fetch_activities_for_context(context, opts \\ %{}) do
509 |> fetch_activities_for_context_query(opts)
513 @spec fetch_latest_direct_activity_id_for_context(String.t(), keyword() | map()) ::
514 FlakeId.Ecto.CompatType.t() | nil
515 def fetch_latest_direct_activity_id_for_context(context, opts \\ %{}) do
517 |> fetch_activities_for_context_query(Map.merge(%{skip_preload: true}, opts))
518 |> restrict_visibility(%{visibility: "direct"})
524 defp fetch_paginated_optimized(query, opts, pagination) do
525 # Note: tag-filtering funcs may apply "ORDER BY objects.id DESC",
526 # and extra sorting on "activities.id DESC NULLS LAST" would worse the query plan
527 opts = Map.put(opts, :skip_extra_order, true)
529 Pagination.fetch_paginated(query, opts, pagination)
532 def fetch_activities(recipients, opts \\ %{}, pagination \\ :keyset) do
533 list_memberships = Pleroma.List.memberships(opts[:user])
535 fetch_activities_query(recipients ++ list_memberships, opts)
536 |> fetch_paginated_optimized(opts, pagination)
538 |> maybe_update_cc(list_memberships, opts[:user])
541 @spec fetch_public_or_unlisted_activities(map(), Pagination.type()) :: [Activity.t()]
542 def fetch_public_or_unlisted_activities(opts \\ %{}, pagination \\ :keyset) do
543 includes_local_public = Map.get(opts, :includes_local_public, false)
545 opts = Map.delete(opts, :user)
547 intended_recipients =
548 if includes_local_public do
549 [Constants.as_public(), as_local_public()]
551 [Constants.as_public()]
555 |> fetch_activities_query(opts)
556 |> restrict_unlisted(opts)
557 |> fetch_paginated_optimized(opts, pagination)
560 @spec fetch_public_activities(map(), Pagination.type()) :: [Activity.t()]
561 def fetch_public_activities(opts \\ %{}, pagination \\ :keyset) do
563 |> Map.put(:restrict_unlisted, true)
564 |> fetch_public_or_unlisted_activities(pagination)
567 @valid_visibilities ~w[direct unlisted public private]
569 defp restrict_visibility(query, %{visibility: visibility})
570 when is_list(visibility) do
571 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
576 "activity_visibility(?, ?, ?) = ANY (?)",
584 Logger.error("Could not restrict visibility to #{visibility}")
588 defp restrict_visibility(query, %{visibility: visibility})
589 when visibility in @valid_visibilities do
593 fragment("activity_visibility(?, ?, ?) = ?", a.actor, a.recipients, a.data, ^visibility)
597 defp restrict_visibility(_query, %{visibility: visibility})
598 when visibility not in @valid_visibilities do
599 Logger.error("Could not restrict visibility to #{visibility}")
602 defp restrict_visibility(query, _visibility), do: query
604 defp exclude_visibility(query, %{exclude_visibilities: visibility})
605 when is_list(visibility) do
606 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
611 "activity_visibility(?, ?, ?) = ANY (?)",
619 Logger.error("Could not exclude visibility to #{visibility}")
624 defp exclude_visibility(query, %{exclude_visibilities: visibility})
625 when visibility in @valid_visibilities do
630 "activity_visibility(?, ?, ?) = ?",
639 defp exclude_visibility(query, %{exclude_visibilities: visibility})
640 when visibility not in [nil | @valid_visibilities] do
641 Logger.error("Could not exclude visibility to #{visibility}")
645 defp exclude_visibility(query, _visibility), do: query
647 defp restrict_thread_visibility(query, _, %{skip_thread_containment: true} = _),
650 defp restrict_thread_visibility(query, %{user: %User{skip_thread_containment: true}}, _),
653 defp restrict_thread_visibility(query, %{user: %User{ap_id: ap_id}}, _) do
654 local_public = as_local_public()
658 where: fragment("thread_visibility(?, (?)->>'id', ?) = true", ^ap_id, a.data, ^local_public)
662 defp restrict_thread_visibility(query, _, _), do: query
664 def fetch_user_abstract_activities(user, reading_user, params \\ %{}) do
667 |> Map.put(:user, reading_user)
668 |> Map.put(:actor_id, user.ap_id)
671 godmode: params[:godmode],
672 reading_user: reading_user
674 |> user_activities_recipients()
675 |> fetch_activities(params)
679 def fetch_user_activities(user, reading_user, params \\ %{})
681 def fetch_user_activities(user, reading_user, %{total: true} = params) do
682 result = fetch_activities_for_user(user, reading_user, params)
684 Keyword.put(result, :items, Enum.reverse(result[:items]))
687 def fetch_user_activities(user, reading_user, params) do
689 |> fetch_activities_for_user(reading_user, params)
693 defp fetch_activities_for_user(user, reading_user, params) do
696 |> Map.put(:type, ["Create", "Announce"])
697 |> Map.put(:user, reading_user)
698 |> Map.put(:actor_id, user.ap_id)
699 |> Map.put(:pinned_object_ids, Map.keys(user.pinned_objects))
702 if User.blocks?(reading_user, user) do
706 |> Map.put(:blocking_user, reading_user)
707 |> Map.put(:muting_user, reading_user)
710 pagination_type = Map.get(params, :pagination_type) || :keyset
713 godmode: params[:godmode],
714 reading_user: reading_user
716 |> user_activities_recipients()
717 |> fetch_activities(params, pagination_type)
720 def fetch_statuses(reading_user, %{total: true} = params) do
721 result = fetch_activities_for_reading_user(reading_user, params)
722 Keyword.put(result, :items, Enum.reverse(result[:items]))
725 def fetch_statuses(reading_user, params) do
727 |> fetch_activities_for_reading_user(params)
731 defp fetch_activities_for_reading_user(reading_user, params) do
732 params = Map.put(params, :type, ["Create", "Announce"])
735 godmode: params[:godmode],
736 reading_user: reading_user
738 |> user_activities_recipients()
739 |> fetch_activities(params, :offset)
742 defp user_activities_recipients(%{godmode: true}), do: []
744 defp user_activities_recipients(%{reading_user: reading_user}) do
745 if not is_nil(reading_user) and reading_user.local do
747 Constants.as_public(),
749 reading_user.ap_id | User.following(reading_user)
752 [Constants.as_public()]
756 defp restrict_announce_object_actor(_query, %{announce_filtering_user: _, skip_preload: true}) do
757 raise "Can't use the child object without preloading!"
760 defp restrict_announce_object_actor(query, %{announce_filtering_user: %{ap_id: actor}}) do
762 [activity, object] in query,
765 "?->>'type' != ? or ?->>'actor' != ?",
774 defp restrict_announce_object_actor(query, _), do: query
776 defp restrict_since(query, %{since_id: ""}), do: query
778 defp restrict_since(query, %{since_id: since_id}) do
779 from(activity in query, where: activity.id > ^since_id)
782 defp restrict_since(query, _), do: query
784 defp restrict_embedded_tag_all(_query, %{tag_all: _tag_all, skip_preload: true}) do
785 raise_on_missing_preload()
788 defp restrict_embedded_tag_all(query, %{tag_all: [_ | _] = tag_all}) do
790 [_activity, object] in query,
791 where: fragment("(?)->'tag' \\?& (?)", object.data, ^tag_all)
795 defp restrict_embedded_tag_all(query, %{tag_all: tag}) when is_binary(tag) do
796 restrict_embedded_tag_any(query, %{tag: tag})
799 defp restrict_embedded_tag_all(query, _), do: query
801 defp restrict_embedded_tag_any(_query, %{tag: _tag, skip_preload: true}) do
802 raise_on_missing_preload()
805 defp restrict_embedded_tag_any(query, %{tag: [_ | _] = tag_any}) do
807 [_activity, object] in query,
808 where: fragment("(?)->'tag' \\?| (?)", object.data, ^tag_any)
812 defp restrict_embedded_tag_any(query, %{tag: tag}) when is_binary(tag) do
813 restrict_embedded_tag_any(query, %{tag: [tag]})
816 defp restrict_embedded_tag_any(query, _), do: query
818 defp restrict_embedded_tag_reject_any(_query, %{tag_reject: _tag_reject, skip_preload: true}) do
819 raise_on_missing_preload()
822 defp restrict_embedded_tag_reject_any(query, %{tag_reject: [_ | _] = tag_reject}) do
824 [_activity, object] in query,
825 where: fragment("not (?)->'tag' \\?| (?)", object.data, ^tag_reject)
829 defp restrict_embedded_tag_reject_any(query, %{tag_reject: tag_reject})
830 when is_binary(tag_reject) do
831 restrict_embedded_tag_reject_any(query, %{tag_reject: [tag_reject]})
834 defp restrict_embedded_tag_reject_any(query, _), do: query
836 defp object_ids_query_for_tags(tags) do
837 from(hto in "hashtags_objects")
838 |> join(:inner, [hto], ht in Pleroma.Hashtag, on: hto.hashtag_id == ht.id)
839 |> where([hto, ht], ht.name in ^tags)
840 |> select([hto], hto.object_id)
841 |> distinct([hto], true)
844 defp restrict_hashtag_all(_query, %{tag_all: _tag, skip_preload: true}) do
845 raise_on_missing_preload()
848 defp restrict_hashtag_all(query, %{tag_all: [single_tag]}) do
849 restrict_hashtag_any(query, %{tag: single_tag})
852 defp restrict_hashtag_all(query, %{tag_all: [_ | _] = tags}) do
854 [_activity, object] in query,
858 (SELECT array_agg(hashtags.name) FROM hashtags JOIN hashtags_objects
859 ON hashtags_objects.hashtag_id = hashtags.id WHERE hashtags.name = ANY(?)
860 AND hashtags_objects.object_id = ?) @> ?
869 defp restrict_hashtag_all(query, %{tag_all: tag}) when is_binary(tag) do
870 restrict_hashtag_all(query, %{tag_all: [tag]})
873 defp restrict_hashtag_all(query, _), do: query
875 defp restrict_hashtag_any(_query, %{tag: _tag, skip_preload: true}) do
876 raise_on_missing_preload()
879 defp restrict_hashtag_any(query, %{tag: [_ | _] = tags}) do
881 from(ht in Hashtag, where: ht.name in ^tags, select: ht.id)
884 # Note: NO extra ordering should be done on "activities.id desc nulls last" for optimal plan
886 [_activity, object] in query,
887 join: hto in "hashtags_objects",
888 on: hto.object_id == object.id,
889 where: hto.hashtag_id in ^hashtag_ids,
890 distinct: [desc: object.id],
891 order_by: [desc: object.id]
895 defp restrict_hashtag_any(query, %{tag: tag}) when is_binary(tag) do
896 restrict_hashtag_any(query, %{tag: [tag]})
899 defp restrict_hashtag_any(query, _), do: query
901 defp restrict_hashtag_reject_any(_query, %{tag_reject: _tag_reject, skip_preload: true}) do
902 raise_on_missing_preload()
905 defp restrict_hashtag_reject_any(query, %{tag_reject: [_ | _] = tags_reject}) do
907 [_activity, object] in query,
908 where: object.id not in subquery(object_ids_query_for_tags(tags_reject))
912 defp restrict_hashtag_reject_any(query, %{tag_reject: tag_reject}) when is_binary(tag_reject) do
913 restrict_hashtag_reject_any(query, %{tag_reject: [tag_reject]})
916 defp restrict_hashtag_reject_any(query, _), do: query
918 defp raise_on_missing_preload do
919 raise "Can't use the child object without preloading!"
922 defp restrict_recipients(query, [], _user), do: query
924 defp restrict_recipients(query, recipients, nil) do
925 from(activity in query, where: fragment("? && ?", ^recipients, activity.recipients))
928 defp restrict_recipients(query, recipients, user) do
931 where: fragment("? && ?", ^recipients, activity.recipients),
932 or_where: activity.actor == ^user.ap_id
936 # Essentially, either look for activities addressed to `recipients`, _OR_ ones
937 # that reference a hashtag that the user follows
938 # Firstly, two fallbacks in case there's no hashtag constraint, or the user doesn't
940 defp restrict_recipients_or_hashtags(query, recipients, user, nil) do
941 restrict_recipients(query, recipients, user)
944 defp restrict_recipients_or_hashtags(query, recipients, user, []) do
945 restrict_recipients(query, recipients, user)
948 defp restrict_recipients_or_hashtags(query, recipients, _user, hashtag_ids) do
950 [activity, object] in query,
951 join: hto in "hashtags_objects",
952 on: hto.object_id == object.id,
954 (hto.hashtag_id in ^hashtag_ids and ^Constants.as_public() in activity.recipients) or
955 fragment("? && ?", ^recipients, activity.recipients)
959 defp restrict_local(query, %{local_only: true}) do
960 from(activity in query, where: activity.local == true)
963 defp restrict_local(query, _), do: query
965 defp restrict_remote(query, %{remote: true}) do
966 from(activity in query, where: activity.local == false)
969 defp restrict_remote(query, _), do: query
971 defp restrict_actor(query, %{actor_id: actor_id}) do
972 from(activity in query, where: activity.actor == ^actor_id)
975 defp restrict_actor(query, _), do: query
977 defp restrict_type(query, %{type: type}) when is_binary(type) do
978 from(activity in query, where: fragment("?->>'type' = ?", activity.data, ^type))
981 defp restrict_type(query, %{type: type}) do
982 from(activity in query, where: fragment("?->>'type' = ANY(?)", activity.data, ^type))
985 defp restrict_type(query, _), do: query
987 defp restrict_state(query, %{state: state}) do
988 from(activity in query, where: fragment("?->>'state' = ?", activity.data, ^state))
991 defp restrict_state(query, _), do: query
993 defp restrict_favorited_by(query, %{favorited_by: ap_id}) do
995 [_activity, object] in query,
996 where: fragment("(?)->'likes' \\? (?)", object.data, ^ap_id)
1000 defp restrict_favorited_by(query, _), do: query
1002 defp restrict_media(_query, %{only_media: _val, skip_preload: true}) do
1003 raise "Can't use the child object without preloading!"
1006 defp restrict_media(query, %{only_media: true}) do
1008 [activity, object] in query,
1009 where: fragment("(?)->>'type' = ?", activity.data, "Create"),
1010 where: fragment("not (?)->'attachment' = (?)", object.data, ^[])
1014 defp restrict_media(query, _), do: query
1016 defp restrict_replies(query, %{exclude_replies: true}) do
1018 [_activity, object] in query,
1019 where: fragment("?->>'inReplyTo' is null", object.data)
1023 defp restrict_replies(query, %{
1024 reply_filtering_user: %User{} = user,
1025 reply_visibility: "self"
1028 [activity, object] in query,
1031 "?->>'inReplyTo' is null OR ? = ANY(?)",
1039 defp restrict_replies(query, %{
1040 reply_filtering_user: %User{} = user,
1041 reply_visibility: "following"
1044 [activity, object] in query,
1048 ?->>'type' != 'Create' -- This isn't a Create
1049 OR ?->>'inReplyTo' is null -- this isn't a reply
1050 OR ? && array_remove(?, ?) -- The recipient is us or one of our friends,
1051 -- unless they are the author (because authors
1052 -- are also part of the recipients). This leads
1053 -- to a bug that self-replies by friends won't
1055 OR ? = ? -- The actor is us
1059 ^[user.ap_id | User.get_cached_user_friends_ap_ids(user)],
1060 activity.recipients,
1068 defp restrict_replies(query, _), do: query
1070 defp restrict_reblogs(query, %{exclude_reblogs: true}) do
1071 from(activity in query, where: fragment("?->>'type' != 'Announce'", activity.data))
1074 defp restrict_reblogs(query, _), do: query
1076 defp restrict_muted(query, %{with_muted: true}), do: query
1078 defp restrict_muted(query, %{muting_user: %User{} = user} = opts) do
1079 mutes = opts[:muted_users_ap_ids] || User.muted_users_ap_ids(user)
1082 from([activity] in query,
1083 where: fragment("not (? = ANY(?))", activity.actor, ^mutes),
1086 "not (?->'to' \\?| ?) or ? = ?",
1094 unless opts[:skip_preload] do
1095 from([thread_mute: tm] in query, where: is_nil(tm.user_id))
1101 defp restrict_muted(query, _), do: query
1103 defp restrict_blocked(query, %{blocking_user: %User{} = user} = opts) do
1104 blocked_ap_ids = opts[:blocked_users_ap_ids] || User.blocked_users_ap_ids(user)
1105 domain_blocks = user.domain_blocks || []
1107 following_ap_ids = User.get_friends_ap_ids(user)
1110 if has_named_binding?(query, :object), do: query, else: Activity.with_joined_object(query)
1113 [activity, object: o] in query,
1114 # You don't block the author
1115 where: fragment("not (? = ANY(?))", activity.actor, ^blocked_ap_ids),
1117 # You don't block any recipients, and didn't author the post
1120 "((not (? && ?)) or ? = ?)",
1121 activity.recipients,
1127 # You don't block the domain of any recipients, and didn't author the post
1130 "(recipients_contain_blocked_domains(?, ?) = false) or ? = ?",
1131 activity.recipients,
1137 # It's not a boost of a user you block
1140 "not (?->>'type' = 'Announce' and ?->'to' \\?| ?)",
1146 # You don't block the author's domain, and also don't follow the author
1149 "(not (split_part(?, '/', 3) = ANY(?))) or ? = ANY(?)",
1156 # Same as above, but checks the Object
1159 "(not (split_part(?->>'actor', '/', 3) = ANY(?))) or (?->>'actor') = ANY(?)",
1168 defp restrict_blocked(query, _), do: query
1170 defp restrict_blockers_visibility(query, %{blocking_user: %User{} = user}) do
1171 if Config.get([:activitypub, :blockers_visible]) == true do
1174 blocker_ap_ids = User.incoming_relationships_ungrouped_ap_ids(user, [:block])
1178 # The author doesn't block you
1179 where: fragment("not (? = ANY(?))", activity.actor, ^blocker_ap_ids),
1181 # It's not a boost of a user that blocks you
1184 "not (?->>'type' = 'Announce' and ?->'to' \\?| ?)",
1193 defp restrict_blockers_visibility(query, _), do: query
1195 defp restrict_unlisted(query, %{restrict_unlisted: true}) do
1200 "not (coalesce(?->'cc', '{}'::jsonb) \\?| ?)",
1202 ^[Constants.as_public()]
1207 defp restrict_unlisted(query, _), do: query
1209 defp restrict_pinned(query, %{pinned: true, pinned_object_ids: ids}) do
1211 [activity, object: o] in query,
1214 "(?)->>'type' = 'Create' and coalesce((?)->'object'->>'id', (?)->>'object') = any (?)",
1223 defp restrict_pinned(query, _), do: query
1225 defp restrict_muted_reblogs(query, %{muting_user: %User{} = user} = opts) do
1226 muted_reblogs = opts[:reblog_muted_users_ap_ids] || User.reblog_muted_users_ap_ids(user)
1232 "not ( ?->>'type' = 'Announce' and ? = ANY(?))",
1240 defp restrict_muted_reblogs(query, _), do: query
1242 defp restrict_instance(query, %{instance: instance}) when is_binary(instance) do
1245 where: fragment("split_part(actor::text, '/'::text, 3) = ?", ^instance)
1249 defp restrict_instance(query, %{instance: instance}) when is_list(instance) do
1252 where: fragment("split_part(actor::text, '/'::text, 3) = ANY(?)", ^instance)
1256 defp restrict_instance(query, _), do: query
1258 defp restrict_filtered(query, %{user: %User{} = user}) do
1259 case Filter.compose_regex(user) do
1264 from([activity, object] in query,
1266 fragment("not(?->>'content' ~* ?)", object.data, ^regex) or
1267 activity.actor == ^user.ap_id
1272 defp restrict_filtered(query, %{blocking_user: %User{} = user}) do
1273 restrict_filtered(query, %{user: user})
1276 defp restrict_filtered(query, _), do: query
1278 defp exclude_poll_votes(query, %{include_poll_votes: true}), do: query
1280 defp exclude_poll_votes(query, _) do
1281 if has_named_binding?(query, :object) do
1282 from([activity, object: o] in query,
1283 where: fragment("not(?->>'type' = ?)", o.data, "Answer")
1290 defp exclude_invisible_actors(query, %{type: "Flag"}), do: query
1291 defp exclude_invisible_actors(query, %{invisible_actors: true}), do: query
1293 defp exclude_invisible_actors(query, _opts) do
1295 |> join(:inner, [activity], u in User,
1297 on: activity.actor == u.ap_id and u.invisible == false
1301 defp exclude_id(query, %{exclude_id: id}) when is_binary(id) do
1302 from(activity in query, where: activity.id != ^id)
1305 defp exclude_id(query, _), do: query
1307 defp maybe_preload_objects(query, %{skip_preload: true}), do: query
1309 defp maybe_preload_objects(query, _) do
1311 |> Activity.with_preloaded_object()
1314 defp maybe_preload_bookmarks(query, %{skip_preload: true}), do: query
1316 defp maybe_preload_bookmarks(query, opts) do
1318 |> Activity.with_preloaded_bookmark(opts[:user])
1321 defp maybe_preload_report_notes(query, %{preload_report_notes: true}) do
1323 |> Activity.with_preloaded_report_notes()
1326 defp maybe_preload_report_notes(query, _), do: query
1328 defp maybe_set_thread_muted_field(query, %{skip_preload: true}), do: query
1330 defp maybe_set_thread_muted_field(query, opts) do
1332 |> Activity.with_set_thread_muted_field(opts[:muting_user] || opts[:user])
1335 defp maybe_order(query, %{order: :desc}) do
1337 |> order_by(desc: :id)
1340 defp maybe_order(query, %{order: :asc}) do
1342 |> order_by(asc: :id)
1345 defp maybe_order(query, _), do: query
1347 defp normalize_fetch_activities_query_opts(opts) do
1348 Enum.reduce([:tag, :tag_all, :tag_reject], opts, fn key, opts ->
1350 value when is_bitstring(value) ->
1351 Map.put(opts, key, Hashtag.normalize_name(value))
1353 value when is_list(value) ->
1356 |> Enum.map(&Hashtag.normalize_name/1)
1359 Map.put(opts, key, normalized_value)
1367 defp fetch_activities_query_ap_ids_ops(opts) do
1368 source_user = opts[:muting_user]
1369 ap_id_relationships = if source_user, do: [:mute, :reblog_mute], else: []
1371 ap_id_relationships =
1372 if opts[:blocking_user] && opts[:blocking_user] == source_user do
1373 [:block | ap_id_relationships]
1378 preloaded_ap_ids = User.outgoing_relationships_ap_ids(source_user, ap_id_relationships)
1380 restrict_blocked_opts = Map.merge(%{blocked_users_ap_ids: preloaded_ap_ids[:block]}, opts)
1381 restrict_muted_opts = Map.merge(%{muted_users_ap_ids: preloaded_ap_ids[:mute]}, opts)
1383 restrict_muted_reblogs_opts =
1384 Map.merge(%{reblog_muted_users_ap_ids: preloaded_ap_ids[:reblog_mute]}, opts)
1386 {restrict_blocked_opts, restrict_muted_opts, restrict_muted_reblogs_opts}
1389 def fetch_activities_query(recipients, opts \\ %{}) do
1390 opts = normalize_fetch_activities_query_opts(opts)
1392 {restrict_blocked_opts, restrict_muted_opts, restrict_muted_reblogs_opts} =
1393 fetch_activities_query_ap_ids_ops(opts)
1396 skip_thread_containment: Config.get([:instance, :skip_thread_containment])
1401 |> maybe_preload_objects(opts)
1402 |> maybe_preload_bookmarks(opts)
1403 |> maybe_preload_report_notes(opts)
1404 |> maybe_set_thread_muted_field(opts)
1405 |> maybe_order(opts)
1406 |> restrict_recipients_or_hashtags(recipients, opts[:user], opts[:followed_hashtags])
1407 |> restrict_replies(opts)
1408 |> restrict_since(opts)
1409 |> restrict_local(opts)
1410 |> restrict_remote(opts)
1411 |> restrict_actor(opts)
1412 |> restrict_type(opts)
1413 |> restrict_state(opts)
1414 |> restrict_favorited_by(opts)
1415 |> restrict_blocked(restrict_blocked_opts)
1416 |> restrict_blockers_visibility(opts)
1417 |> restrict_muted(restrict_muted_opts)
1418 |> restrict_filtered(opts)
1419 |> restrict_media(opts)
1420 |> restrict_visibility(opts)
1421 |> restrict_thread_visibility(opts, config)
1422 |> restrict_reblogs(opts)
1423 |> restrict_pinned(opts)
1424 |> restrict_muted_reblogs(restrict_muted_reblogs_opts)
1425 |> restrict_instance(opts)
1426 |> restrict_announce_object_actor(opts)
1427 |> restrict_filtered(opts)
1428 |> maybe_restrict_deactivated_users(opts)
1429 |> exclude_poll_votes(opts)
1430 |> exclude_invisible_actors(opts)
1431 |> exclude_visibility(opts)
1433 if Config.feature_enabled?(:improved_hashtag_timeline) do
1435 |> restrict_hashtag_any(opts)
1436 |> restrict_hashtag_all(opts)
1437 |> restrict_hashtag_reject_any(opts)
1440 |> restrict_embedded_tag_any(opts)
1441 |> restrict_embedded_tag_all(opts)
1442 |> restrict_embedded_tag_reject_any(opts)
1447 Fetch favorites activities of user with order by sort adds to favorites
1449 @spec fetch_favourites(User.t(), map(), Pagination.type()) :: list(Activity.t())
1450 def fetch_favourites(user, params \\ %{}, pagination \\ :keyset) do
1452 |> Activity.Queries.by_actor()
1453 |> Activity.Queries.by_type("Like")
1454 |> Activity.with_joined_object()
1455 |> Object.with_joined_activity()
1456 |> select([like, object, activity], %{activity | object: object, pagination_id: like.id})
1457 |> order_by([like, _, _], desc_nulls_last: like.id)
1458 |> Pagination.fetch_paginated(
1459 Map.merge(params, %{skip_order: true}),
1464 defp maybe_update_cc(activities, [_ | _] = list_memberships, %User{ap_id: user_ap_id}) do
1465 Enum.map(activities, fn
1466 %{data: %{"bcc" => [_ | _] = bcc}} = activity ->
1467 if Enum.any?(bcc, &(&1 in list_memberships)) do
1468 update_in(activity.data["cc"], &[user_ap_id | &1])
1478 defp maybe_update_cc(activities, _, _), do: activities
1480 defp fetch_activities_bounded_query(query, recipients, recipients_with_public) do
1481 from(activity in query,
1483 fragment("? && ?", activity.recipients, ^recipients) or
1484 (fragment("? && ?", activity.recipients, ^recipients_with_public) and
1485 ^Constants.as_public() in activity.recipients)
1489 def fetch_activities_bounded(
1491 recipients_with_public,
1493 pagination \\ :keyset
1495 fetch_activities_query([], opts)
1496 |> fetch_activities_bounded_query(recipients, recipients_with_public)
1497 |> Pagination.fetch_paginated(opts, pagination)
1501 @spec upload(Upload.source(), keyword()) :: {:ok, Object.t()} | {:error, any()}
1502 def upload(file, opts \\ []) do
1503 with {:ok, data} <- Upload.store(file, opts) do
1504 obj_data = Maps.put_if_present(data, "actor", opts[:actor])
1506 Repo.insert(%Object{data: obj_data})
1510 @spec get_actor_url(any()) :: binary() | nil
1511 defp get_actor_url(url) when is_binary(url), do: url
1512 defp get_actor_url(%{"href" => href}) when is_binary(href), do: href
1514 defp get_actor_url(url) when is_list(url) do
1520 defp get_actor_url(_url), do: nil
1522 defp normalize_image(%{"url" => url}) do
1525 "url" => [%{"href" => url}]
1529 defp normalize_image(urls) when is_list(urls), do: urls |> List.first() |> normalize_image()
1530 defp normalize_image(_), do: nil
1532 defp object_to_user_data(data, additional) do
1535 |> Map.get("attachment", [])
1536 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
1537 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
1541 |> Map.get("tag", [])
1543 %{"type" => "Emoji"} -> true
1546 |> Map.new(fn %{"icon" => %{"url" => url}, "name" => name} ->
1547 {String.trim(name, ":"), url}
1550 is_locked = data["manuallyApprovesFollowers"] || false
1551 data = Transmogrifier.maybe_fix_user_object(data)
1552 is_discoverable = data["discoverable"] || false
1553 invisible = data["invisible"] || false
1554 actor_type = data["type"] || "Person"
1556 featured_address = data["featured"]
1557 {:ok, pinned_objects} = fetch_and_prepare_featured_from_ap_id(featured_address)
1560 if is_map(data["publicKey"]) && is_binary(data["publicKey"]["publicKeyPem"]) do
1561 data["publicKey"]["publicKeyPem"]
1565 if is_map(data["endpoints"]) && is_binary(data["endpoints"]["sharedInbox"]) do
1566 data["endpoints"]["sharedInbox"]
1569 # if WebFinger request was already done, we probably have acct, otherwise
1570 # we request WebFinger here
1571 nickname = additional[:nickname_from_acct] || generate_nickname(data)
1573 # also_known_as must be a URL
1576 |> Map.get("alsoKnownAs", [])
1577 |> Enum.filter(fn url ->
1578 case URI.parse(url) do
1579 %URI{scheme: "http"} -> true
1580 %URI{scheme: "https"} -> true
1587 uri: get_actor_url(data["url"]),
1589 banner: normalize_image(data["image"]),
1592 is_locked: is_locked,
1593 is_discoverable: is_discoverable,
1594 invisible: invisible,
1595 avatar: normalize_image(data["icon"]),
1597 follower_address: data["followers"],
1598 following_address: data["following"],
1599 featured_address: featured_address,
1600 bio: data["summary"] || "",
1601 actor_type: actor_type,
1602 also_known_as: also_known_as,
1603 public_key: public_key,
1604 inbox: data["inbox"],
1605 shared_inbox: shared_inbox,
1606 pinned_objects: pinned_objects,
1611 defp generate_nickname(%{"preferredUsername" => username} = data) when is_binary(username) do
1612 generated = "#{username}@#{URI.parse(data["id"]).host}"
1614 if Config.get([WebFinger, :update_nickname_on_user_fetch]) do
1615 case WebFinger.finger(generated) do
1616 {:ok, %{"subject" => "acct:" <> acct}} -> acct
1624 # nickname can be nil because of virtual actors
1625 defp generate_nickname(_), do: nil
1627 def fetch_follow_information_for_user(user) do
1628 with {:ok, following_data} <-
1629 Fetcher.fetch_and_contain_remote_object_from_id(user.following_address),
1630 {:ok, hide_follows} <- collection_private(following_data),
1631 {:ok, followers_data} <-
1632 Fetcher.fetch_and_contain_remote_object_from_id(user.follower_address),
1633 {:ok, hide_followers} <- collection_private(followers_data) do
1636 hide_follows: hide_follows,
1637 follower_count: normalize_counter(followers_data["totalItems"]),
1638 following_count: normalize_counter(following_data["totalItems"]),
1639 hide_followers: hide_followers
1642 {:error, _} = e -> e
1647 defp normalize_counter(counter) when is_integer(counter), do: counter
1648 defp normalize_counter(_), do: 0
1650 def maybe_update_follow_information(user_data) do
1651 with {:enabled, true} <- {:enabled, Config.get([:instance, :external_user_synchronization])},
1652 {_, true} <- {:user_type_check, user_data[:type] in ["Person", "Service"]},
1654 {:collections_available,
1655 !!(user_data[:following_address] && user_data[:follower_address])},
1657 fetch_follow_information_for_user(user_data) do
1658 info = Map.merge(user_data[:info] || %{}, info)
1661 |> Map.put(:info, info)
1663 {:user_type_check, false} ->
1666 {:collections_available, false} ->
1669 {:enabled, false} ->
1674 "Follower/Following counter update for #{user_data.ap_id} failed.\n" <> inspect(e)
1681 defp collection_private(%{"first" => %{"type" => type}})
1682 when type in ["CollectionPage", "OrderedCollectionPage"],
1685 defp collection_private(%{"first" => first}) do
1686 with {:ok, %{"type" => type}} when type in ["CollectionPage", "OrderedCollectionPage"] <-
1687 Fetcher.fetch_and_contain_remote_object_from_id(first) do
1690 {:error, {:ok, %{status: code}}} when code in [401, 403] -> {:ok, true}
1691 {:error, _} = e -> e
1696 defp collection_private(_data), do: {:ok, true}
1698 def user_data_from_user_object(data, additional \\ []) do
1699 with {:ok, data} <- MRF.filter(data) do
1700 {:ok, object_to_user_data(data, additional)}
1706 def fetch_and_prepare_user_from_ap_id(ap_id, additional \\ []) do
1707 with {:ok, data} <- Fetcher.fetch_and_contain_remote_object_from_id(ap_id),
1708 {:ok, data} <- user_data_from_user_object(data, additional) do
1709 {:ok, maybe_update_follow_information(data)}
1711 # If this has been deleted, only log a debug and not an error
1712 {:error, "Object has been deleted" = e} ->
1713 Logger.debug("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
1716 {:error, {:reject, reason} = e} ->
1717 Logger.debug("Rejected user #{ap_id}: #{inspect(reason)}")
1721 Logger.error("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
1726 def maybe_handle_clashing_nickname(data) do
1727 with nickname when is_binary(nickname) <- data[:nickname],
1728 %User{} = old_user <- User.get_by_nickname(nickname),
1729 {_, false} <- {:ap_id_comparison, data[:ap_id] == old_user.ap_id} do
1731 "Found an old user for #{nickname}, the old ap id is #{old_user.ap_id}, new one is #{data[:ap_id]}, renaming."
1735 |> User.remote_user_changeset(%{nickname: "#{old_user.id}.#{old_user.nickname}"})
1736 |> User.update_and_set_cache()
1738 {:ap_id_comparison, true} ->
1740 "Found an old user for #{data[:nickname]}, but the ap id #{data[:ap_id]} is the same as the new user. Race condition? Not changing anything."
1748 def pin_data_from_featured_collection(%{
1749 "type" => "OrderedCollection",
1752 with {:ok, page} <- Fetcher.fetch_and_contain_remote_object_from_id(first) do
1754 |> Map.get("orderedItems")
1755 |> Map.new(fn %{"id" => object_ap_id} -> {object_ap_id, NaiveDateTime.utc_now()} end)
1758 Logger.error("Could not decode featured collection at fetch #{first}, #{inspect(e)}")
1763 def pin_data_from_featured_collection(
1768 when type in ["OrderedCollection", "Collection"] do
1769 {:ok, objects} = Collections.Fetcher.fetch_collection(collection)
1771 # Items can either be a map _or_ a string
1774 ap_id when is_binary(ap_id) -> {ap_id, NaiveDateTime.utc_now()}
1775 %{"id" => object_ap_id} -> {object_ap_id, NaiveDateTime.utc_now()}
1779 def fetch_and_prepare_featured_from_ap_id(nil) do
1783 def fetch_and_prepare_featured_from_ap_id(ap_id) do
1784 with {:ok, data} <- Fetcher.fetch_and_contain_remote_object_from_id(ap_id) do
1785 {:ok, pin_data_from_featured_collection(data)}
1788 Logger.error("Could not decode featured collection at fetch #{ap_id}, #{inspect(e)}")
1793 def pinned_fetch_task(nil), do: nil
1795 def pinned_fetch_task(%{pinned_objects: pins}) do
1796 if Enum.all?(pins, fn {ap_id, _} ->
1797 Object.get_cached_by_ap_id(ap_id) ||
1798 match?({:ok, _object}, Fetcher.fetch_object_from_id(ap_id))
1806 def make_user_from_ap_id(ap_id, additional \\ []) do
1807 user = User.get_cached_by_ap_id(ap_id)
1809 if user && !User.ap_enabled?(user) do
1810 Transmogrifier.upgrade_user_from_ap_id(ap_id)
1812 with {:ok, data} <- fetch_and_prepare_user_from_ap_id(ap_id, additional) do
1813 {:ok, _pid} = Task.start(fn -> pinned_fetch_task(data) end)
1817 |> User.remote_user_changeset(data)
1818 |> User.update_and_set_cache()
1820 maybe_handle_clashing_nickname(data)
1823 |> User.remote_user_changeset()
1831 def make_user_from_nickname(nickname) do
1832 with {:ok, %{"ap_id" => ap_id, "subject" => "acct:" <> acct}} when not is_nil(ap_id) <-
1833 WebFinger.finger(nickname) do
1834 make_user_from_ap_id(ap_id, nickname_from_acct: acct)
1836 _e -> {:error, "No AP id in WebFinger"}
1840 # filter out broken threads
1841 defp contain_broken_threads(%Activity{} = activity, %User{} = user) do
1842 entire_thread_visible_for_user?(activity, user)
1845 # do post-processing on a specific activity
1846 def contain_activity(%Activity{} = activity, %User{} = user) do
1847 contain_broken_threads(activity, user)
1850 def fetch_direct_messages_query do
1852 |> restrict_type(%{type: "Create"})
1853 |> restrict_visibility(%{visibility: "direct"})
1854 |> order_by([activity], asc: activity.id)
1857 defp maybe_restrict_deactivated_users(activity, %{type: "Flag"}), do: activity
1859 defp maybe_restrict_deactivated_users(activity, _opts),
1860 do: Activity.restrict_deactivated_users(activity)