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 announce(User.t(), Object.t(), String.t() | nil, boolean(), boolean()) ::
360 {:ok, Activity.t(), Object.t()} | {:error, any()}
362 %User{ap_id: _} = user,
363 %Object{data: %{"id" => _}} = object,
368 with {:ok, result} <-
369 Repo.transaction(fn -> do_announce(user, object, activity_id, local, public) end) do
374 defp do_announce(user, object, activity_id, local, public) do
375 with true <- is_announceable?(object, user, public),
376 object <- Object.get_by_id(object.id),
377 announce_data <- make_announce_data(user, object, activity_id, public),
378 {:ok, activity} <- insert(announce_data, local),
379 {:ok, object} <- add_announce_to_object(activity, object),
380 _ <- notify_and_stream(activity),
381 :ok <- maybe_federate(activity) do
382 {:ok, activity, object}
384 false -> {:error, false}
385 {:error, error} -> Repo.rollback(error)
389 @spec follow(User.t(), User.t(), String.t() | nil, boolean()) ::
390 {:ok, Activity.t()} | {:error, any()}
391 def follow(follower, followed, activity_id \\ nil, local \\ true) do
392 with {:ok, result} <-
393 Repo.transaction(fn -> do_follow(follower, followed, activity_id, local) end) do
398 defp do_follow(follower, followed, activity_id, local) do
399 with data <- make_follow_data(follower, followed, activity_id),
400 {:ok, activity} <- insert(data, local),
401 _ <- notify_and_stream(activity),
402 :ok <- maybe_federate(activity) do
405 {:error, error} -> Repo.rollback(error)
409 @spec unfollow(User.t(), User.t(), String.t() | nil, boolean()) ::
410 {:ok, Activity.t()} | nil | {:error, any()}
411 def unfollow(follower, followed, activity_id \\ nil, local \\ true) do
412 with {:ok, result} <-
413 Repo.transaction(fn -> do_unfollow(follower, followed, activity_id, local) end) do
418 defp do_unfollow(follower, followed, activity_id, local) do
419 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
420 {:ok, follow_activity} <- update_follow_state(follow_activity, "cancelled"),
421 unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
422 {:ok, activity} <- insert(unfollow_data, local),
423 _ <- notify_and_stream(activity),
424 :ok <- maybe_federate(activity) do
428 {:error, error} -> Repo.rollback(error)
432 @spec block(User.t(), User.t(), String.t() | nil, boolean()) ::
433 {:ok, Activity.t()} | {:error, any()}
434 def block(blocker, blocked, activity_id \\ nil, local \\ true) do
435 with {:ok, result} <-
436 Repo.transaction(fn -> do_block(blocker, blocked, activity_id, local) end) do
441 defp do_block(blocker, blocked, activity_id, local) do
442 outgoing_blocks = Config.get([:activitypub, :outgoing_blocks])
443 unfollow_blocked = Config.get([:activitypub, :unfollow_blocked])
445 if unfollow_blocked do
446 follow_activity = fetch_latest_follow(blocker, blocked)
447 if follow_activity, do: unfollow(blocker, blocked, nil, local)
450 with true <- outgoing_blocks,
451 block_data <- make_block_data(blocker, blocked, activity_id),
452 {:ok, activity} <- insert(block_data, local),
453 _ <- notify_and_stream(activity),
454 :ok <- maybe_federate(activity) do
457 {:error, error} -> Repo.rollback(error)
461 @spec flag(map()) :: {:ok, Activity.t()} | {:error, any()}
471 # only accept false as false value
472 local = !(params[:local] == false)
473 forward = !(params[:forward] == false)
475 additional = params[:additional] || %{}
479 Map.merge(additional, %{"to" => [], "cc" => [account.ap_id]})
481 Map.merge(additional, %{"to" => [], "cc" => []})
484 with flag_data <- make_flag_data(params, additional),
485 {:ok, activity} <- insert(flag_data, local),
486 {:ok, stripped_activity} <- strip_report_status_data(activity),
487 _ <- notify_and_stream(activity),
488 :ok <- maybe_federate(stripped_activity) do
489 User.all_superusers()
490 |> Enum.filter(fn user -> not is_nil(user.email) end)
491 |> Enum.each(fn superuser ->
493 |> Pleroma.Emails.AdminEmail.report(actor, account, statuses, content)
494 |> Pleroma.Emails.Mailer.deliver_async()
501 @spec move(User.t(), User.t(), boolean()) :: {:ok, Activity.t()} | {:error, any()}
502 def move(%User{} = origin, %User{} = target, local \\ true) do
505 "actor" => origin.ap_id,
506 "object" => origin.ap_id,
507 "target" => target.ap_id
510 with true <- origin.ap_id in target.also_known_as,
511 {:ok, activity} <- insert(params, local),
512 _ <- notify_and_stream(activity) do
513 maybe_federate(activity)
515 BackgroundWorker.enqueue("move_following", %{
516 "origin_id" => origin.id,
517 "target_id" => target.id
522 false -> {:error, "Target account must have the origin in `alsoKnownAs`"}
527 def fetch_activities_for_context_query(context, opts) do
528 public = [Constants.as_public()]
532 do: [opts["user"].ap_id | User.following(opts["user"])] ++ public,
535 from(activity in Activity)
536 |> maybe_preload_objects(opts)
537 |> maybe_preload_bookmarks(opts)
538 |> maybe_set_thread_muted_field(opts)
539 |> restrict_blocked(opts)
540 |> restrict_recipients(recipients, opts["user"])
544 "?->>'type' = ? and ?->>'context' = ?",
551 |> exclude_poll_votes(opts)
553 |> order_by([activity], desc: activity.id)
556 @spec fetch_activities_for_context(String.t(), keyword() | map()) :: [Activity.t()]
557 def fetch_activities_for_context(context, opts \\ %{}) do
559 |> fetch_activities_for_context_query(opts)
563 @spec fetch_latest_activity_id_for_context(String.t(), keyword() | map()) ::
564 FlakeId.Ecto.CompatType.t() | nil
565 def fetch_latest_activity_id_for_context(context, opts \\ %{}) do
567 |> fetch_activities_for_context_query(Map.merge(%{"skip_preload" => true}, opts))
573 @spec fetch_public_activities(map(), Pagination.type()) :: [Activity.t()]
574 def fetch_public_activities(opts \\ %{}, pagination \\ :keyset) do
575 opts = Map.drop(opts, ["user"])
577 [Constants.as_public()]
578 |> fetch_activities_query(opts)
579 |> restrict_unlisted()
580 |> Pagination.fetch_paginated(opts, pagination)
583 @valid_visibilities ~w[direct unlisted public private]
585 defp restrict_visibility(query, %{visibility: visibility})
586 when is_list(visibility) do
587 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
593 "activity_visibility(?, ?, ?) = ANY (?)",
603 Logger.error("Could not restrict visibility to #{visibility}")
607 defp restrict_visibility(query, %{visibility: visibility})
608 when visibility in @valid_visibilities do
612 fragment("activity_visibility(?, ?, ?) = ?", a.actor, a.recipients, a.data, ^visibility)
616 defp restrict_visibility(_query, %{visibility: visibility})
617 when visibility not in @valid_visibilities do
618 Logger.error("Could not restrict visibility to #{visibility}")
621 defp restrict_visibility(query, _visibility), do: query
623 defp exclude_visibility(query, %{"exclude_visibilities" => visibility})
624 when is_list(visibility) do
625 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
630 "activity_visibility(?, ?, ?) = ANY (?)",
638 Logger.error("Could not exclude visibility to #{visibility}")
643 defp exclude_visibility(query, %{"exclude_visibilities" => visibility})
644 when visibility in @valid_visibilities do
649 "activity_visibility(?, ?, ?) = ?",
658 defp exclude_visibility(query, %{"exclude_visibilities" => visibility})
659 when visibility not in [nil | @valid_visibilities] do
660 Logger.error("Could not exclude visibility to #{visibility}")
664 defp exclude_visibility(query, _visibility), do: query
666 defp restrict_thread_visibility(query, _, %{skip_thread_containment: true} = _),
669 defp restrict_thread_visibility(
671 %{"user" => %User{skip_thread_containment: true}},
676 defp restrict_thread_visibility(query, %{"user" => %User{ap_id: ap_id}}, _) do
679 where: fragment("thread_visibility(?, (?)->>'id') = true", ^ap_id, a.data)
683 defp restrict_thread_visibility(query, _, _), do: query
685 def fetch_user_abstract_activities(user, reading_user, params \\ %{}) do
688 |> Map.put("user", reading_user)
689 |> Map.put("actor_id", user.ap_id)
692 user_activities_recipients(%{
693 "godmode" => params["godmode"],
694 "reading_user" => reading_user
697 fetch_activities(recipients, params)
701 def fetch_user_activities(user, reading_user, params \\ %{}) do
704 |> Map.put("type", ["Create", "Announce"])
705 |> Map.put("user", reading_user)
706 |> Map.put("actor_id", user.ap_id)
707 |> Map.put("pinned_activity_ids", user.pinned_activities)
710 if User.blocks?(reading_user, user) do
714 |> Map.put("blocking_user", reading_user)
715 |> Map.put("muting_user", reading_user)
719 user_activities_recipients(%{
720 "godmode" => params["godmode"],
721 "reading_user" => reading_user
724 fetch_activities(recipients, params)
728 def fetch_statuses(reading_user, params) do
731 |> Map.put("type", ["Create", "Announce"])
734 user_activities_recipients(%{
735 "godmode" => params["godmode"],
736 "reading_user" => reading_user
739 fetch_activities(recipients, params, :offset)
743 defp user_activities_recipients(%{"godmode" => true}) do
747 defp user_activities_recipients(%{"reading_user" => reading_user}) do
749 [Constants.as_public()] ++ [reading_user.ap_id | User.following(reading_user)]
751 [Constants.as_public()]
755 defp restrict_since(query, %{"since_id" => ""}), do: query
757 defp restrict_since(query, %{"since_id" => since_id}) do
758 from(activity in query, where: activity.id > ^since_id)
761 defp restrict_since(query, _), do: query
763 defp restrict_tag_reject(_query, %{"tag_reject" => _tag_reject, "skip_preload" => true}) do
764 raise "Can't use the child object without preloading!"
767 defp restrict_tag_reject(query, %{"tag_reject" => tag_reject})
768 when is_list(tag_reject) and tag_reject != [] do
770 [_activity, object] in query,
771 where: fragment("not (?)->'tag' \\?| (?)", object.data, ^tag_reject)
775 defp restrict_tag_reject(query, _), do: query
777 defp restrict_tag_all(_query, %{"tag_all" => _tag_all, "skip_preload" => true}) do
778 raise "Can't use the child object without preloading!"
781 defp restrict_tag_all(query, %{"tag_all" => tag_all})
782 when is_list(tag_all) and tag_all != [] do
784 [_activity, object] in query,
785 where: fragment("(?)->'tag' \\?& (?)", object.data, ^tag_all)
789 defp restrict_tag_all(query, _), do: query
791 defp restrict_tag(_query, %{"tag" => _tag, "skip_preload" => true}) do
792 raise "Can't use the child object without preloading!"
795 defp restrict_tag(query, %{"tag" => tag}) when is_list(tag) do
797 [_activity, object] in query,
798 where: fragment("(?)->'tag' \\?| (?)", object.data, ^tag)
802 defp restrict_tag(query, %{"tag" => tag}) when is_binary(tag) do
804 [_activity, object] in query,
805 where: fragment("(?)->'tag' \\? (?)", object.data, ^tag)
809 defp restrict_tag(query, _), do: query
811 defp restrict_recipients(query, [], _user), do: query
813 defp restrict_recipients(query, recipients, nil) do
814 from(activity in query, where: fragment("? && ?", ^recipients, activity.recipients))
817 defp restrict_recipients(query, recipients, user) do
820 where: fragment("? && ?", ^recipients, activity.recipients),
821 or_where: activity.actor == ^user.ap_id
825 defp restrict_local(query, %{"local_only" => true}) do
826 from(activity in query, where: activity.local == true)
829 defp restrict_local(query, _), do: query
831 defp restrict_actor(query, %{"actor_id" => actor_id}) do
832 from(activity in query, where: activity.actor == ^actor_id)
835 defp restrict_actor(query, _), do: query
837 defp restrict_type(query, %{"type" => type}) when is_binary(type) do
838 from(activity in query, where: fragment("?->>'type' = ?", activity.data, ^type))
841 defp restrict_type(query, %{"type" => type}) do
842 from(activity in query, where: fragment("?->>'type' = ANY(?)", activity.data, ^type))
845 defp restrict_type(query, _), do: query
847 defp restrict_state(query, %{"state" => state}) do
848 from(activity in query, where: fragment("?->>'state' = ?", activity.data, ^state))
851 defp restrict_state(query, _), do: query
853 defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
855 [_activity, object] in query,
856 where: fragment("(?)->'likes' \\? (?)", object.data, ^ap_id)
860 defp restrict_favorited_by(query, _), do: query
862 defp restrict_media(_query, %{"only_media" => _val, "skip_preload" => true}) do
863 raise "Can't use the child object without preloading!"
866 defp restrict_media(query, %{"only_media" => val}) when val in [true, "true", "1"] do
868 [_activity, object] in query,
869 where: fragment("not (?)->'attachment' = (?)", object.data, ^[])
873 defp restrict_media(query, _), do: query
875 defp restrict_replies(query, %{"exclude_replies" => val}) when val in [true, "true", "1"] do
877 [_activity, object] in query,
878 where: fragment("?->>'inReplyTo' is null", object.data)
882 defp restrict_replies(query, %{
883 "reply_filtering_user" => user,
884 "reply_visibility" => "self"
887 [activity, object] in query,
890 "?->>'inReplyTo' is null OR ? = ANY(?)",
898 defp restrict_replies(query, %{
899 "reply_filtering_user" => user,
900 "reply_visibility" => "following"
903 [activity, object] in query,
906 "?->>'inReplyTo' is null OR ? && array_remove(?, ?) OR ? = ?",
908 ^[user.ap_id | User.get_cached_user_friends_ap_ids(user)],
917 defp restrict_replies(query, _), do: query
919 defp restrict_reblogs(query, %{"exclude_reblogs" => val}) when val in [true, "true", "1"] do
920 from(activity in query, where: fragment("?->>'type' != 'Announce'", activity.data))
923 defp restrict_reblogs(query, _), do: query
925 defp restrict_muted(query, %{"with_muted" => val}) when val in [true, "true", "1"], do: query
927 defp restrict_muted(query, %{"muting_user" => %User{} = user} = opts) do
928 mutes = opts["muted_users_ap_ids"] || User.muted_users_ap_ids(user)
931 from([activity] in query,
932 where: fragment("not (? = ANY(?))", activity.actor, ^mutes),
933 where: fragment("not (?->'to' \\?| ?)", activity.data, ^mutes)
936 unless opts["skip_preload"] do
937 from([thread_mute: tm] in query, where: is_nil(tm.user_id))
943 defp restrict_muted(query, _), do: query
945 defp restrict_blocked(query, %{"blocking_user" => %User{} = user} = opts) do
946 blocked_ap_ids = opts["blocked_users_ap_ids"] || User.blocked_users_ap_ids(user)
947 domain_blocks = user.domain_blocks || []
949 following_ap_ids = User.get_friends_ap_ids(user)
952 if has_named_binding?(query, :object), do: query, else: Activity.with_joined_object(query)
955 [activity, object: o] in query,
956 where: fragment("not (? = ANY(?))", activity.actor, ^blocked_ap_ids),
957 where: fragment("not (? && ?)", activity.recipients, ^blocked_ap_ids),
960 "not (?->>'type' = 'Announce' and ?->'to' \\?| ?)",
967 "(not (split_part(?, '/', 3) = ANY(?))) or ? = ANY(?)",
975 "(not (split_part(?->>'actor', '/', 3) = ANY(?))) or (?->>'actor') = ANY(?)",
984 defp restrict_blocked(query, _), do: query
986 defp restrict_unlisted(query) do
991 "not (coalesce(?->'cc', '{}'::jsonb) \\?| ?)",
993 ^[Constants.as_public()]
998 # TODO: when all endpoints migrated to OpenAPI compare `pinned` with `true` (boolean) only,
999 # the same for `restrict_media/2`, `restrict_replies/2`, 'restrict_reblogs/2'
1000 # and `restrict_muted/2`
1002 defp restrict_pinned(query, %{"pinned" => pinned, "pinned_activity_ids" => ids})
1003 when pinned in [true, "true", "1"] do
1004 from(activity in query, where: activity.id in ^ids)
1007 defp restrict_pinned(query, _), do: query
1009 defp restrict_muted_reblogs(query, %{"muting_user" => %User{} = user} = opts) do
1010 muted_reblogs = opts["reblog_muted_users_ap_ids"] || User.reblog_muted_users_ap_ids(user)
1016 "not ( ?->>'type' = 'Announce' and ? = ANY(?))",
1024 defp restrict_muted_reblogs(query, _), do: query
1026 defp restrict_instance(query, %{"instance" => instance}) do
1031 where: fragment("? LIKE ?", u.nickname, ^"%@#{instance}")
1035 from(activity in query, where: activity.actor in ^users)
1038 defp restrict_instance(query, _), do: query
1040 defp exclude_poll_votes(query, %{"include_poll_votes" => true}), do: query
1042 defp exclude_poll_votes(query, _) do
1043 if has_named_binding?(query, :object) do
1044 from([activity, object: o] in query,
1045 where: fragment("not(?->>'type' = ?)", o.data, "Answer")
1052 defp exclude_id(query, %{"exclude_id" => id}) when is_binary(id) do
1053 from(activity in query, where: activity.id != ^id)
1056 defp exclude_id(query, _), do: query
1058 defp maybe_preload_objects(query, %{"skip_preload" => true}), do: query
1060 defp maybe_preload_objects(query, _) do
1062 |> Activity.with_preloaded_object()
1065 defp maybe_preload_bookmarks(query, %{"skip_preload" => true}), do: query
1067 defp maybe_preload_bookmarks(query, opts) do
1069 |> Activity.with_preloaded_bookmark(opts["user"])
1072 defp maybe_preload_report_notes(query, %{"preload_report_notes" => true}) do
1074 |> Activity.with_preloaded_report_notes()
1077 defp maybe_preload_report_notes(query, _), do: query
1079 defp maybe_set_thread_muted_field(query, %{"skip_preload" => true}), do: query
1081 defp maybe_set_thread_muted_field(query, opts) do
1083 |> Activity.with_set_thread_muted_field(opts["muting_user"] || opts["user"])
1086 defp maybe_order(query, %{order: :desc}) do
1088 |> order_by(desc: :id)
1091 defp maybe_order(query, %{order: :asc}) do
1093 |> order_by(asc: :id)
1096 defp maybe_order(query, _), do: query
1098 defp fetch_activities_query_ap_ids_ops(opts) do
1099 source_user = opts["muting_user"]
1100 ap_id_relationships = if source_user, do: [:mute, :reblog_mute], else: []
1102 ap_id_relationships =
1103 ap_id_relationships ++
1104 if opts["blocking_user"] && opts["blocking_user"] == source_user do
1110 preloaded_ap_ids = User.outgoing_relationships_ap_ids(source_user, ap_id_relationships)
1112 restrict_blocked_opts = Map.merge(%{"blocked_users_ap_ids" => preloaded_ap_ids[:block]}, opts)
1113 restrict_muted_opts = Map.merge(%{"muted_users_ap_ids" => preloaded_ap_ids[:mute]}, opts)
1115 restrict_muted_reblogs_opts =
1116 Map.merge(%{"reblog_muted_users_ap_ids" => preloaded_ap_ids[:reblog_mute]}, opts)
1118 {restrict_blocked_opts, restrict_muted_opts, restrict_muted_reblogs_opts}
1121 def fetch_activities_query(recipients, opts \\ %{}) do
1122 {restrict_blocked_opts, restrict_muted_opts, restrict_muted_reblogs_opts} =
1123 fetch_activities_query_ap_ids_ops(opts)
1126 skip_thread_containment: Config.get([:instance, :skip_thread_containment])
1130 |> maybe_preload_objects(opts)
1131 |> maybe_preload_bookmarks(opts)
1132 |> maybe_preload_report_notes(opts)
1133 |> maybe_set_thread_muted_field(opts)
1134 |> maybe_order(opts)
1135 |> restrict_recipients(recipients, opts["user"])
1136 |> restrict_replies(opts)
1137 |> restrict_tag(opts)
1138 |> restrict_tag_reject(opts)
1139 |> restrict_tag_all(opts)
1140 |> restrict_since(opts)
1141 |> restrict_local(opts)
1142 |> restrict_actor(opts)
1143 |> restrict_type(opts)
1144 |> restrict_state(opts)
1145 |> restrict_favorited_by(opts)
1146 |> restrict_blocked(restrict_blocked_opts)
1147 |> restrict_muted(restrict_muted_opts)
1148 |> restrict_media(opts)
1149 |> restrict_visibility(opts)
1150 |> restrict_thread_visibility(opts, config)
1151 |> restrict_reblogs(opts)
1152 |> restrict_pinned(opts)
1153 |> restrict_muted_reblogs(restrict_muted_reblogs_opts)
1154 |> restrict_instance(opts)
1155 |> Activity.restrict_deactivated_users()
1156 |> exclude_poll_votes(opts)
1157 |> exclude_visibility(opts)
1160 def fetch_activities(recipients, opts \\ %{}, pagination \\ :keyset) do
1161 list_memberships = Pleroma.List.memberships(opts["user"])
1163 fetch_activities_query(recipients ++ list_memberships, opts)
1164 |> Pagination.fetch_paginated(opts, pagination)
1166 |> maybe_update_cc(list_memberships, opts["user"])
1170 Fetch favorites activities of user with order by sort adds to favorites
1172 @spec fetch_favourites(User.t(), map(), Pagination.type()) :: list(Activity.t())
1173 def fetch_favourites(user, params \\ %{}, pagination \\ :keyset) do
1175 |> Activity.Queries.by_actor()
1176 |> Activity.Queries.by_type("Like")
1177 |> Activity.with_joined_object()
1178 |> Object.with_joined_activity()
1179 |> select([_like, object, activity], %{activity | object: object})
1180 |> order_by([like, _, _], desc: like.id)
1181 |> Pagination.fetch_paginated(
1182 Map.merge(params, %{"skip_order" => true}),
1188 defp maybe_update_cc(activities, list_memberships, %User{ap_id: user_ap_id})
1189 when is_list(list_memberships) and length(list_memberships) > 0 do
1190 Enum.map(activities, fn
1191 %{data: %{"bcc" => bcc}} = activity when is_list(bcc) and length(bcc) > 0 ->
1192 if Enum.any?(bcc, &(&1 in list_memberships)) do
1193 update_in(activity.data["cc"], &[user_ap_id | &1])
1203 defp maybe_update_cc(activities, _, _), do: activities
1205 def fetch_activities_bounded_query(query, recipients, recipients_with_public) do
1206 from(activity in query,
1208 fragment("? && ?", activity.recipients, ^recipients) or
1209 (fragment("? && ?", activity.recipients, ^recipients_with_public) and
1210 ^Constants.as_public() in activity.recipients)
1214 def fetch_activities_bounded(
1216 recipients_with_public,
1218 pagination \\ :keyset
1220 fetch_activities_query([], opts)
1221 |> fetch_activities_bounded_query(recipients, recipients_with_public)
1222 |> Pagination.fetch_paginated(opts, pagination)
1226 @spec upload(Upload.source(), keyword()) :: {:ok, Object.t()} | {:error, any()}
1227 def upload(file, opts \\ []) do
1228 with {:ok, data} <- Upload.store(file, opts) do
1231 Map.put(data, "actor", opts[:actor])
1236 Repo.insert(%Object{data: obj_data})
1240 @spec get_actor_url(any()) :: binary() | nil
1241 defp get_actor_url(url) when is_binary(url), do: url
1242 defp get_actor_url(%{"href" => href}) when is_binary(href), do: href
1244 defp get_actor_url(url) when is_list(url) do
1250 defp get_actor_url(_url), do: nil
1252 defp object_to_user_data(data) do
1254 data["icon"]["url"] &&
1257 "url" => [%{"href" => data["icon"]["url"]}]
1261 data["image"]["url"] &&
1264 "url" => [%{"href" => data["image"]["url"]}]
1269 |> Map.get("attachment", [])
1270 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
1271 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
1275 |> Map.get("tag", [])
1277 %{"type" => "Emoji"} -> true
1280 |> Enum.reduce(%{}, fn %{"icon" => %{"url" => url}, "name" => name}, acc ->
1281 Map.put(acc, String.trim(name, ":"), url)
1284 locked = data["manuallyApprovesFollowers"] || false
1285 data = Transmogrifier.maybe_fix_user_object(data)
1286 discoverable = data["discoverable"] || false
1287 invisible = data["invisible"] || false
1288 actor_type = data["type"] || "Person"
1291 if is_map(data["publicKey"]) && is_binary(data["publicKey"]["publicKeyPem"]) do
1292 data["publicKey"]["publicKeyPem"]
1298 if is_map(data["endpoints"]) && is_binary(data["endpoints"]["sharedInbox"]) do
1299 data["endpoints"]["sharedInbox"]
1306 uri: get_actor_url(data["url"]),
1312 discoverable: discoverable,
1313 invisible: invisible,
1316 follower_address: data["followers"],
1317 following_address: data["following"],
1318 bio: data["summary"],
1319 actor_type: actor_type,
1320 also_known_as: Map.get(data, "alsoKnownAs", []),
1321 public_key: public_key,
1322 inbox: data["inbox"],
1323 shared_inbox: shared_inbox
1326 # nickname can be nil because of virtual actors
1328 if data["preferredUsername"] do
1332 "#{data["preferredUsername"]}@#{URI.parse(data["id"]).host}"
1335 Map.put(user_data, :nickname, nil)
1341 def fetch_follow_information_for_user(user) do
1342 with {:ok, following_data} <-
1343 Fetcher.fetch_and_contain_remote_object_from_id(user.following_address),
1344 {:ok, hide_follows} <- collection_private(following_data),
1345 {:ok, followers_data} <-
1346 Fetcher.fetch_and_contain_remote_object_from_id(user.follower_address),
1347 {:ok, hide_followers} <- collection_private(followers_data) do
1350 hide_follows: hide_follows,
1351 follower_count: normalize_counter(followers_data["totalItems"]),
1352 following_count: normalize_counter(following_data["totalItems"]),
1353 hide_followers: hide_followers
1356 {:error, _} = e -> e
1361 defp normalize_counter(counter) when is_integer(counter), do: counter
1362 defp normalize_counter(_), do: 0
1364 def maybe_update_follow_information(user_data) do
1365 with {:enabled, true} <- {:enabled, Config.get([:instance, :external_user_synchronization])},
1366 {_, true} <- {:user_type_check, user_data[:type] in ["Person", "Service"]},
1368 {:collections_available,
1369 !!(user_data[:following_address] && user_data[:follower_address])},
1371 fetch_follow_information_for_user(user_data) do
1372 info = Map.merge(user_data[:info] || %{}, info)
1375 |> Map.put(:info, info)
1377 {:user_type_check, false} ->
1380 {:collections_available, false} ->
1383 {:enabled, false} ->
1388 "Follower/Following counter update for #{user_data.ap_id} failed.\n" <> inspect(e)
1395 defp collection_private(%{"first" => %{"type" => type}})
1396 when type in ["CollectionPage", "OrderedCollectionPage"],
1399 defp collection_private(%{"first" => first}) do
1400 with {:ok, %{"type" => type}} when type in ["CollectionPage", "OrderedCollectionPage"] <-
1401 Fetcher.fetch_and_contain_remote_object_from_id(first) do
1404 {:error, {:ok, %{status: code}}} when code in [401, 403] -> {:ok, true}
1405 {:error, _} = e -> e
1410 defp collection_private(_data), do: {:ok, true}
1412 def user_data_from_user_object(data) do
1413 with {:ok, data} <- MRF.filter(data),
1414 {:ok, data} <- object_to_user_data(data) do
1421 def fetch_and_prepare_user_from_ap_id(ap_id) do
1422 with {:ok, data} <- Fetcher.fetch_and_contain_remote_object_from_id(ap_id),
1423 {:ok, data} <- user_data_from_user_object(data),
1424 data <- maybe_update_follow_information(data) do
1427 {:error, "Object has been deleted"} = e ->
1428 Logger.debug("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
1432 Logger.error("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
1437 def make_user_from_ap_id(ap_id) do
1438 user = User.get_cached_by_ap_id(ap_id)
1440 if user && !User.ap_enabled?(user) do
1441 Transmogrifier.upgrade_user_from_ap_id(ap_id)
1443 with {:ok, data} <- fetch_and_prepare_user_from_ap_id(ap_id) do
1446 |> User.remote_user_changeset(data)
1447 |> User.update_and_set_cache()
1450 |> User.remote_user_changeset()
1460 def make_user_from_nickname(nickname) do
1461 with {:ok, %{"ap_id" => ap_id}} when not is_nil(ap_id) <- WebFinger.finger(nickname) do
1462 make_user_from_ap_id(ap_id)
1464 _e -> {:error, "No AP id in WebFinger"}
1468 # filter out broken threads
1469 def contain_broken_threads(%Activity{} = activity, %User{} = user) do
1470 entire_thread_visible_for_user?(activity, user)
1473 # do post-processing on a specific activity
1474 def contain_activity(%Activity{} = activity, %User{} = user) do
1475 contain_broken_threads(activity, user)
1478 def fetch_direct_messages_query do
1480 |> restrict_type(%{"type" => "Create"})
1481 |> restrict_visibility(%{visibility: "direct"})
1482 |> order_by([activity], asc: activity.id)