1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ActivityPub do
7 alias Pleroma.Activity.Ir.Topics
9 alias Pleroma.Constants
10 alias Pleroma.Conversation
11 alias Pleroma.Conversation.Participation
12 alias Pleroma.Notification
14 alias Pleroma.Object.Containment
15 alias Pleroma.Object.Fetcher
16 alias Pleroma.Pagination
20 alias Pleroma.Web.ActivityPub.MRF
21 alias Pleroma.Web.ActivityPub.Transmogrifier
22 alias Pleroma.Web.ActivityPub.Utils
23 alias Pleroma.Web.Streamer
24 alias Pleroma.Web.WebFinger
25 alias Pleroma.Workers.BackgroundWorker
28 import Pleroma.Web.ActivityPub.Utils
29 import Pleroma.Web.ActivityPub.Visibility
32 require Pleroma.Constants
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"])
43 Enum.filter(Enum.concat([to, cc, bcc]), fn recipient ->
44 case User.get_cached_by_ap_id(recipient) do
46 user -> User.following?(user, actor)
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()
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])
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
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
88 defp check_remote_limit(_), do: true
90 def increase_note_count_if_public(actor, object) do
91 if is_public?(object), do: User.increase_note_count(actor), else: {:ok, actor}
94 def decrease_note_count_if_public(actor, object) do
95 if is_public?(object), do: User.decrease_note_count(actor), else: {:ok, actor}
98 def increase_replies_count_if_reply(%{
99 "object" => %{"inReplyTo" => reply_ap_id} = object,
102 if is_public?(object) do
103 Object.increase_replies_count(reply_ap_id)
107 def increase_replies_count_if_reply(_create_data), do: :noop
109 def decrease_replies_count_if_reply(%Object{
110 data: %{"inReplyTo" => reply_ap_id} = object
112 if is_public?(object) do
113 Object.decrease_replies_count(reply_ap_id)
117 def decrease_replies_count_if_reply(_object), do: :noop
119 def increase_poll_votes_if_vote(%{
120 "object" => %{"inReplyTo" => reply_ap_id, "name" => name},
124 Object.increase_vote_count(reply_ap_id, name, actor)
127 def increase_poll_votes_if_vote(_create_data), do: :noop
129 @spec persist(map(), keyword()) :: {:ok, Activity.t() | Object.t()}
130 def persist(object, meta) do
131 with local <- Keyword.fetch!(meta, :local),
132 {recipients, _, _} <- get_recipients(object),
134 Repo.insert(%Activity{
137 recipients: recipients,
138 actor: object["actor"]
140 {:ok, activity, meta}
144 @spec insert(map(), boolean(), boolean(), boolean()) :: {:ok, Activity.t()} | {:error, any()}
145 def insert(map, local \\ true, fake \\ false, bypass_actor_check \\ false) when is_map(map) do
146 with nil <- Activity.normalize(map),
147 map <- lazy_put_activity_defaults(map, fake),
148 true <- bypass_actor_check || check_actor_is_active(map["actor"]),
149 {_, true} <- {:remote_limit_error, check_remote_limit(map)},
150 {:ok, map} <- MRF.filter(map),
151 {recipients, _, _} = get_recipients(map),
152 {:fake, false, map, recipients} <- {:fake, fake, map, recipients},
153 {:containment, :ok} <- {:containment, Containment.contain_child(map)},
154 {:ok, map, object} <- insert_full_object(map) do
156 Repo.insert(%Activity{
160 recipients: recipients
163 # Splice in the child object if we have one.
165 if not is_nil(object) do
166 Map.put(activity, :object, object)
171 BackgroundWorker.enqueue("fetch_data_for_activity", %{"activity_id" => activity.id})
175 %Activity{} = activity ->
178 {:fake, true, map, recipients} ->
179 activity = %Activity{
183 recipients: recipients,
187 Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
195 def notify_and_stream(activity) do
196 Notification.create_notifications(activity)
198 conversation = create_or_bump_conversation(activity, activity.actor)
199 participations = get_participations(conversation)
201 stream_out_participations(participations)
204 defp create_or_bump_conversation(activity, actor) do
205 with {:ok, conversation} <- Conversation.create_or_bump_for(activity),
206 %User{} = user <- User.get_cached_by_ap_id(actor),
207 Participation.mark_as_read(user, conversation) do
212 defp get_participations({:ok, conversation}) do
214 |> Repo.preload(:participations, force: true)
215 |> Map.get(:participations)
218 defp get_participations(_), do: []
220 def stream_out_participations(participations) do
223 |> Repo.preload(:user)
225 Streamer.stream("participation", participations)
228 def stream_out_participations(%Object{data: %{"context" => context}}, user) do
229 with %Conversation{} = conversation <- Conversation.get_for_ap_id(context),
230 conversation = Repo.preload(conversation, :participations),
232 fetch_latest_activity_id_for_context(conversation.ap_id, %{
234 "blocking_user" => user
236 if last_activity_id do
237 stream_out_participations(conversation.participations)
242 def stream_out_participations(_, _), do: :noop
244 def stream_out(%Activity{data: %{"type" => data_type}} = activity)
245 when data_type in ["Create", "Announce", "Delete"] do
247 |> Topics.get_activity_topics()
248 |> Streamer.stream(activity)
251 def stream_out(_activity) do
255 @spec create(map(), boolean()) :: {:ok, Activity.t()} | {:error, any()}
256 def create(params, fake \\ false) do
257 with {:ok, result} <- Repo.transaction(fn -> do_create(params, fake) end) do
262 defp do_create(%{to: to, actor: actor, context: context, object: object} = params, fake) do
263 additional = params[:additional] || %{}
264 # only accept false as false value
265 local = !(params[:local] == false)
266 published = params[:published]
267 quick_insert? = Config.get([:env]) == :benchmark
271 %{to: to, actor: actor, published: published, context: context, object: object},
274 {:ok, activity} <- insert(create_data, local, fake),
275 {:fake, false, activity} <- {:fake, fake, activity},
276 _ <- increase_replies_count_if_reply(create_data),
277 _ <- increase_poll_votes_if_vote(create_data),
278 {:quick_insert, false, activity} <- {:quick_insert, quick_insert?, activity},
279 {:ok, _actor} <- increase_note_count_if_public(actor, activity),
280 _ <- notify_and_stream(activity),
281 :ok <- maybe_federate(activity) do
284 {:quick_insert, true, activity} ->
287 {:fake, true, activity} ->
291 Repo.rollback(message)
295 @spec listen(map()) :: {:ok, Activity.t()} | {:error, any()}
296 def listen(%{to: to, actor: actor, context: context, object: object} = params) do
297 additional = params[:additional] || %{}
298 # only accept false as false value
299 local = !(params[:local] == false)
300 published = params[:published]
304 %{to: to, actor: actor, published: published, context: context, object: object},
307 {:ok, activity} <- insert(listen_data, local),
308 _ <- notify_and_stream(activity),
309 :ok <- maybe_federate(activity) do
314 @spec accept(map()) :: {:ok, Activity.t()} | {:error, any()}
315 def accept(params) do
316 accept_or_reject("Accept", params)
319 @spec reject(map()) :: {:ok, Activity.t()} | {:error, any()}
320 def reject(params) do
321 accept_or_reject("Reject", params)
324 @spec accept_or_reject(String.t(), map()) :: {:ok, Activity.t()} | {:error, any()}
325 def accept_or_reject(type, %{to: to, actor: actor, object: object} = params) do
326 local = Map.get(params, :local, true)
327 activity_id = Map.get(params, :activity_id, nil)
330 %{"to" => to, "type" => type, "actor" => actor.ap_id, "object" => object}
331 |> Utils.maybe_put("id", activity_id),
332 {:ok, activity} <- insert(data, local),
333 _ <- notify_and_stream(activity),
334 :ok <- maybe_federate(activity) do
339 @spec update(map()) :: {:ok, Activity.t()} | {:error, any()}
340 def update(%{to: to, cc: cc, actor: actor, object: object} = params) do
341 local = !(params[:local] == false)
342 activity_id = params[:activity_id]
351 data <- Utils.maybe_put(data, "id", activity_id),
352 {:ok, activity} <- insert(data, local),
353 _ <- notify_and_stream(activity),
354 :ok <- maybe_federate(activity) do
359 @spec follow(User.t(), User.t(), String.t() | nil, boolean()) ::
360 {:ok, Activity.t()} | {:error, any()}
361 def follow(follower, followed, activity_id \\ nil, local \\ true) do
362 with {:ok, result} <-
363 Repo.transaction(fn -> do_follow(follower, followed, activity_id, local) end) do
368 defp do_follow(follower, followed, activity_id, local) do
369 with data <- make_follow_data(follower, followed, activity_id),
370 {:ok, activity} <- insert(data, local),
371 _ <- notify_and_stream(activity),
372 :ok <- maybe_federate(activity) do
375 {:error, error} -> Repo.rollback(error)
379 @spec unfollow(User.t(), User.t(), String.t() | nil, boolean()) ::
380 {:ok, Activity.t()} | nil | {:error, any()}
381 def unfollow(follower, followed, activity_id \\ nil, local \\ true) do
382 with {:ok, result} <-
383 Repo.transaction(fn -> do_unfollow(follower, followed, activity_id, local) end) do
388 defp do_unfollow(follower, followed, activity_id, local) do
389 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
390 {:ok, follow_activity} <- update_follow_state(follow_activity, "cancelled"),
391 unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
392 {:ok, activity} <- insert(unfollow_data, local),
393 _ <- notify_and_stream(activity),
394 :ok <- maybe_federate(activity) do
398 {:error, error} -> Repo.rollback(error)
402 @spec block(User.t(), User.t(), String.t() | nil, boolean()) ::
403 {:ok, Activity.t()} | {:error, any()}
404 def block(blocker, blocked, activity_id \\ nil, local \\ true) do
405 with {:ok, result} <-
406 Repo.transaction(fn -> do_block(blocker, blocked, activity_id, local) end) do
411 defp do_block(blocker, blocked, activity_id, local) do
412 unfollow_blocked = Config.get([:activitypub, :unfollow_blocked])
414 if unfollow_blocked do
415 follow_activity = fetch_latest_follow(blocker, blocked)
416 if follow_activity, do: unfollow(blocker, blocked, nil, local)
419 with block_data <- make_block_data(blocker, blocked, activity_id),
420 {:ok, activity} <- insert(block_data, local),
421 _ <- notify_and_stream(activity),
422 :ok <- maybe_federate(activity) do
425 {:error, error} -> Repo.rollback(error)
429 @spec flag(map()) :: {:ok, Activity.t()} | {:error, any()}
439 # only accept false as false value
440 local = !(params[:local] == false)
441 forward = !(params[:forward] == false)
443 additional = params[:additional] || %{}
447 Map.merge(additional, %{"to" => [], "cc" => [account.ap_id]})
449 Map.merge(additional, %{"to" => [], "cc" => []})
452 with flag_data <- make_flag_data(params, additional),
453 {:ok, activity} <- insert(flag_data, local),
454 {:ok, stripped_activity} <- strip_report_status_data(activity),
455 _ <- notify_and_stream(activity),
456 :ok <- maybe_federate(stripped_activity) do
457 User.all_superusers()
458 |> Enum.filter(fn user -> not is_nil(user.email) end)
459 |> Enum.each(fn superuser ->
461 |> Pleroma.Emails.AdminEmail.report(actor, account, statuses, content)
462 |> Pleroma.Emails.Mailer.deliver_async()
469 @spec move(User.t(), User.t(), boolean()) :: {:ok, Activity.t()} | {:error, any()}
470 def move(%User{} = origin, %User{} = target, local \\ true) do
473 "actor" => origin.ap_id,
474 "object" => origin.ap_id,
475 "target" => target.ap_id
478 with true <- origin.ap_id in target.also_known_as,
479 {:ok, activity} <- insert(params, local),
480 _ <- notify_and_stream(activity) do
481 maybe_federate(activity)
483 BackgroundWorker.enqueue("move_following", %{
484 "origin_id" => origin.id,
485 "target_id" => target.id
490 false -> {:error, "Target account must have the origin in `alsoKnownAs`"}
495 def fetch_activities_for_context_query(context, opts) do
496 public = [Constants.as_public()]
500 do: [opts["user"].ap_id | User.following(opts["user"])] ++ public,
503 from(activity in Activity)
504 |> maybe_preload_objects(opts)
505 |> maybe_preload_bookmarks(opts)
506 |> maybe_set_thread_muted_field(opts)
507 |> restrict_blocked(opts)
508 |> restrict_recipients(recipients, opts["user"])
512 "?->>'type' = ? and ?->>'context' = ?",
519 |> exclude_poll_votes(opts)
521 |> order_by([activity], desc: activity.id)
524 @spec fetch_activities_for_context(String.t(), keyword() | map()) :: [Activity.t()]
525 def fetch_activities_for_context(context, opts \\ %{}) do
527 |> fetch_activities_for_context_query(opts)
531 @spec fetch_latest_activity_id_for_context(String.t(), keyword() | map()) ::
532 FlakeId.Ecto.CompatType.t() | nil
533 def fetch_latest_activity_id_for_context(context, opts \\ %{}) do
535 |> fetch_activities_for_context_query(Map.merge(%{"skip_preload" => true}, opts))
541 @spec fetch_public_or_unlisted_activities(map(), Pagination.type()) :: [Activity.t()]
542 def fetch_public_or_unlisted_activities(opts \\ %{}, pagination \\ :keyset) do
543 opts = Map.drop(opts, ["user"])
545 query = fetch_activities_query([Constants.as_public()], opts)
548 if opts["restrict_unlisted"] do
549 restrict_unlisted(query)
554 Pagination.fetch_paginated(query, opts, pagination)
557 @spec fetch_public_activities(map(), Pagination.type()) :: [Activity.t()]
558 def fetch_public_activities(opts \\ %{}, pagination \\ :keyset) do
560 |> Map.put("restrict_unlisted", true)
561 |> fetch_public_or_unlisted_activities(pagination)
564 @valid_visibilities ~w[direct unlisted public private]
566 defp restrict_visibility(query, %{visibility: visibility})
567 when is_list(visibility) do
568 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
574 "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(
652 %{"user" => %User{skip_thread_containment: true}},
657 defp restrict_thread_visibility(query, %{"user" => %User{ap_id: ap_id}}, _) do
660 where: fragment("thread_visibility(?, (?)->>'id') = true", ^ap_id, a.data)
664 defp restrict_thread_visibility(query, _, _), do: query
666 def fetch_user_abstract_activities(user, reading_user, params \\ %{}) do
669 |> Map.put("user", reading_user)
670 |> Map.put("actor_id", user.ap_id)
673 user_activities_recipients(%{
674 "godmode" => params["godmode"],
675 "reading_user" => reading_user
678 fetch_activities(recipients, params)
682 def fetch_user_activities(user, reading_user, params \\ %{}) do
685 |> Map.put("type", ["Create", "Announce"])
686 |> Map.put("user", reading_user)
687 |> Map.put("actor_id", user.ap_id)
688 |> Map.put("pinned_activity_ids", user.pinned_activities)
691 if User.blocks?(reading_user, user) do
695 |> Map.put("blocking_user", reading_user)
696 |> Map.put("muting_user", reading_user)
700 user_activities_recipients(%{
701 "godmode" => params["godmode"],
702 "reading_user" => reading_user
705 fetch_activities(recipients, params)
709 def fetch_statuses(reading_user, params) do
712 |> Map.put("type", ["Create", "Announce"])
715 user_activities_recipients(%{
716 "godmode" => params["godmode"],
717 "reading_user" => reading_user
720 fetch_activities(recipients, params, :offset)
724 defp user_activities_recipients(%{"godmode" => true}) do
728 defp user_activities_recipients(%{"reading_user" => reading_user}) do
730 [Constants.as_public()] ++ [reading_user.ap_id | User.following(reading_user)]
732 [Constants.as_public()]
736 defp restrict_since(query, %{"since_id" => ""}), do: query
738 defp restrict_since(query, %{"since_id" => since_id}) do
739 from(activity in query, where: activity.id > ^since_id)
742 defp restrict_since(query, _), do: query
744 defp restrict_tag_reject(_query, %{"tag_reject" => _tag_reject, "skip_preload" => true}) do
745 raise "Can't use the child object without preloading!"
748 defp restrict_tag_reject(query, %{"tag_reject" => tag_reject})
749 when is_list(tag_reject) and tag_reject != [] do
751 [_activity, object] in query,
752 where: fragment("not (?)->'tag' \\?| (?)", object.data, ^tag_reject)
756 defp restrict_tag_reject(query, _), do: query
758 defp restrict_tag_all(_query, %{"tag_all" => _tag_all, "skip_preload" => true}) do
759 raise "Can't use the child object without preloading!"
762 defp restrict_tag_all(query, %{"tag_all" => tag_all})
763 when is_list(tag_all) and tag_all != [] do
765 [_activity, object] in query,
766 where: fragment("(?)->'tag' \\?& (?)", object.data, ^tag_all)
770 defp restrict_tag_all(query, _), do: query
772 defp restrict_tag(_query, %{"tag" => _tag, "skip_preload" => true}) do
773 raise "Can't use the child object without preloading!"
776 defp restrict_tag(query, %{"tag" => tag}) when is_list(tag) do
778 [_activity, object] in query,
779 where: fragment("(?)->'tag' \\?| (?)", object.data, ^tag)
783 defp restrict_tag(query, %{"tag" => tag}) when is_binary(tag) do
785 [_activity, object] in query,
786 where: fragment("(?)->'tag' \\? (?)", object.data, ^tag)
790 defp restrict_tag(query, _), do: query
792 defp restrict_recipients(query, [], _user), do: query
794 defp restrict_recipients(query, recipients, nil) do
795 from(activity in query, where: fragment("? && ?", ^recipients, activity.recipients))
798 defp restrict_recipients(query, recipients, user) do
801 where: fragment("? && ?", ^recipients, activity.recipients),
802 or_where: activity.actor == ^user.ap_id
806 defp restrict_local(query, %{"local_only" => true}) do
807 from(activity in query, where: activity.local == true)
810 defp restrict_local(query, _), do: query
812 defp restrict_actor(query, %{"actor_id" => actor_id}) do
813 from(activity in query, where: activity.actor == ^actor_id)
816 defp restrict_actor(query, _), do: query
818 defp restrict_type(query, %{"type" => type}) when is_binary(type) do
819 from(activity in query, where: fragment("?->>'type' = ?", activity.data, ^type))
822 defp restrict_type(query, %{"type" => type}) do
823 from(activity in query, where: fragment("?->>'type' = ANY(?)", activity.data, ^type))
826 defp restrict_type(query, _), do: query
828 defp restrict_state(query, %{"state" => state}) do
829 from(activity in query, where: fragment("?->>'state' = ?", activity.data, ^state))
832 defp restrict_state(query, _), do: query
834 defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
836 [_activity, object] in query,
837 where: fragment("(?)->'likes' \\? (?)", object.data, ^ap_id)
841 defp restrict_favorited_by(query, _), do: query
843 defp restrict_media(_query, %{"only_media" => _val, "skip_preload" => true}) do
844 raise "Can't use the child object without preloading!"
847 defp restrict_media(query, %{"only_media" => val}) when val in [true, "true", "1"] do
849 [_activity, object] in query,
850 where: fragment("not (?)->'attachment' = (?)", object.data, ^[])
854 defp restrict_media(query, _), do: query
856 defp restrict_replies(query, %{"exclude_replies" => val}) when val in [true, "true", "1"] do
858 [_activity, object] in query,
859 where: fragment("?->>'inReplyTo' is null", object.data)
863 defp restrict_replies(query, %{
864 "reply_filtering_user" => user,
865 "reply_visibility" => "self"
868 [activity, object] in query,
871 "?->>'inReplyTo' is null OR ? = ANY(?)",
879 defp restrict_replies(query, %{
880 "reply_filtering_user" => user,
881 "reply_visibility" => "following"
884 [activity, object] in query,
887 "?->>'inReplyTo' is null OR ? && array_remove(?, ?) OR ? = ?",
889 ^[user.ap_id | User.get_cached_user_friends_ap_ids(user)],
898 defp restrict_replies(query, _), do: query
900 defp restrict_reblogs(query, %{"exclude_reblogs" => val}) when val in [true, "true", "1"] do
901 from(activity in query, where: fragment("?->>'type' != 'Announce'", activity.data))
904 defp restrict_reblogs(query, _), do: query
906 defp restrict_muted(query, %{"with_muted" => val}) when val in [true, "true", "1"], do: query
908 defp restrict_muted(query, %{"muting_user" => %User{} = user} = opts) do
909 mutes = opts["muted_users_ap_ids"] || User.muted_users_ap_ids(user)
912 from([activity] in query,
913 where: fragment("not (? = ANY(?))", activity.actor, ^mutes),
914 where: fragment("not (?->'to' \\?| ?)", activity.data, ^mutes)
917 unless opts["skip_preload"] do
918 from([thread_mute: tm] in query, where: is_nil(tm.user_id))
924 defp restrict_muted(query, _), do: query
926 defp restrict_blocked(query, %{"blocking_user" => %User{} = user} = opts) do
927 blocked_ap_ids = opts["blocked_users_ap_ids"] || User.blocked_users_ap_ids(user)
928 domain_blocks = user.domain_blocks || []
930 following_ap_ids = User.get_friends_ap_ids(user)
933 if has_named_binding?(query, :object), do: query, else: Activity.with_joined_object(query)
936 [activity, object: o] in query,
937 where: fragment("not (? = ANY(?))", activity.actor, ^blocked_ap_ids),
938 where: fragment("not (? && ?)", activity.recipients, ^blocked_ap_ids),
941 "recipients_contain_blocked_domains(?, ?) = false",
947 "not (?->>'type' = 'Announce' and ?->'to' \\?| ?)",
954 "(not (split_part(?, '/', 3) = ANY(?))) or ? = ANY(?)",
962 "(not (split_part(?->>'actor', '/', 3) = ANY(?))) or (?->>'actor') = ANY(?)",
971 defp restrict_blocked(query, _), do: query
973 defp restrict_unlisted(query) do
978 "not (coalesce(?->'cc', '{}'::jsonb) \\?| ?)",
980 ^[Constants.as_public()]
985 # TODO: when all endpoints migrated to OpenAPI compare `pinned` with `true` (boolean) only,
986 # the same for `restrict_media/2`, `restrict_replies/2`, 'restrict_reblogs/2'
987 # and `restrict_muted/2`
989 defp restrict_pinned(query, %{"pinned" => pinned, "pinned_activity_ids" => ids})
990 when pinned in [true, "true", "1"] do
991 from(activity in query, where: activity.id in ^ids)
994 defp restrict_pinned(query, _), do: query
996 defp restrict_muted_reblogs(query, %{"muting_user" => %User{} = user} = opts) do
997 muted_reblogs = opts["reblog_muted_users_ap_ids"] || User.reblog_muted_users_ap_ids(user)
1003 "not ( ?->>'type' = 'Announce' and ? = ANY(?))",
1011 defp restrict_muted_reblogs(query, _), do: query
1013 defp restrict_instance(query, %{"instance" => instance}) do
1018 where: fragment("? LIKE ?", u.nickname, ^"%@#{instance}")
1022 from(activity in query, where: activity.actor in ^users)
1025 defp restrict_instance(query, _), do: query
1027 defp exclude_poll_votes(query, %{"include_poll_votes" => true}), do: query
1029 defp exclude_poll_votes(query, _) do
1030 if has_named_binding?(query, :object) do
1031 from([activity, object: o] in query,
1032 where: fragment("not(?->>'type' = ?)", o.data, "Answer")
1039 defp exclude_invisible_actors(query, %{"invisible_actors" => true}), do: query
1041 defp exclude_invisible_actors(query, _opts) do
1043 User.Query.build(%{invisible: true, select: [:ap_id]})
1045 |> Enum.map(fn %{ap_id: ap_id} -> ap_id end)
1047 from([activity] in query, where: activity.actor not in ^invisible_ap_ids)
1050 defp exclude_id(query, %{"exclude_id" => id}) when is_binary(id) do
1051 from(activity in query, where: activity.id != ^id)
1054 defp exclude_id(query, _), do: query
1056 defp maybe_preload_objects(query, %{"skip_preload" => true}), do: query
1058 defp maybe_preload_objects(query, _) do
1060 |> Activity.with_preloaded_object()
1063 defp maybe_preload_bookmarks(query, %{"skip_preload" => true}), do: query
1065 defp maybe_preload_bookmarks(query, opts) do
1067 |> Activity.with_preloaded_bookmark(opts["user"])
1070 defp maybe_preload_report_notes(query, %{"preload_report_notes" => true}) do
1072 |> Activity.with_preloaded_report_notes()
1075 defp maybe_preload_report_notes(query, _), do: query
1077 defp maybe_set_thread_muted_field(query, %{"skip_preload" => true}), do: query
1079 defp maybe_set_thread_muted_field(query, opts) do
1081 |> Activity.with_set_thread_muted_field(opts["muting_user"] || opts["user"])
1084 defp maybe_order(query, %{order: :desc}) do
1086 |> order_by(desc: :id)
1089 defp maybe_order(query, %{order: :asc}) do
1091 |> order_by(asc: :id)
1094 defp maybe_order(query, _), do: query
1096 defp fetch_activities_query_ap_ids_ops(opts) do
1097 source_user = opts["muting_user"]
1098 ap_id_relationships = if source_user, do: [:mute, :reblog_mute], else: []
1100 ap_id_relationships =
1101 ap_id_relationships ++
1102 if opts["blocking_user"] && opts["blocking_user"] == source_user do
1108 preloaded_ap_ids = User.outgoing_relationships_ap_ids(source_user, ap_id_relationships)
1110 restrict_blocked_opts = Map.merge(%{"blocked_users_ap_ids" => preloaded_ap_ids[:block]}, opts)
1111 restrict_muted_opts = Map.merge(%{"muted_users_ap_ids" => preloaded_ap_ids[:mute]}, opts)
1113 restrict_muted_reblogs_opts =
1114 Map.merge(%{"reblog_muted_users_ap_ids" => preloaded_ap_ids[:reblog_mute]}, opts)
1116 {restrict_blocked_opts, restrict_muted_opts, restrict_muted_reblogs_opts}
1119 def fetch_activities_query(recipients, opts \\ %{}) do
1120 {restrict_blocked_opts, restrict_muted_opts, restrict_muted_reblogs_opts} =
1121 fetch_activities_query_ap_ids_ops(opts)
1124 skip_thread_containment: Config.get([:instance, :skip_thread_containment])
1128 |> maybe_preload_objects(opts)
1129 |> maybe_preload_bookmarks(opts)
1130 |> maybe_preload_report_notes(opts)
1131 |> maybe_set_thread_muted_field(opts)
1132 |> maybe_order(opts)
1133 |> restrict_recipients(recipients, opts["user"])
1134 |> restrict_replies(opts)
1135 |> restrict_tag(opts)
1136 |> restrict_tag_reject(opts)
1137 |> restrict_tag_all(opts)
1138 |> restrict_since(opts)
1139 |> restrict_local(opts)
1140 |> restrict_actor(opts)
1141 |> restrict_type(opts)
1142 |> restrict_state(opts)
1143 |> restrict_favorited_by(opts)
1144 |> restrict_blocked(restrict_blocked_opts)
1145 |> restrict_muted(restrict_muted_opts)
1146 |> restrict_media(opts)
1147 |> restrict_visibility(opts)
1148 |> restrict_thread_visibility(opts, config)
1149 |> restrict_reblogs(opts)
1150 |> restrict_pinned(opts)
1151 |> restrict_muted_reblogs(restrict_muted_reblogs_opts)
1152 |> restrict_instance(opts)
1153 |> Activity.restrict_deactivated_users()
1154 |> exclude_poll_votes(opts)
1155 |> exclude_invisible_actors(opts)
1156 |> exclude_visibility(opts)
1159 def fetch_activities(recipients, opts \\ %{}, pagination \\ :keyset) do
1160 list_memberships = Pleroma.List.memberships(opts["user"])
1162 fetch_activities_query(recipients ++ list_memberships, opts)
1163 |> Pagination.fetch_paginated(opts, pagination)
1165 |> maybe_update_cc(list_memberships, opts["user"])
1169 Fetch favorites activities of user with order by sort adds to favorites
1171 @spec fetch_favourites(User.t(), map(), Pagination.type()) :: list(Activity.t())
1172 def fetch_favourites(user, params \\ %{}, pagination \\ :keyset) do
1174 |> Activity.Queries.by_actor()
1175 |> Activity.Queries.by_type("Like")
1176 |> Activity.with_joined_object()
1177 |> Object.with_joined_activity()
1178 |> select([_like, object, activity], %{activity | object: object})
1179 |> order_by([like, _, _], desc_nulls_last: like.id)
1180 |> Pagination.fetch_paginated(
1181 Map.merge(params, %{"skip_order" => true}),
1187 defp maybe_update_cc(activities, list_memberships, %User{ap_id: user_ap_id})
1188 when is_list(list_memberships) and length(list_memberships) > 0 do
1189 Enum.map(activities, fn
1190 %{data: %{"bcc" => bcc}} = activity when is_list(bcc) and length(bcc) > 0 ->
1191 if Enum.any?(bcc, &(&1 in list_memberships)) do
1192 update_in(activity.data["cc"], &[user_ap_id | &1])
1202 defp maybe_update_cc(activities, _, _), do: activities
1204 def fetch_activities_bounded_query(query, recipients, recipients_with_public) do
1205 from(activity in query,
1207 fragment("? && ?", activity.recipients, ^recipients) or
1208 (fragment("? && ?", activity.recipients, ^recipients_with_public) and
1209 ^Constants.as_public() in activity.recipients)
1213 def fetch_activities_bounded(
1215 recipients_with_public,
1217 pagination \\ :keyset
1219 fetch_activities_query([], opts)
1220 |> fetch_activities_bounded_query(recipients, recipients_with_public)
1221 |> Pagination.fetch_paginated(opts, pagination)
1225 @spec upload(Upload.source(), keyword()) :: {:ok, Object.t()} | {:error, any()}
1226 def upload(file, opts \\ []) do
1227 with {:ok, data} <- Upload.store(file, opts) do
1230 Map.put(data, "actor", opts[:actor])
1235 Repo.insert(%Object{data: obj_data})
1239 @spec get_actor_url(any()) :: binary() | nil
1240 defp get_actor_url(url) when is_binary(url), do: url
1241 defp get_actor_url(%{"href" => href}) when is_binary(href), do: href
1243 defp get_actor_url(url) when is_list(url) do
1249 defp get_actor_url(_url), do: nil
1251 defp object_to_user_data(data) do
1253 data["icon"]["url"] &&
1256 "url" => [%{"href" => data["icon"]["url"]}]
1260 data["image"]["url"] &&
1263 "url" => [%{"href" => data["image"]["url"]}]
1268 |> Map.get("attachment", [])
1269 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
1270 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
1274 |> Map.get("tag", [])
1276 %{"type" => "Emoji"} -> true
1279 |> Enum.reduce(%{}, fn %{"icon" => %{"url" => url}, "name" => name}, acc ->
1280 Map.put(acc, String.trim(name, ":"), url)
1283 locked = data["manuallyApprovesFollowers"] || false
1284 data = Transmogrifier.maybe_fix_user_object(data)
1285 discoverable = data["discoverable"] || false
1286 invisible = data["invisible"] || false
1287 actor_type = data["type"] || "Person"
1290 if is_map(data["publicKey"]) && is_binary(data["publicKey"]["publicKeyPem"]) do
1291 data["publicKey"]["publicKeyPem"]
1297 if is_map(data["endpoints"]) && is_binary(data["endpoints"]["sharedInbox"]) do
1298 data["endpoints"]["sharedInbox"]
1305 uri: get_actor_url(data["url"]),
1311 discoverable: discoverable,
1312 invisible: invisible,
1315 follower_address: data["followers"],
1316 following_address: data["following"],
1317 bio: data["summary"],
1318 actor_type: actor_type,
1319 also_known_as: Map.get(data, "alsoKnownAs", []),
1320 public_key: public_key,
1321 inbox: data["inbox"],
1322 shared_inbox: shared_inbox
1325 # nickname can be nil because of virtual actors
1327 if data["preferredUsername"] do
1331 "#{data["preferredUsername"]}@#{URI.parse(data["id"]).host}"
1334 Map.put(user_data, :nickname, nil)
1340 def fetch_follow_information_for_user(user) do
1341 with {:ok, following_data} <-
1342 Fetcher.fetch_and_contain_remote_object_from_id(user.following_address),
1343 {:ok, hide_follows} <- collection_private(following_data),
1344 {:ok, followers_data} <-
1345 Fetcher.fetch_and_contain_remote_object_from_id(user.follower_address),
1346 {:ok, hide_followers} <- collection_private(followers_data) do
1349 hide_follows: hide_follows,
1350 follower_count: normalize_counter(followers_data["totalItems"]),
1351 following_count: normalize_counter(following_data["totalItems"]),
1352 hide_followers: hide_followers
1355 {:error, _} = e -> e
1360 defp normalize_counter(counter) when is_integer(counter), do: counter
1361 defp normalize_counter(_), do: 0
1363 def maybe_update_follow_information(user_data) do
1364 with {:enabled, true} <- {:enabled, Config.get([:instance, :external_user_synchronization])},
1365 {_, true} <- {:user_type_check, user_data[:type] in ["Person", "Service"]},
1367 {:collections_available,
1368 !!(user_data[:following_address] && user_data[:follower_address])},
1370 fetch_follow_information_for_user(user_data) do
1371 info = Map.merge(user_data[:info] || %{}, info)
1374 |> Map.put(:info, info)
1376 {:user_type_check, false} ->
1379 {:collections_available, false} ->
1382 {:enabled, false} ->
1387 "Follower/Following counter update for #{user_data.ap_id} failed.\n" <> inspect(e)
1394 defp collection_private(%{"first" => %{"type" => type}})
1395 when type in ["CollectionPage", "OrderedCollectionPage"],
1398 defp collection_private(%{"first" => first}) do
1399 with {:ok, %{"type" => type}} when type in ["CollectionPage", "OrderedCollectionPage"] <-
1400 Fetcher.fetch_and_contain_remote_object_from_id(first) do
1403 {:error, {:ok, %{status: code}}} when code in [401, 403] -> {:ok, true}
1404 {:error, _} = e -> e
1409 defp collection_private(_data), do: {:ok, true}
1411 def user_data_from_user_object(data) do
1412 with {:ok, data} <- MRF.filter(data),
1413 {:ok, data} <- object_to_user_data(data) do
1420 def fetch_and_prepare_user_from_ap_id(ap_id) do
1421 with {:ok, data} <- Fetcher.fetch_and_contain_remote_object_from_id(ap_id),
1422 {:ok, data} <- user_data_from_user_object(data),
1423 data <- maybe_update_follow_information(data) do
1426 {:error, "Object has been deleted"} = e ->
1427 Logger.debug("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
1431 Logger.error("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
1436 def make_user_from_ap_id(ap_id) do
1437 user = User.get_cached_by_ap_id(ap_id)
1439 if user && !User.ap_enabled?(user) do
1440 Transmogrifier.upgrade_user_from_ap_id(ap_id)
1442 with {:ok, data} <- fetch_and_prepare_user_from_ap_id(ap_id) do
1445 |> User.remote_user_changeset(data)
1446 |> User.update_and_set_cache()
1449 |> User.remote_user_changeset()
1459 def make_user_from_nickname(nickname) do
1460 with {:ok, %{"ap_id" => ap_id}} when not is_nil(ap_id) <- WebFinger.finger(nickname) do
1461 make_user_from_ap_id(ap_id)
1463 _e -> {:error, "No AP id in WebFinger"}
1467 # filter out broken threads
1468 def contain_broken_threads(%Activity{} = activity, %User{} = user) do
1469 entire_thread_visible_for_user?(activity, user)
1472 # do post-processing on a specific activity
1473 def contain_activity(%Activity{} = activity, %User{} = user) do
1474 contain_broken_threads(activity, user)
1477 def fetch_direct_messages_query do
1479 |> restrict_type(%{"type" => "Create"})
1480 |> restrict_visibility(%{visibility: "direct"})
1481 |> order_by([activity], asc: activity.id)