1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ActivityPub do
8 alias Pleroma.Conversation
9 alias Pleroma.Notification
11 alias Pleroma.Object.Containment
12 alias Pleroma.Object.Fetcher
13 alias Pleroma.Pagination
17 alias Pleroma.Web.ActivityPub.MRF
18 alias Pleroma.Web.ActivityPub.Transmogrifier
19 alias Pleroma.Web.WebFinger
22 import Pleroma.Web.ActivityPub.Utils
23 import Pleroma.Web.ActivityPub.Visibility
26 require Pleroma.Constants
28 # For Announce activities, we filter the recipients based on following status for any actors
29 # that match actual users. See issue #164 for more information about why this is necessary.
30 defp get_recipients(%{"type" => "Announce"} = data) do
31 to = Map.get(data, "to", [])
32 cc = Map.get(data, "cc", [])
33 bcc = Map.get(data, "bcc", [])
34 actor = User.get_cached_by_ap_id(data["actor"])
37 Enum.filter(Enum.concat([to, cc, bcc]), fn recipient ->
38 case User.get_cached_by_ap_id(recipient) do
40 user -> User.following?(user, actor)
47 defp get_recipients(%{"type" => "Create"} = data) do
48 to = Map.get(data, "to", [])
49 cc = Map.get(data, "cc", [])
50 bcc = Map.get(data, "bcc", [])
51 actor = Map.get(data, "actor", [])
52 recipients = [to, cc, bcc, [actor]] |> Enum.concat() |> Enum.uniq()
56 defp get_recipients(data) do
57 to = Map.get(data, "to", [])
58 cc = Map.get(data, "cc", [])
59 bcc = Map.get(data, "bcc", [])
60 recipients = Enum.concat([to, cc, bcc])
64 defp check_actor_is_active(actor) do
65 if not is_nil(actor) do
66 with user <- User.get_cached_by_ap_id(actor),
67 false <- user.info.deactivated do
77 defp check_remote_limit(%{"object" => %{"content" => content}}) when not is_nil(content) do
78 limit = Config.get([:instance, :remote_limit])
79 String.length(content) <= limit
82 defp check_remote_limit(_), do: true
84 def increase_note_count_if_public(actor, object) do
85 if is_public?(object), do: User.increase_note_count(actor), else: {:ok, actor}
88 def decrease_note_count_if_public(actor, object) do
89 if is_public?(object), do: User.decrease_note_count(actor), else: {:ok, actor}
92 def increase_replies_count_if_reply(%{
93 "object" => %{"inReplyTo" => reply_ap_id} = object,
96 if is_public?(object) do
97 Object.increase_replies_count(reply_ap_id)
101 def increase_replies_count_if_reply(_create_data), do: :noop
103 def decrease_replies_count_if_reply(%Object{
104 data: %{"inReplyTo" => reply_ap_id} = object
106 if is_public?(object) do
107 Object.decrease_replies_count(reply_ap_id)
111 def decrease_replies_count_if_reply(_object), do: :noop
113 def increase_poll_votes_if_vote(%{
114 "object" => %{"inReplyTo" => reply_ap_id, "name" => name},
117 Object.increase_vote_count(reply_ap_id, name)
120 def increase_poll_votes_if_vote(_create_data), do: :noop
122 def insert(map, local \\ true, fake \\ false) when is_map(map) do
123 with nil <- Activity.normalize(map),
124 map <- lazy_put_activity_defaults(map, fake),
125 :ok <- check_actor_is_active(map["actor"]),
126 {_, true} <- {:remote_limit_error, check_remote_limit(map)},
127 {:ok, map} <- MRF.filter(map),
128 {recipients, _, _} = get_recipients(map),
129 {:fake, false, map, recipients} <- {:fake, fake, map, recipients},
130 :ok <- Containment.contain_child(map),
131 {:ok, map, object} <- insert_full_object(map) do
133 Repo.insert(%Activity{
137 recipients: recipients
140 # Splice in the child object if we have one.
142 if !is_nil(object) do
143 Map.put(activity, :object, object)
148 PleromaJobQueue.enqueue(:background, Pleroma.Web.RichMedia.Helpers, [:fetch, activity])
150 Notification.create_notifications(activity)
154 |> Conversation.create_or_bump_for()
155 |> get_participations()
158 stream_out_participations(participations)
161 %Activity{} = activity ->
164 {:fake, true, map, recipients} ->
165 activity = %Activity{
169 recipients: recipients,
173 Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
181 defp get_participations({:ok, %{participations: participations}}), do: participations
182 defp get_participations(_), do: []
184 def stream_out_participations(participations) do
187 |> Repo.preload(:user)
189 Enum.each(participations, fn participation ->
190 Pleroma.Web.Streamer.stream("participation", participation)
194 def stream_out_participations(%Object{data: %{"context" => context}}, user) do
195 with %Conversation{} = conversation <- Conversation.get_for_ap_id(context),
196 conversation = Repo.preload(conversation, :participations),
198 fetch_latest_activity_id_for_context(conversation.ap_id, %{
200 "blocking_user" => user
202 if last_activity_id do
203 stream_out_participations(conversation.participations)
208 def stream_out_participations(_, _), do: :noop
210 def stream_out(activity) do
211 if activity.data["type"] in ["Create", "Announce", "Delete"] do
212 object = Object.normalize(activity)
213 # Do not stream out poll replies
214 unless object.data["type"] == "Answer" do
215 Pleroma.Web.Streamer.stream("user", activity)
216 Pleroma.Web.Streamer.stream("list", activity)
218 if get_visibility(activity) == "public" do
219 Pleroma.Web.Streamer.stream("public", activity)
222 Pleroma.Web.Streamer.stream("public:local", activity)
225 if activity.data["type"] in ["Create"] do
227 |> Map.get("tag", [])
228 |> Enum.filter(fn tag -> is_bitstring(tag) end)
229 |> Enum.each(fn tag -> Pleroma.Web.Streamer.stream("hashtag:" <> tag, activity) end)
231 if object.data["attachment"] != [] do
232 Pleroma.Web.Streamer.stream("public:media", activity)
235 Pleroma.Web.Streamer.stream("public:local:media", activity)
240 if get_visibility(activity) == "direct",
241 do: Pleroma.Web.Streamer.stream("direct", activity)
247 def create(%{to: to, actor: actor, context: context, object: object} = params, fake \\ false) do
248 additional = params[:additional] || %{}
249 # only accept false as false value
250 local = !(params[:local] == false)
251 published = params[:published]
255 %{to: to, actor: actor, published: published, context: context, object: object},
258 {:ok, activity} <- insert(create_data, local, fake),
259 {:fake, false, activity} <- {:fake, fake, activity},
260 _ <- increase_replies_count_if_reply(create_data),
261 _ <- increase_poll_votes_if_vote(create_data),
262 # Changing note count prior to enqueuing federation task in order to avoid
263 # race conditions on updating user.info
264 {:ok, _actor} <- increase_note_count_if_public(actor, activity),
265 :ok <- maybe_federate(activity) do
268 {:fake, true, activity} ->
276 def accept(%{to: to, actor: actor, object: object} = params) do
277 # only accept false as false value
278 local = !(params[:local] == false)
280 with data <- %{"to" => to, "type" => "Accept", "actor" => actor.ap_id, "object" => object},
281 {:ok, activity} <- insert(data, local),
282 :ok <- maybe_federate(activity) do
287 def reject(%{to: to, actor: actor, object: object} = params) do
288 # only accept false as false value
289 local = !(params[:local] == false)
291 with data <- %{"to" => to, "type" => "Reject", "actor" => actor.ap_id, "object" => object},
292 {:ok, activity} <- insert(data, local),
293 :ok <- maybe_federate(activity) do
298 def update(%{to: to, cc: cc, actor: actor, object: object} = params) do
299 # only accept false as false value
300 local = !(params[:local] == false)
309 {:ok, activity} <- insert(data, local),
310 :ok <- maybe_federate(activity) do
315 # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
317 %User{ap_id: ap_id} = user,
318 %Object{data: %{"id" => _}} = object,
322 with nil <- get_existing_like(ap_id, object),
323 like_data <- make_like_data(user, object, activity_id),
324 {:ok, activity} <- insert(like_data, local),
325 {:ok, object} <- add_like_to_object(activity, object),
326 :ok <- maybe_federate(activity) do
327 {:ok, activity, object}
329 %Activity{} = activity -> {:ok, activity, object}
330 error -> {:error, error}
340 with %Activity{} = like_activity <- get_existing_like(actor.ap_id, object),
341 unlike_data <- make_unlike_data(actor, like_activity, activity_id),
342 {:ok, unlike_activity} <- insert(unlike_data, local),
343 {:ok, _activity} <- Repo.delete(like_activity),
344 {:ok, object} <- remove_like_from_object(like_activity, object),
345 :ok <- maybe_federate(unlike_activity) do
346 {:ok, unlike_activity, like_activity, object}
353 %User{ap_id: _} = user,
354 %Object{data: %{"id" => _}} = object,
359 with true <- is_public?(object),
360 announce_data <- make_announce_data(user, object, activity_id, public),
361 {:ok, activity} <- insert(announce_data, local),
362 {:ok, object} <- add_announce_to_object(activity, object),
363 :ok <- maybe_federate(activity) do
364 {:ok, activity, object}
366 error -> {:error, error}
376 with %Activity{} = announce_activity <- get_existing_announce(actor.ap_id, object),
377 unannounce_data <- make_unannounce_data(actor, announce_activity, activity_id),
378 {:ok, unannounce_activity} <- insert(unannounce_data, local),
379 :ok <- maybe_federate(unannounce_activity),
380 {:ok, _activity} <- Repo.delete(announce_activity),
381 {:ok, object} <- remove_announce_from_object(announce_activity, object) do
382 {:ok, unannounce_activity, object}
388 def follow(follower, followed, activity_id \\ nil, local \\ true) do
389 with data <- make_follow_data(follower, followed, activity_id),
390 {:ok, activity} <- insert(data, local),
391 :ok <- maybe_federate(activity) do
396 def unfollow(follower, followed, activity_id \\ nil, local \\ true) do
397 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
398 {:ok, follow_activity} <- update_follow_state(follow_activity, "cancelled"),
399 unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
400 {:ok, activity} <- insert(unfollow_data, local),
401 :ok <- maybe_federate(activity) do
406 def delete(%User{ap_id: ap_id, follower_address: follower_address} = user) do
408 "to" => [follower_address],
411 "object" => %{"type" => "Person", "id" => ap_id}
413 {:ok, activity} <- insert(data, true, true),
414 :ok <- maybe_federate(activity) do
419 def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
420 user = User.get_cached_by_ap_id(actor)
421 to = (object.data["to"] || []) ++ (object.data["cc"] || [])
423 with {:ok, object, activity} <- Object.delete(object),
429 "deleted_activity_id" => activity && activity.id
431 {:ok, activity} <- insert(data, local, false),
432 stream_out_participations(object, user),
433 _ <- decrease_replies_count_if_reply(object),
434 # Changing note count prior to enqueuing federation task in order to avoid
435 # race conditions on updating user.info
436 {:ok, _actor} <- decrease_note_count_if_public(user, object),
437 :ok <- maybe_federate(activity) do
442 def block(blocker, blocked, activity_id \\ nil, local \\ true) do
443 outgoing_blocks = Config.get([:activitypub, :outgoing_blocks])
444 unfollow_blocked = Config.get([:activitypub, :unfollow_blocked])
446 if unfollow_blocked do
447 follow_activity = fetch_latest_follow(blocker, blocked)
448 if follow_activity, do: unfollow(blocker, blocked, nil, local)
451 with true <- outgoing_blocks,
452 block_data <- make_block_data(blocker, blocked, activity_id),
453 {:ok, activity} <- insert(block_data, local),
454 :ok <- maybe_federate(activity) do
461 def unblock(blocker, blocked, activity_id \\ nil, local \\ true) do
462 with %Activity{} = block_activity <- fetch_latest_block(blocker, blocked),
463 unblock_data <- make_unblock_data(blocker, blocked, block_activity, activity_id),
464 {:ok, activity} <- insert(unblock_data, local),
465 :ok <- maybe_federate(activity) do
479 # only accept false as false value
480 local = !(params[:local] == false)
481 forward = !(params[:forward] == false)
483 additional = params[:additional] || %{}
495 Map.merge(additional, %{"to" => [], "cc" => [account.ap_id]})
497 Map.merge(additional, %{"to" => [], "cc" => []})
500 with flag_data <- make_flag_data(params, additional),
501 {:ok, activity} <- insert(flag_data, local),
502 :ok <- maybe_federate(activity) do
503 Enum.each(User.all_superusers(), fn superuser ->
505 |> Pleroma.Emails.AdminEmail.report(actor, account, statuses, content)
506 |> Pleroma.Emails.Mailer.deliver_async()
513 defp fetch_activities_for_context_query(context, opts) do
514 public = [Pleroma.Constants.as_public()]
517 if opts["user"], do: [opts["user"].ap_id | opts["user"].following] ++ public, else: public
519 from(activity in Activity)
520 |> maybe_preload_objects(opts)
521 |> restrict_blocked(opts)
522 |> restrict_recipients(recipients, opts["user"])
526 "?->>'type' = ? and ?->>'context' = ?",
533 |> exclude_poll_votes(opts)
534 |> order_by([activity], desc: activity.id)
537 @spec fetch_activities_for_context(String.t(), keyword() | map()) :: [Activity.t()]
538 def fetch_activities_for_context(context, opts \\ %{}) do
540 |> fetch_activities_for_context_query(opts)
544 @spec fetch_latest_activity_id_for_context(String.t(), keyword() | map()) ::
545 Pleroma.FlakeId.t() | nil
546 def fetch_latest_activity_id_for_context(context, opts \\ %{}) do
548 |> fetch_activities_for_context_query(Map.merge(%{"skip_preload" => true}, opts))
554 def fetch_public_activities(opts \\ %{}) do
555 q = fetch_activities_query([Pleroma.Constants.as_public()], opts)
558 |> restrict_unlisted()
559 |> Pagination.fetch_paginated(opts)
563 @valid_visibilities ~w[direct unlisted public private]
565 defp restrict_visibility(query, %{visibility: visibility})
566 when is_list(visibility) do
567 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
573 "activity_visibility(?, ?, ?) = ANY (?)",
583 Logger.error("Could not restrict visibility to #{visibility}")
587 defp restrict_visibility(query, %{visibility: visibility})
588 when visibility in @valid_visibilities do
592 fragment("activity_visibility(?, ?, ?) = ?", a.actor, a.recipients, a.data, ^visibility)
596 defp restrict_visibility(_query, %{visibility: visibility})
597 when visibility not in @valid_visibilities do
598 Logger.error("Could not restrict visibility to #{visibility}")
601 defp restrict_visibility(query, _visibility), do: query
603 defp restrict_thread_visibility(query, _, %{skip_thread_containment: true} = _),
606 defp restrict_thread_visibility(
608 %{"user" => %User{info: %{skip_thread_containment: true}}},
613 defp restrict_thread_visibility(query, %{"user" => %User{ap_id: ap_id}}, _) do
616 where: fragment("thread_visibility(?, (?)->>'id') = true", ^ap_id, a.data)
620 defp restrict_thread_visibility(query, _, _), do: query
622 def fetch_user_activities(user, reading_user, params \\ %{}) do
625 |> Map.put("type", ["Create", "Announce"])
626 |> Map.put("actor_id", user.ap_id)
627 |> Map.put("whole_db", true)
628 |> Map.put("pinned_activity_ids", user.info.pinned_activities)
631 user_activities_recipients(%{
632 "godmode" => params["godmode"],
633 "reading_user" => reading_user
636 fetch_activities(recipients, params)
640 defp user_activities_recipients(%{"godmode" => true}) do
644 defp user_activities_recipients(%{"reading_user" => reading_user}) do
646 [Pleroma.Constants.as_public()] ++ [reading_user.ap_id | reading_user.following]
648 [Pleroma.Constants.as_public()]
652 defp restrict_since(query, %{"since_id" => ""}), do: query
654 defp restrict_since(query, %{"since_id" => since_id}) do
655 from(activity in query, where: activity.id > ^since_id)
658 defp restrict_since(query, _), do: query
660 defp restrict_tag_reject(_query, %{"tag_reject" => _tag_reject, "skip_preload" => true}) do
661 raise "Can't use the child object without preloading!"
664 defp restrict_tag_reject(query, %{"tag_reject" => tag_reject})
665 when is_list(tag_reject) and tag_reject != [] do
667 [_activity, object] in query,
668 where: fragment("not (?)->'tag' \\?| (?)", object.data, ^tag_reject)
672 defp restrict_tag_reject(query, _), do: query
674 defp restrict_tag_all(_query, %{"tag_all" => _tag_all, "skip_preload" => true}) do
675 raise "Can't use the child object without preloading!"
678 defp restrict_tag_all(query, %{"tag_all" => tag_all})
679 when is_list(tag_all) and tag_all != [] do
681 [_activity, object] in query,
682 where: fragment("(?)->'tag' \\?& (?)", object.data, ^tag_all)
686 defp restrict_tag_all(query, _), do: query
688 defp restrict_tag(_query, %{"tag" => _tag, "skip_preload" => true}) do
689 raise "Can't use the child object without preloading!"
692 defp restrict_tag(query, %{"tag" => tag}) when is_list(tag) do
694 [_activity, object] in query,
695 where: fragment("(?)->'tag' \\?| (?)", object.data, ^tag)
699 defp restrict_tag(query, %{"tag" => tag}) when is_binary(tag) do
701 [_activity, object] in query,
702 where: fragment("(?)->'tag' \\? (?)", object.data, ^tag)
706 defp restrict_tag(query, _), do: query
708 defp restrict_recipients(query, [], _user), do: query
710 defp restrict_recipients(query, recipients, nil) do
711 from(activity in query, where: fragment("? && ?", ^recipients, activity.recipients))
714 defp restrict_recipients(query, recipients, user) do
717 where: fragment("? && ?", ^recipients, activity.recipients),
718 or_where: activity.actor == ^user.ap_id
722 defp restrict_local(query, %{"local_only" => true}) do
723 from(activity in query, where: activity.local == true)
726 defp restrict_local(query, _), do: query
728 defp restrict_actor(query, %{"actor_id" => actor_id}) do
729 from(activity in query, where: activity.actor == ^actor_id)
732 defp restrict_actor(query, _), do: query
734 defp restrict_type(query, %{"type" => type}) when is_binary(type) do
735 from(activity in query, where: fragment("?->>'type' = ?", activity.data, ^type))
738 defp restrict_type(query, %{"type" => type}) do
739 from(activity in query, where: fragment("?->>'type' = ANY(?)", activity.data, ^type))
742 defp restrict_type(query, _), do: query
744 defp restrict_state(query, %{"state" => state}) do
745 from(activity in query, where: fragment("?->>'state' = ?", activity.data, ^state))
748 defp restrict_state(query, _), do: query
750 defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
752 [_activity, object] in query,
753 where: fragment("(?)->'likes' \\? (?)", object.data, ^ap_id)
757 defp restrict_favorited_by(query, _), do: query
759 defp restrict_media(_query, %{"only_media" => _val, "skip_preload" => true}) do
760 raise "Can't use the child object without preloading!"
763 defp restrict_media(query, %{"only_media" => val}) when val == "true" or val == "1" do
765 [_activity, object] in query,
766 where: fragment("not (?)->'attachment' = (?)", object.data, ^[])
770 defp restrict_media(query, _), do: query
772 defp restrict_replies(query, %{"exclude_replies" => val}) when val == "true" or val == "1" do
775 where: fragment("?->'object'->>'inReplyTo' is null", activity.data)
779 defp restrict_replies(query, _), do: query
781 defp restrict_reblogs(query, %{"exclude_reblogs" => val}) when val == "true" or val == "1" do
782 from(activity in query, where: fragment("?->>'type' != 'Announce'", activity.data))
785 defp restrict_reblogs(query, _), do: query
787 defp restrict_muted(query, %{"with_muted" => val}) when val in [true, "true", "1"], do: query
789 defp restrict_muted(query, %{"muting_user" => %User{info: info}}) do
794 where: fragment("not (? = ANY(?))", activity.actor, ^mutes),
795 where: fragment("not (?->'to' \\?| ?)", activity.data, ^mutes)
799 defp restrict_muted(query, _), do: query
801 defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
802 blocks = info.blocks || []
803 domain_blocks = info.domain_blocks || []
806 if has_named_binding?(query, :object), do: query, else: Activity.with_joined_object(query)
809 [activity, object: o] in query,
810 where: fragment("not (? = ANY(?))", activity.actor, ^blocks),
811 where: fragment("not (? && ?)", activity.recipients, ^blocks),
814 "not (?->>'type' = 'Announce' and ?->'to' \\?| ?)",
819 where: fragment("not (split_part(?, '/', 3) = ANY(?))", activity.actor, ^domain_blocks),
820 where: fragment("not (split_part(?->>'actor', '/', 3) = ANY(?))", o.data, ^domain_blocks)
824 defp restrict_blocked(query, _), do: query
826 defp restrict_unlisted(query) do
831 "not (coalesce(?->'cc', '{}'::jsonb) \\?| ?)",
833 ^[Pleroma.Constants.as_public()]
838 defp restrict_pinned(query, %{"pinned" => "true", "pinned_activity_ids" => ids}) do
839 from(activity in query, where: activity.id in ^ids)
842 defp restrict_pinned(query, _), do: query
844 defp restrict_muted_reblogs(query, %{"muting_user" => %User{info: info}}) do
845 muted_reblogs = info.muted_reblogs || []
851 "not ( ?->>'type' = 'Announce' and ? = ANY(?))",
859 defp restrict_muted_reblogs(query, _), do: query
861 defp exclude_poll_votes(query, %{"include_poll_votes" => "true"}), do: query
863 defp exclude_poll_votes(query, _) do
864 if has_named_binding?(query, :object) do
865 from([activity, object: o] in query,
866 where: fragment("not(?->>'type' = ?)", o.data, "Answer")
873 defp maybe_preload_objects(query, %{"skip_preload" => true}), do: query
875 defp maybe_preload_objects(query, _) do
877 |> Activity.with_preloaded_object()
880 defp maybe_preload_bookmarks(query, %{"skip_preload" => true}), do: query
882 defp maybe_preload_bookmarks(query, opts) do
884 |> Activity.with_preloaded_bookmark(opts["user"])
887 defp maybe_set_thread_muted_field(query, %{"skip_preload" => true}), do: query
889 defp maybe_set_thread_muted_field(query, opts) do
891 |> Activity.with_set_thread_muted_field(opts["user"])
894 defp maybe_order(query, %{order: :desc}) do
896 |> order_by(desc: :id)
899 defp maybe_order(query, %{order: :asc}) do
901 |> order_by(asc: :id)
904 defp maybe_order(query, _), do: query
906 def fetch_activities_query(recipients, opts \\ %{}) do
908 skip_thread_containment: Config.get([:instance, :skip_thread_containment])
912 |> maybe_preload_objects(opts)
913 |> maybe_preload_bookmarks(opts)
914 |> maybe_set_thread_muted_field(opts)
916 |> restrict_recipients(recipients, opts["user"])
917 |> restrict_tag(opts)
918 |> restrict_tag_reject(opts)
919 |> restrict_tag_all(opts)
920 |> restrict_since(opts)
921 |> restrict_local(opts)
922 |> restrict_actor(opts)
923 |> restrict_type(opts)
924 |> restrict_state(opts)
925 |> restrict_favorited_by(opts)
926 |> restrict_blocked(opts)
927 |> restrict_muted(opts)
928 |> restrict_media(opts)
929 |> restrict_visibility(opts)
930 |> restrict_thread_visibility(opts, config)
931 |> restrict_replies(opts)
932 |> restrict_reblogs(opts)
933 |> restrict_pinned(opts)
934 |> restrict_muted_reblogs(opts)
935 |> Activity.restrict_deactivated_users()
936 |> exclude_poll_votes(opts)
939 def fetch_activities(recipients, opts \\ %{}) do
940 list_memberships = Pleroma.List.memberships(opts["user"])
942 fetch_activities_query(recipients ++ list_memberships, opts)
943 |> Pagination.fetch_paginated(opts)
945 |> maybe_update_cc(list_memberships, opts["user"])
948 defp maybe_update_cc(activities, list_memberships, %User{ap_id: user_ap_id})
949 when is_list(list_memberships) and length(list_memberships) > 0 do
950 Enum.map(activities, fn
951 %{data: %{"bcc" => bcc}} = activity when is_list(bcc) and length(bcc) > 0 ->
952 if Enum.any?(bcc, &(&1 in list_memberships)) do
953 update_in(activity.data["cc"], &[user_ap_id | &1])
963 defp maybe_update_cc(activities, _, _), do: activities
965 def fetch_activities_bounded_query(query, recipients, recipients_with_public) do
966 from(activity in query,
968 fragment("? && ?", activity.recipients, ^recipients) or
969 (fragment("? && ?", activity.recipients, ^recipients_with_public) and
970 ^Pleroma.Constants.as_public() in activity.recipients)
974 def fetch_activities_bounded(recipients, recipients_with_public, opts \\ %{}) do
975 fetch_activities_query([], opts)
976 |> fetch_activities_bounded_query(recipients, recipients_with_public)
977 |> Pagination.fetch_paginated(opts)
981 def upload(file, opts \\ []) do
982 with {:ok, data} <- Upload.store(file, opts) do
985 Map.put(data, "actor", opts[:actor])
990 Repo.insert(%Object{data: obj_data})
994 defp object_to_user_data(data) do
996 data["icon"]["url"] &&
999 "url" => [%{"href" => data["icon"]["url"]}]
1003 data["image"]["url"] &&
1006 "url" => [%{"href" => data["image"]["url"]}]
1009 locked = data["manuallyApprovesFollowers"] || false
1010 data = Transmogrifier.maybe_fix_user_object(data)
1022 follower_address: data["followers"],
1023 following_address: data["following"],
1024 bio: data["summary"]
1027 # nickname can be nil because of virtual actors
1029 if data["preferredUsername"] do
1033 "#{data["preferredUsername"]}@#{URI.parse(data["id"]).host}"
1036 Map.put(user_data, :nickname, nil)
1042 def fetch_follow_information_for_user(user) do
1043 with {:ok, following_data} <-
1044 Fetcher.fetch_and_contain_remote_object_from_id(user.following_address),
1045 following_count when is_integer(following_count) <- following_data["totalItems"],
1046 {:ok, hide_follows} <- collection_private(following_data),
1047 {:ok, followers_data} <-
1048 Fetcher.fetch_and_contain_remote_object_from_id(user.follower_address),
1049 followers_count when is_integer(followers_count) <- followers_data["totalItems"],
1050 {:ok, hide_followers} <- collection_private(followers_data) do
1053 hide_follows: hide_follows,
1054 follower_count: followers_count,
1055 following_count: following_count,
1056 hide_followers: hide_followers
1067 defp maybe_update_follow_information(data) do
1068 with {:enabled, true} <-
1069 {:enabled, Pleroma.Config.get([:instance, :external_user_synchronization])},
1070 {:ok, info} <- fetch_follow_information_for_user(data) do
1071 info = Map.merge(data.info, info)
1072 Map.put(data, :info, info)
1074 {:enabled, false} ->
1079 "Follower/Following counter update for #{data.ap_id} failed.\n" <> inspect(e)
1086 defp collection_private(data) do
1087 if is_map(data["first"]) and
1088 data["first"]["type"] in ["CollectionPage", "OrderedCollectionPage"] do
1091 with {:ok, %{"type" => type}} when type in ["CollectionPage", "OrderedCollectionPage"] <-
1092 Fetcher.fetch_and_contain_remote_object_from_id(data["first"]) do
1095 {:error, {:ok, %{status: code}}} when code in [401, 403] ->
1107 def user_data_from_user_object(data) do
1108 with {:ok, data} <- MRF.filter(data),
1109 {:ok, data} <- object_to_user_data(data) do
1116 def fetch_and_prepare_user_from_ap_id(ap_id) do
1117 with {:ok, data} <- Fetcher.fetch_and_contain_remote_object_from_id(ap_id),
1118 {:ok, data} <- user_data_from_user_object(data),
1119 data <- maybe_update_follow_information(data) do
1122 e -> Logger.error("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
1126 def make_user_from_ap_id(ap_id) do
1127 if _user = User.get_cached_by_ap_id(ap_id) do
1128 Transmogrifier.upgrade_user_from_ap_id(ap_id)
1130 with {:ok, data} <- fetch_and_prepare_user_from_ap_id(ap_id) do
1131 User.insert_or_update_user(data)
1138 def make_user_from_nickname(nickname) do
1139 with {:ok, %{"ap_id" => ap_id}} when not is_nil(ap_id) <- WebFinger.finger(nickname) do
1140 make_user_from_ap_id(ap_id)
1142 _e -> {:error, "No AP id in WebFinger"}
1146 # filter out broken threads
1147 def contain_broken_threads(%Activity{} = activity, %User{} = user) do
1148 entire_thread_visible_for_user?(activity, user)
1151 # do post-processing on a specific activity
1152 def contain_activity(%Activity{} = activity, %User{} = user) do
1153 contain_broken_threads(activity, user)
1156 def fetch_direct_messages_query do
1158 |> restrict_type(%{"type" => "Create"})
1159 |> restrict_visibility(%{visibility: "direct"})
1160 |> order_by([activity], asc: activity.id)