1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.ActivityPub do
7 alias Pleroma.Activity.Ir.Topics
9 alias Pleroma.Conversation
10 alias Pleroma.Notification
12 alias Pleroma.Object.Containment
13 alias Pleroma.Object.Fetcher
14 alias Pleroma.Pagination
18 alias Pleroma.Web.ActivityPub.MRF
19 alias Pleroma.Web.ActivityPub.Transmogrifier
20 alias Pleroma.Web.Streamer
21 alias Pleroma.Web.WebFinger
22 alias Pleroma.Workers.BackgroundWorker
25 import Pleroma.Web.ActivityPub.Utils
26 import Pleroma.Web.ActivityPub.Visibility
29 require Pleroma.Constants
31 # For Announce activities, we filter the recipients based on following status for any actors
32 # that match actual users. See issue #164 for more information about why this is necessary.
33 defp get_recipients(%{"type" => "Announce"} = data) do
34 to = Map.get(data, "to", [])
35 cc = Map.get(data, "cc", [])
36 bcc = Map.get(data, "bcc", [])
37 actor = User.get_cached_by_ap_id(data["actor"])
40 Enum.filter(Enum.concat([to, cc, bcc]), fn recipient ->
41 case User.get_cached_by_ap_id(recipient) do
43 user -> User.following?(user, actor)
50 defp get_recipients(%{"type" => "Create"} = data) do
51 to = Map.get(data, "to", [])
52 cc = Map.get(data, "cc", [])
53 bcc = Map.get(data, "bcc", [])
54 actor = Map.get(data, "actor", [])
55 recipients = [to, cc, bcc, [actor]] |> Enum.concat() |> Enum.uniq()
59 defp get_recipients(data) do
60 to = Map.get(data, "to", [])
61 cc = Map.get(data, "cc", [])
62 bcc = Map.get(data, "bcc", [])
63 recipients = Enum.concat([to, cc, bcc])
67 defp check_actor_is_active(actor) do
68 if not is_nil(actor) do
69 with user <- User.get_cached_by_ap_id(actor),
70 false <- user.info.deactivated do
80 defp check_remote_limit(%{"object" => %{"content" => content}}) when not is_nil(content) do
81 limit = Config.get([:instance, :remote_limit])
82 String.length(content) <= limit
85 defp check_remote_limit(_), do: true
87 def increase_note_count_if_public(actor, object) do
88 if is_public?(object), do: User.increase_note_count(actor), else: {:ok, actor}
91 def decrease_note_count_if_public(actor, object) do
92 if is_public?(object), do: User.decrease_note_count(actor), else: {:ok, actor}
95 def increase_replies_count_if_reply(%{
96 "object" => %{"inReplyTo" => reply_ap_id} = object,
99 if is_public?(object) do
100 Object.increase_replies_count(reply_ap_id)
104 def increase_replies_count_if_reply(_create_data), do: :noop
106 def decrease_replies_count_if_reply(%Object{
107 data: %{"inReplyTo" => reply_ap_id} = object
109 if is_public?(object) do
110 Object.decrease_replies_count(reply_ap_id)
114 def decrease_replies_count_if_reply(_object), do: :noop
116 def increase_poll_votes_if_vote(%{
117 "object" => %{"inReplyTo" => reply_ap_id, "name" => name},
120 Object.increase_vote_count(reply_ap_id, name)
123 def increase_poll_votes_if_vote(_create_data), do: :noop
125 def insert(map, local \\ true, fake \\ false, bypass_actor_check \\ false) when is_map(map) do
126 with nil <- Activity.normalize(map),
127 map <- lazy_put_activity_defaults(map, fake),
128 true <- bypass_actor_check || check_actor_is_active(map["actor"]),
129 {_, true} <- {:remote_limit_error, check_remote_limit(map)},
130 {:ok, map} <- MRF.filter(map),
131 {recipients, _, _} = get_recipients(map),
132 {:fake, false, map, recipients} <- {:fake, fake, map, recipients},
133 :ok <- Containment.contain_child(map),
134 {:ok, map, object} <- insert_full_object(map) do
136 Repo.insert(%Activity{
140 recipients: recipients
143 # Splice in the child object if we have one.
145 if not is_nil(object) do
146 Map.put(activity, :object, object)
151 BackgroundWorker.enqueue("fetch_data_for_activity", %{"activity_id" => activity.id})
153 Notification.create_notifications(activity)
157 |> Conversation.create_or_bump_for()
158 |> get_participations()
161 stream_out_participations(participations)
164 %Activity{} = activity ->
167 {:fake, true, map, recipients} ->
168 activity = %Activity{
172 recipients: recipients,
176 Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
184 defp get_participations({:ok, %{participations: participations}}), do: participations
185 defp get_participations(_), do: []
187 def stream_out_participations(participations) do
190 |> Repo.preload(:user)
192 Streamer.stream("participation", participations)
195 def stream_out_participations(%Object{data: %{"context" => context}}, user) do
196 with %Conversation{} = conversation <- Conversation.get_for_ap_id(context),
197 conversation = Repo.preload(conversation, :participations),
199 fetch_latest_activity_id_for_context(conversation.ap_id, %{
201 "blocking_user" => user
203 if last_activity_id do
204 stream_out_participations(conversation.participations)
209 def stream_out_participations(_, _), do: :noop
211 def stream_out(%Activity{data: %{"type" => data_type}} = activity)
212 when data_type in ["Create", "Announce", "Delete"] do
214 |> Topics.get_activity_topics()
215 |> Streamer.stream(activity)
218 def stream_out(_activity) do
222 def create(%{to: to, actor: actor, context: context, object: object} = params, fake \\ false) do
223 additional = params[:additional] || %{}
224 # only accept false as false value
225 local = !(params[:local] == false)
226 published = params[:published]
230 %{to: to, actor: actor, published: published, context: context, object: object},
233 {:ok, activity} <- insert(create_data, local, fake),
234 {:fake, false, activity} <- {:fake, fake, activity},
235 _ <- increase_replies_count_if_reply(create_data),
236 _ <- increase_poll_votes_if_vote(create_data),
237 # Changing note count prior to enqueuing federation task in order to avoid
238 # race conditions on updating user.info
239 {:ok, _actor} <- increase_note_count_if_public(actor, activity),
240 :ok <- maybe_federate(activity) do
243 {:fake, true, activity} ->
251 def accept(%{to: to, actor: actor, object: object} = params) do
252 # only accept false as false value
253 local = !(params[:local] == false)
255 with data <- %{"to" => to, "type" => "Accept", "actor" => actor.ap_id, "object" => object},
256 {:ok, activity} <- insert(data, local),
257 :ok <- maybe_federate(activity) do
262 def reject(%{to: to, actor: actor, object: object} = params) do
263 # only accept false as false value
264 local = !(params[:local] == false)
266 with data <- %{"to" => to, "type" => "Reject", "actor" => actor.ap_id, "object" => object},
267 {:ok, activity} <- insert(data, local),
268 :ok <- maybe_federate(activity) do
273 def update(%{to: to, cc: cc, actor: actor, object: object} = params) do
274 # only accept false as false value
275 local = !(params[:local] == false)
284 {:ok, activity} <- insert(data, local),
285 :ok <- maybe_federate(activity) do
290 # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
292 %User{ap_id: ap_id} = user,
293 %Object{data: %{"id" => _}} = object,
297 with nil <- get_existing_like(ap_id, object),
298 like_data <- make_like_data(user, object, activity_id),
299 {:ok, activity} <- insert(like_data, local),
300 {:ok, object} <- add_like_to_object(activity, object),
301 :ok <- maybe_federate(activity) do
302 {:ok, activity, object}
304 %Activity{} = activity -> {:ok, activity, object}
305 error -> {:error, error}
309 def unlike(%User{} = actor, %Object{} = object, activity_id \\ nil, local \\ true) do
310 with %Activity{} = like_activity <- get_existing_like(actor.ap_id, object),
311 unlike_data <- make_unlike_data(actor, like_activity, activity_id),
312 {:ok, unlike_activity} <- insert(unlike_data, local),
313 {:ok, _activity} <- Repo.delete(like_activity),
314 {:ok, object} <- remove_like_from_object(like_activity, object),
315 :ok <- maybe_federate(unlike_activity) do
316 {:ok, unlike_activity, like_activity, object}
323 %User{ap_id: _} = user,
324 %Object{data: %{"id" => _}} = object,
329 with true <- is_public?(object),
330 announce_data <- make_announce_data(user, object, activity_id, public),
331 {:ok, activity} <- insert(announce_data, local),
332 {:ok, object} <- add_announce_to_object(activity, object),
333 :ok <- maybe_federate(activity) do
334 {:ok, activity, object}
336 error -> {:error, error}
346 with %Activity{} = announce_activity <- get_existing_announce(actor.ap_id, object),
347 unannounce_data <- make_unannounce_data(actor, announce_activity, activity_id),
348 {:ok, unannounce_activity} <- insert(unannounce_data, local),
349 :ok <- maybe_federate(unannounce_activity),
350 {:ok, _activity} <- Repo.delete(announce_activity),
351 {:ok, object} <- remove_announce_from_object(announce_activity, object) do
352 {:ok, unannounce_activity, object}
358 def follow(follower, followed, activity_id \\ nil, local \\ true) do
359 with data <- make_follow_data(follower, followed, activity_id),
360 {:ok, activity} <- insert(data, local),
361 :ok <- maybe_federate(activity),
362 _ <- User.set_follow_state_cache(follower.ap_id, followed.ap_id, activity.data["state"]) do
367 def unfollow(follower, followed, activity_id \\ nil, local \\ true) do
368 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
369 {:ok, follow_activity} <- update_follow_state(follow_activity, "cancelled"),
370 unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
371 {:ok, activity} <- insert(unfollow_data, local),
372 :ok <- maybe_federate(activity) do
377 def delete(%User{ap_id: ap_id, follower_address: follower_address} = user) do
379 "to" => [follower_address],
382 "object" => %{"type" => "Person", "id" => ap_id}
384 {:ok, activity} <- insert(data, true, true, true),
385 :ok <- maybe_federate(activity) do
390 def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
391 user = User.get_cached_by_ap_id(actor)
392 to = (object.data["to"] || []) ++ (object.data["cc"] || [])
394 with {:ok, object, activity} <- Object.delete(object),
400 "deleted_activity_id" => activity && activity.id
402 {:ok, activity} <- insert(data, local, false),
403 stream_out_participations(object, user),
404 _ <- decrease_replies_count_if_reply(object),
405 # Changing note count prior to enqueuing federation task in order to avoid
406 # race conditions on updating user.info
407 {:ok, _actor} <- decrease_note_count_if_public(user, object),
408 :ok <- maybe_federate(activity) do
413 @spec block(User.t(), User.t(), String.t() | nil, boolean) :: {:ok, Activity.t() | nil}
414 def block(blocker, blocked, activity_id \\ nil, local \\ true) do
415 outgoing_blocks = Config.get([:activitypub, :outgoing_blocks])
416 unfollow_blocked = Config.get([:activitypub, :unfollow_blocked])
418 if unfollow_blocked do
419 follow_activity = fetch_latest_follow(blocker, blocked)
420 if follow_activity, do: unfollow(blocker, blocked, nil, local)
423 with true <- outgoing_blocks,
424 block_data <- make_block_data(blocker, blocked, activity_id),
425 {:ok, activity} <- insert(block_data, local),
426 :ok <- maybe_federate(activity) do
433 def unblock(blocker, blocked, activity_id \\ nil, local \\ true) do
434 with %Activity{} = block_activity <- fetch_latest_block(blocker, blocked),
435 unblock_data <- make_unblock_data(blocker, blocked, block_activity, activity_id),
436 {:ok, activity} <- insert(unblock_data, local),
437 :ok <- maybe_federate(activity) do
442 @spec flag(map()) :: {:ok, Activity.t()} | any
452 # only accept false as false value
453 local = !(params[:local] == false)
454 forward = !(params[:forward] == false)
456 additional = params[:additional] || %{}
460 Map.merge(additional, %{"to" => [], "cc" => [account.ap_id]})
462 Map.merge(additional, %{"to" => [], "cc" => []})
465 with flag_data <- make_flag_data(params, additional),
466 {:ok, activity} <- insert(flag_data, local),
467 :ok <- maybe_federate(activity) do
468 Enum.each(User.all_superusers(), fn superuser ->
470 |> Pleroma.Emails.AdminEmail.report(actor, account, statuses, content)
471 |> Pleroma.Emails.Mailer.deliver_async()
478 defp fetch_activities_for_context_query(context, opts) do
479 public = [Pleroma.Constants.as_public()]
482 if opts["user"], do: [opts["user"].ap_id | opts["user"].following] ++ public, else: public
484 from(activity in Activity)
485 |> maybe_preload_objects(opts)
486 |> maybe_preload_bookmarks(opts)
487 |> maybe_set_thread_muted_field(opts)
488 |> restrict_blocked(opts)
489 |> restrict_recipients(recipients, opts["user"])
493 "?->>'type' = ? and ?->>'context' = ?",
500 |> exclude_poll_votes(opts)
502 |> order_by([activity], desc: activity.id)
505 @spec fetch_activities_for_context(String.t(), keyword() | map()) :: [Activity.t()]
506 def fetch_activities_for_context(context, opts \\ %{}) do
508 |> fetch_activities_for_context_query(opts)
512 @spec fetch_latest_activity_id_for_context(String.t(), keyword() | map()) ::
513 Pleroma.FlakeId.t() | nil
514 def fetch_latest_activity_id_for_context(context, opts \\ %{}) do
516 |> fetch_activities_for_context_query(Map.merge(%{"skip_preload" => true}, opts))
522 def fetch_public_activities(opts \\ %{}) do
523 opts = Map.drop(opts, ["user"])
525 [Pleroma.Constants.as_public()]
526 |> fetch_activities_query(opts)
527 |> restrict_unlisted()
528 |> Pagination.fetch_paginated(opts)
532 @valid_visibilities ~w[direct unlisted public private]
534 defp restrict_visibility(query, %{visibility: visibility})
535 when is_list(visibility) do
536 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
542 "activity_visibility(?, ?, ?) = ANY (?)",
552 Logger.error("Could not restrict visibility to #{visibility}")
556 defp restrict_visibility(query, %{visibility: visibility})
557 when visibility in @valid_visibilities do
561 fragment("activity_visibility(?, ?, ?) = ?", a.actor, a.recipients, a.data, ^visibility)
565 defp restrict_visibility(_query, %{visibility: visibility})
566 when visibility not in @valid_visibilities do
567 Logger.error("Could not restrict visibility to #{visibility}")
570 defp restrict_visibility(query, _visibility), do: query
572 defp restrict_thread_visibility(query, _, %{skip_thread_containment: true} = _),
575 defp restrict_thread_visibility(
577 %{"user" => %User{info: %{skip_thread_containment: true}}},
582 defp restrict_thread_visibility(query, %{"user" => %User{ap_id: ap_id}}, _) do
585 where: fragment("thread_visibility(?, (?)->>'id') = true", ^ap_id, a.data)
589 defp restrict_thread_visibility(query, _, _), do: query
591 def fetch_user_activities(user, reading_user, params \\ %{}) do
594 |> Map.put("type", ["Create", "Announce"])
595 |> Map.put("user", reading_user)
596 |> Map.put("actor_id", user.ap_id)
597 |> Map.put("whole_db", true)
598 |> Map.put("pinned_activity_ids", user.info.pinned_activities)
601 user_activities_recipients(%{
602 "godmode" => params["godmode"],
603 "reading_user" => reading_user
606 fetch_activities(recipients, params)
610 defp user_activities_recipients(%{"godmode" => true}) do
614 defp user_activities_recipients(%{"reading_user" => reading_user}) do
616 [Pleroma.Constants.as_public()] ++ [reading_user.ap_id | reading_user.following]
618 [Pleroma.Constants.as_public()]
622 defp restrict_since(query, %{"since_id" => ""}), do: query
624 defp restrict_since(query, %{"since_id" => since_id}) do
625 from(activity in query, where: activity.id > ^since_id)
628 defp restrict_since(query, _), do: query
630 defp restrict_tag_reject(_query, %{"tag_reject" => _tag_reject, "skip_preload" => true}) do
631 raise "Can't use the child object without preloading!"
634 defp restrict_tag_reject(query, %{"tag_reject" => tag_reject})
635 when is_list(tag_reject) and tag_reject != [] do
637 [_activity, object] in query,
638 where: fragment("not (?)->'tag' \\?| (?)", object.data, ^tag_reject)
642 defp restrict_tag_reject(query, _), do: query
644 defp restrict_tag_all(_query, %{"tag_all" => _tag_all, "skip_preload" => true}) do
645 raise "Can't use the child object without preloading!"
648 defp restrict_tag_all(query, %{"tag_all" => tag_all})
649 when is_list(tag_all) and tag_all != [] do
651 [_activity, object] in query,
652 where: fragment("(?)->'tag' \\?& (?)", object.data, ^tag_all)
656 defp restrict_tag_all(query, _), do: query
658 defp restrict_tag(_query, %{"tag" => _tag, "skip_preload" => true}) do
659 raise "Can't use the child object without preloading!"
662 defp restrict_tag(query, %{"tag" => tag}) when is_list(tag) do
664 [_activity, object] in query,
665 where: fragment("(?)->'tag' \\?| (?)", object.data, ^tag)
669 defp restrict_tag(query, %{"tag" => tag}) when is_binary(tag) do
671 [_activity, object] in query,
672 where: fragment("(?)->'tag' \\? (?)", object.data, ^tag)
676 defp restrict_tag(query, _), do: query
678 defp restrict_recipients(query, [], _user), do: query
680 defp restrict_recipients(query, recipients, nil) do
681 from(activity in query, where: fragment("? && ?", ^recipients, activity.recipients))
684 defp restrict_recipients(query, recipients, user) do
687 where: fragment("? && ?", ^recipients, activity.recipients),
688 or_where: activity.actor == ^user.ap_id
692 defp restrict_local(query, %{"local_only" => true}) do
693 from(activity in query, where: activity.local == true)
696 defp restrict_local(query, _), do: query
698 defp restrict_actor(query, %{"actor_id" => actor_id}) do
699 from(activity in query, where: activity.actor == ^actor_id)
702 defp restrict_actor(query, _), do: query
704 defp restrict_type(query, %{"type" => type}) when is_binary(type) do
705 from(activity in query, where: fragment("?->>'type' = ?", activity.data, ^type))
708 defp restrict_type(query, %{"type" => type}) do
709 from(activity in query, where: fragment("?->>'type' = ANY(?)", activity.data, ^type))
712 defp restrict_type(query, _), do: query
714 defp restrict_state(query, %{"state" => state}) do
715 from(activity in query, where: fragment("?->>'state' = ?", activity.data, ^state))
718 defp restrict_state(query, _), do: query
720 defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
722 [_activity, object] in query,
723 where: fragment("(?)->'likes' \\? (?)", object.data, ^ap_id)
727 defp restrict_favorited_by(query, _), do: query
729 defp restrict_media(_query, %{"only_media" => _val, "skip_preload" => true}) do
730 raise "Can't use the child object without preloading!"
733 defp restrict_media(query, %{"only_media" => val}) when val == "true" or val == "1" do
735 [_activity, object] in query,
736 where: fragment("not (?)->'attachment' = (?)", object.data, ^[])
740 defp restrict_media(query, _), do: query
742 defp restrict_replies(query, %{"exclude_replies" => val}) when val == "true" or val == "1" do
745 where: fragment("?->'object'->>'inReplyTo' is null", activity.data)
749 defp restrict_replies(query, _), do: query
751 defp restrict_reblogs(query, %{"exclude_reblogs" => val}) when val == "true" or val == "1" do
752 from(activity in query, where: fragment("?->>'type' != 'Announce'", activity.data))
755 defp restrict_reblogs(query, _), do: query
757 defp restrict_muted(query, %{"with_muted" => val}) when val in [true, "true", "1"], do: query
759 defp restrict_muted(query, %{"muting_user" => %User{info: info}} = opts) do
763 from([activity] in query,
764 where: fragment("not (? = ANY(?))", activity.actor, ^mutes),
765 where: fragment("not (?->'to' \\?| ?)", activity.data, ^mutes)
768 unless opts["skip_preload"] do
769 from([thread_mute: tm] in query, where: is_nil(tm.user_id))
775 defp restrict_muted(query, _), do: query
777 defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
778 blocks = info.blocks || []
779 domain_blocks = info.domain_blocks || []
782 if has_named_binding?(query, :object), do: query, else: Activity.with_joined_object(query)
785 [activity, object: o] in query,
786 where: fragment("not (? = ANY(?))", activity.actor, ^blocks),
787 where: fragment("not (? && ?)", activity.recipients, ^blocks),
790 "not (?->>'type' = 'Announce' and ?->'to' \\?| ?)",
795 where: fragment("not (split_part(?, '/', 3) = ANY(?))", activity.actor, ^domain_blocks),
796 where: fragment("not (split_part(?->>'actor', '/', 3) = ANY(?))", o.data, ^domain_blocks)
800 defp restrict_blocked(query, _), do: query
802 defp restrict_unlisted(query) do
807 "not (coalesce(?->'cc', '{}'::jsonb) \\?| ?)",
809 ^[Pleroma.Constants.as_public()]
814 defp restrict_pinned(query, %{"pinned" => "true", "pinned_activity_ids" => ids}) do
815 from(activity in query, where: activity.id in ^ids)
818 defp restrict_pinned(query, _), do: query
820 defp restrict_muted_reblogs(query, %{"muting_user" => %User{info: info}}) do
821 muted_reblogs = info.muted_reblogs || []
827 "not ( ?->>'type' = 'Announce' and ? = ANY(?))",
835 defp restrict_muted_reblogs(query, _), do: query
837 defp exclude_poll_votes(query, %{"include_poll_votes" => "true"}), do: query
839 defp exclude_poll_votes(query, _) do
840 if has_named_binding?(query, :object) do
841 from([activity, object: o] in query,
842 where: fragment("not(?->>'type' = ?)", o.data, "Answer")
849 defp exclude_id(query, %{"exclude_id" => id}) when is_binary(id) do
850 from(activity in query, where: activity.id != ^id)
853 defp exclude_id(query, _), do: query
855 defp maybe_preload_objects(query, %{"skip_preload" => true}), do: query
857 defp maybe_preload_objects(query, _) do
859 |> Activity.with_preloaded_object()
862 defp maybe_preload_bookmarks(query, %{"skip_preload" => true}), do: query
864 defp maybe_preload_bookmarks(query, opts) do
866 |> Activity.with_preloaded_bookmark(opts["user"])
869 defp maybe_set_thread_muted_field(query, %{"skip_preload" => true}), do: query
871 defp maybe_set_thread_muted_field(query, opts) do
873 |> Activity.with_set_thread_muted_field(opts["muting_user"] || opts["user"])
876 defp maybe_order(query, %{order: :desc}) do
878 |> order_by(desc: :id)
881 defp maybe_order(query, %{order: :asc}) do
883 |> order_by(asc: :id)
886 defp maybe_order(query, _), do: query
888 def fetch_activities_query(recipients, opts \\ %{}) do
890 skip_thread_containment: Config.get([:instance, :skip_thread_containment])
894 |> maybe_preload_objects(opts)
895 |> maybe_preload_bookmarks(opts)
896 |> maybe_set_thread_muted_field(opts)
898 |> restrict_recipients(recipients, opts["user"])
899 |> restrict_tag(opts)
900 |> restrict_tag_reject(opts)
901 |> restrict_tag_all(opts)
902 |> restrict_since(opts)
903 |> restrict_local(opts)
904 |> restrict_actor(opts)
905 |> restrict_type(opts)
906 |> restrict_state(opts)
907 |> restrict_favorited_by(opts)
908 |> restrict_blocked(opts)
909 |> restrict_muted(opts)
910 |> restrict_media(opts)
911 |> restrict_visibility(opts)
912 |> restrict_thread_visibility(opts, config)
913 |> restrict_replies(opts)
914 |> restrict_reblogs(opts)
915 |> restrict_pinned(opts)
916 |> restrict_muted_reblogs(opts)
917 |> Activity.restrict_deactivated_users()
918 |> exclude_poll_votes(opts)
921 def fetch_activities(recipients, opts \\ %{}) do
922 list_memberships = Pleroma.List.memberships(opts["user"])
924 fetch_activities_query(recipients ++ list_memberships, opts)
925 |> Pagination.fetch_paginated(opts)
927 |> maybe_update_cc(list_memberships, opts["user"])
930 defp maybe_update_cc(activities, list_memberships, %User{ap_id: user_ap_id})
931 when is_list(list_memberships) and length(list_memberships) > 0 do
932 Enum.map(activities, fn
933 %{data: %{"bcc" => bcc}} = activity when is_list(bcc) and length(bcc) > 0 ->
934 if Enum.any?(bcc, &(&1 in list_memberships)) do
935 update_in(activity.data["cc"], &[user_ap_id | &1])
945 defp maybe_update_cc(activities, _, _), do: activities
947 def fetch_activities_bounded_query(query, recipients, recipients_with_public) do
948 from(activity in query,
950 fragment("? && ?", activity.recipients, ^recipients) or
951 (fragment("? && ?", activity.recipients, ^recipients_with_public) and
952 ^Pleroma.Constants.as_public() in activity.recipients)
956 def fetch_activities_bounded(recipients, recipients_with_public, opts \\ %{}) do
957 fetch_activities_query([], opts)
958 |> fetch_activities_bounded_query(recipients, recipients_with_public)
959 |> Pagination.fetch_paginated(opts)
963 def upload(file, opts \\ []) do
964 with {:ok, data} <- Upload.store(file, opts) do
967 Map.put(data, "actor", opts[:actor])
972 Repo.insert(%Object{data: obj_data})
976 defp object_to_user_data(data) do
978 data["icon"]["url"] &&
981 "url" => [%{"href" => data["icon"]["url"]}]
985 data["image"]["url"] &&
988 "url" => [%{"href" => data["image"]["url"]}]
993 |> Map.get("attachment", [])
994 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
995 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
997 locked = data["manuallyApprovesFollowers"] || false
998 data = Transmogrifier.maybe_fix_user_object(data)
1011 follower_address: data["followers"],
1012 following_address: data["following"],
1013 bio: data["summary"]
1016 # nickname can be nil because of virtual actors
1018 if data["preferredUsername"] do
1022 "#{data["preferredUsername"]}@#{URI.parse(data["id"]).host}"
1025 Map.put(user_data, :nickname, nil)
1031 def fetch_follow_information_for_user(user) do
1032 with {:ok, following_data} <-
1033 Fetcher.fetch_and_contain_remote_object_from_id(user.following_address),
1034 following_count when is_integer(following_count) <- following_data["totalItems"],
1035 {:ok, hide_follows} <- collection_private(following_data),
1036 {:ok, followers_data} <-
1037 Fetcher.fetch_and_contain_remote_object_from_id(user.follower_address),
1038 followers_count when is_integer(followers_count) <- followers_data["totalItems"],
1039 {:ok, hide_followers} <- collection_private(followers_data) do
1042 hide_follows: hide_follows,
1043 follower_count: followers_count,
1044 following_count: following_count,
1045 hide_followers: hide_followers
1056 defp maybe_update_follow_information(data) do
1057 with {:enabled, true} <-
1058 {:enabled, Pleroma.Config.get([:instance, :external_user_synchronization])},
1059 {:ok, info} <- fetch_follow_information_for_user(data) do
1060 info = Map.merge(data.info, info)
1061 Map.put(data, :info, info)
1063 {:enabled, false} ->
1068 "Follower/Following counter update for #{data.ap_id} failed.\n" <> inspect(e)
1075 defp collection_private(data) do
1076 if is_map(data["first"]) and
1077 data["first"]["type"] in ["CollectionPage", "OrderedCollectionPage"] do
1080 with {:ok, %{"type" => type}} when type in ["CollectionPage", "OrderedCollectionPage"] <-
1081 Fetcher.fetch_and_contain_remote_object_from_id(data["first"]) do
1084 {:error, {:ok, %{status: code}}} when code in [401, 403] ->
1096 def user_data_from_user_object(data) do
1097 with {:ok, data} <- MRF.filter(data),
1098 {:ok, data} <- object_to_user_data(data) do
1105 def fetch_and_prepare_user_from_ap_id(ap_id) do
1106 with {:ok, data} <- Fetcher.fetch_and_contain_remote_object_from_id(ap_id),
1107 {:ok, data} <- user_data_from_user_object(data),
1108 data <- maybe_update_follow_information(data) do
1111 e -> Logger.error("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
1115 def make_user_from_ap_id(ap_id) do
1116 if _user = User.get_cached_by_ap_id(ap_id) do
1117 Transmogrifier.upgrade_user_from_ap_id(ap_id)
1119 with {:ok, data} <- fetch_and_prepare_user_from_ap_id(ap_id) do
1120 User.insert_or_update_user(data)
1127 def make_user_from_nickname(nickname) do
1128 with {:ok, %{"ap_id" => ap_id}} when not is_nil(ap_id) <- WebFinger.finger(nickname) do
1129 make_user_from_ap_id(ap_id)
1131 _e -> {:error, "No AP id in WebFinger"}
1135 # filter out broken threads
1136 def contain_broken_threads(%Activity{} = activity, %User{} = user) do
1137 entire_thread_visible_for_user?(activity, user)
1140 # do post-processing on a specific activity
1141 def contain_activity(%Activity{} = activity, %User{} = user) do
1142 contain_broken_threads(activity, user)
1145 def fetch_direct_messages_query do
1147 |> restrict_type(%{"type" => "Create"})
1148 |> restrict_visibility(%{visibility: "direct"})
1149 |> order_by([activity], asc: activity.id)