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, bypass_actor_check \\ false) when is_map(map) do
123 with nil <- Activity.normalize(map),
124 map <- lazy_put_activity_defaults(map, fake),
125 true <- bypass_actor_check || 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 not 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}
334 def unlike(%User{} = actor, %Object{} = object, activity_id \\ nil, local \\ true) do
335 with %Activity{} = like_activity <- get_existing_like(actor.ap_id, object),
336 unlike_data <- make_unlike_data(actor, like_activity, activity_id),
337 {:ok, unlike_activity} <- insert(unlike_data, local),
338 {:ok, _activity} <- Repo.delete(like_activity),
339 {:ok, object} <- remove_like_from_object(like_activity, object),
340 :ok <- maybe_federate(unlike_activity) do
341 {:ok, unlike_activity, like_activity, object}
348 %User{ap_id: _} = user,
349 %Object{data: %{"id" => _}} = object,
354 with true <- is_public?(object),
355 announce_data <- make_announce_data(user, object, activity_id, public),
356 {:ok, activity} <- insert(announce_data, local),
357 {:ok, object} <- add_announce_to_object(activity, object),
358 :ok <- maybe_federate(activity) do
359 {:ok, activity, object}
361 error -> {:error, error}
371 with %Activity{} = announce_activity <- get_existing_announce(actor.ap_id, object),
372 unannounce_data <- make_unannounce_data(actor, announce_activity, activity_id),
373 {:ok, unannounce_activity} <- insert(unannounce_data, local),
374 :ok <- maybe_federate(unannounce_activity),
375 {:ok, _activity} <- Repo.delete(announce_activity),
376 {:ok, object} <- remove_announce_from_object(announce_activity, object) do
377 {:ok, unannounce_activity, object}
383 def follow(follower, followed, activity_id \\ nil, local \\ true) do
384 with data <- make_follow_data(follower, followed, activity_id),
385 {:ok, activity} <- insert(data, local),
386 :ok <- maybe_federate(activity),
387 _ <- User.set_follow_state_cache(follower.ap_id, followed.ap_id, activity.data["state"]) do
392 def unfollow(follower, followed, activity_id \\ nil, local \\ true) do
393 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
394 {:ok, follow_activity} <- update_follow_state(follow_activity, "cancelled"),
395 unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
396 {:ok, activity} <- insert(unfollow_data, local),
397 :ok <- maybe_federate(activity) do
402 def delete(%User{ap_id: ap_id, follower_address: follower_address} = user) do
404 "to" => [follower_address],
407 "object" => %{"type" => "Person", "id" => ap_id}
409 {:ok, activity} <- insert(data, true, true, true),
410 :ok <- maybe_federate(activity) do
415 def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
416 user = User.get_cached_by_ap_id(actor)
417 to = (object.data["to"] || []) ++ (object.data["cc"] || [])
419 with {:ok, object, activity} <- Object.delete(object),
425 "deleted_activity_id" => activity && activity.id
427 {:ok, activity} <- insert(data, local, false),
428 stream_out_participations(object, user),
429 _ <- decrease_replies_count_if_reply(object),
430 # Changing note count prior to enqueuing federation task in order to avoid
431 # race conditions on updating user.info
432 {:ok, _actor} <- decrease_note_count_if_public(user, object),
433 :ok <- maybe_federate(activity) do
438 def block(blocker, blocked, activity_id \\ nil, local \\ true) do
439 outgoing_blocks = Config.get([:activitypub, :outgoing_blocks])
440 unfollow_blocked = Config.get([:activitypub, :unfollow_blocked])
442 if unfollow_blocked do
443 follow_activity = fetch_latest_follow(blocker, blocked)
444 if follow_activity, do: unfollow(blocker, blocked, nil, local)
447 with true <- outgoing_blocks,
448 block_data <- make_block_data(blocker, blocked, activity_id),
449 {:ok, activity} <- insert(block_data, local),
450 :ok <- maybe_federate(activity) do
457 def unblock(blocker, blocked, activity_id \\ nil, local \\ true) do
458 with %Activity{} = block_activity <- fetch_latest_block(blocker, blocked),
459 unblock_data <- make_unblock_data(blocker, blocked, block_activity, activity_id),
460 {:ok, activity} <- insert(unblock_data, local),
461 :ok <- maybe_federate(activity) do
475 # only accept false as false value
476 local = !(params[:local] == false)
477 forward = !(params[:forward] == false)
479 additional = params[:additional] || %{}
491 Map.merge(additional, %{"to" => [], "cc" => [account.ap_id]})
493 Map.merge(additional, %{"to" => [], "cc" => []})
496 with flag_data <- make_flag_data(params, additional),
497 {:ok, activity} <- insert(flag_data, local),
498 :ok <- maybe_federate(activity) do
499 Enum.each(User.all_superusers(), fn superuser ->
501 |> Pleroma.Emails.AdminEmail.report(actor, account, statuses, content)
502 |> Pleroma.Emails.Mailer.deliver_async()
509 defp fetch_activities_for_context_query(context, opts) do
510 public = [Pleroma.Constants.as_public()]
513 if opts["user"], do: [opts["user"].ap_id | opts["user"].following] ++ public, else: public
515 from(activity in Activity)
516 |> maybe_preload_objects(opts)
517 |> maybe_preload_bookmarks(opts)
518 |> maybe_set_thread_muted_field(opts)
519 |> restrict_blocked(opts)
520 |> restrict_recipients(recipients, opts["user"])
524 "?->>'type' = ? and ?->>'context' = ?",
531 |> exclude_poll_votes(opts)
533 |> order_by([activity], desc: activity.id)
536 @spec fetch_activities_for_context(String.t(), keyword() | map()) :: [Activity.t()]
537 def fetch_activities_for_context(context, opts \\ %{}) do
539 |> fetch_activities_for_context_query(opts)
543 @spec fetch_latest_activity_id_for_context(String.t(), keyword() | map()) ::
544 Pleroma.FlakeId.t() | nil
545 def fetch_latest_activity_id_for_context(context, opts \\ %{}) do
547 |> fetch_activities_for_context_query(Map.merge(%{"skip_preload" => true}, opts))
553 def fetch_public_activities(opts \\ %{}) do
554 q = fetch_activities_query([Pleroma.Constants.as_public()], opts)
557 |> restrict_unlisted()
558 |> Pagination.fetch_paginated(opts)
562 @valid_visibilities ~w[direct unlisted public private]
564 defp restrict_visibility(query, %{visibility: visibility})
565 when is_list(visibility) do
566 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
572 "activity_visibility(?, ?, ?) = ANY (?)",
582 Logger.error("Could not restrict visibility to #{visibility}")
586 defp restrict_visibility(query, %{visibility: visibility})
587 when visibility in @valid_visibilities do
591 fragment("activity_visibility(?, ?, ?) = ?", a.actor, a.recipients, a.data, ^visibility)
595 defp restrict_visibility(_query, %{visibility: visibility})
596 when visibility not in @valid_visibilities do
597 Logger.error("Could not restrict visibility to #{visibility}")
600 defp restrict_visibility(query, _visibility), do: query
602 defp restrict_thread_visibility(query, _, %{skip_thread_containment: true} = _),
605 defp restrict_thread_visibility(
607 %{"user" => %User{info: %{skip_thread_containment: true}}},
612 defp restrict_thread_visibility(query, %{"user" => %User{ap_id: ap_id}}, _) do
615 where: fragment("thread_visibility(?, (?)->>'id') = true", ^ap_id, a.data)
619 defp restrict_thread_visibility(query, _, _), do: query
621 def fetch_user_activities(user, reading_user, params \\ %{}) do
624 |> Map.put("type", ["Create", "Announce"])
625 |> Map.put("user", reading_user)
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}} = opts) do
793 from([activity] in query,
794 where: fragment("not (? = ANY(?))", activity.actor, ^mutes),
795 where: fragment("not (?->'to' \\?| ?)", activity.data, ^mutes)
798 unless opts["skip_preload"] do
799 from([thread_mute: tm] in query, where: is_nil(tm.user_id))
805 defp restrict_muted(query, _), do: query
807 defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
808 blocks = info.blocks || []
809 domain_blocks = info.domain_blocks || []
812 if has_named_binding?(query, :object), do: query, else: Activity.with_joined_object(query)
815 [activity, object: o] in query,
816 where: fragment("not (? = ANY(?))", activity.actor, ^blocks),
817 where: fragment("not (? && ?)", activity.recipients, ^blocks),
820 "not (?->>'type' = 'Announce' and ?->'to' \\?| ?)",
825 where: fragment("not (split_part(?, '/', 3) = ANY(?))", activity.actor, ^domain_blocks),
826 where: fragment("not (split_part(?->>'actor', '/', 3) = ANY(?))", o.data, ^domain_blocks)
830 defp restrict_blocked(query, _), do: query
832 defp restrict_unlisted(query) do
837 "not (coalesce(?->'cc', '{}'::jsonb) \\?| ?)",
839 ^[Pleroma.Constants.as_public()]
844 defp restrict_pinned(query, %{"pinned" => "true", "pinned_activity_ids" => ids}) do
845 from(activity in query, where: activity.id in ^ids)
848 defp restrict_pinned(query, _), do: query
850 defp restrict_muted_reblogs(query, %{"muting_user" => %User{info: info}}) do
851 muted_reblogs = info.muted_reblogs || []
857 "not ( ?->>'type' = 'Announce' and ? = ANY(?))",
865 defp restrict_muted_reblogs(query, _), do: query
867 defp exclude_poll_votes(query, %{"include_poll_votes" => "true"}), do: query
869 defp exclude_poll_votes(query, _) do
870 if has_named_binding?(query, :object) do
871 from([activity, object: o] in query,
872 where: fragment("not(?->>'type' = ?)", o.data, "Answer")
879 defp exclude_id(query, %{"exclude_id" => id}) when is_binary(id) do
880 from(activity in query, where: activity.id != ^id)
883 defp exclude_id(query, _), do: query
885 defp maybe_preload_objects(query, %{"skip_preload" => true}), do: query
887 defp maybe_preload_objects(query, _) do
889 |> Activity.with_preloaded_object()
892 defp maybe_preload_bookmarks(query, %{"skip_preload" => true}), do: query
894 defp maybe_preload_bookmarks(query, opts) do
896 |> Activity.with_preloaded_bookmark(opts["user"])
899 defp maybe_set_thread_muted_field(query, %{"skip_preload" => true}), do: query
901 defp maybe_set_thread_muted_field(query, opts) do
903 |> Activity.with_set_thread_muted_field(opts["muting_user"] || opts["user"])
906 defp maybe_order(query, %{order: :desc}) do
908 |> order_by(desc: :id)
911 defp maybe_order(query, %{order: :asc}) do
913 |> order_by(asc: :id)
916 defp maybe_order(query, _), do: query
918 def fetch_activities_query(recipients, opts \\ %{}) do
920 skip_thread_containment: Config.get([:instance, :skip_thread_containment])
924 |> maybe_preload_objects(opts)
925 |> maybe_preload_bookmarks(opts)
926 |> maybe_set_thread_muted_field(opts)
928 |> restrict_recipients(recipients, opts["user"])
929 |> restrict_tag(opts)
930 |> restrict_tag_reject(opts)
931 |> restrict_tag_all(opts)
932 |> restrict_since(opts)
933 |> restrict_local(opts)
934 |> restrict_actor(opts)
935 |> restrict_type(opts)
936 |> restrict_state(opts)
937 |> restrict_favorited_by(opts)
938 |> restrict_blocked(opts)
939 |> restrict_muted(opts)
940 |> restrict_media(opts)
941 |> restrict_visibility(opts)
942 |> restrict_thread_visibility(opts, config)
943 |> restrict_replies(opts)
944 |> restrict_reblogs(opts)
945 |> restrict_pinned(opts)
946 |> restrict_muted_reblogs(opts)
947 |> Activity.restrict_deactivated_users()
948 |> exclude_poll_votes(opts)
951 def fetch_activities(recipients, opts \\ %{}) do
952 list_memberships = Pleroma.List.memberships(opts["user"])
954 fetch_activities_query(recipients ++ list_memberships, opts)
955 |> Pagination.fetch_paginated(opts)
957 |> maybe_update_cc(list_memberships, opts["user"])
960 defp maybe_update_cc(activities, list_memberships, %User{ap_id: user_ap_id})
961 when is_list(list_memberships) and length(list_memberships) > 0 do
962 Enum.map(activities, fn
963 %{data: %{"bcc" => bcc}} = activity when is_list(bcc) and length(bcc) > 0 ->
964 if Enum.any?(bcc, &(&1 in list_memberships)) do
965 update_in(activity.data["cc"], &[user_ap_id | &1])
975 defp maybe_update_cc(activities, _, _), do: activities
977 def fetch_activities_bounded_query(query, recipients, recipients_with_public) do
978 from(activity in query,
980 fragment("? && ?", activity.recipients, ^recipients) or
981 (fragment("? && ?", activity.recipients, ^recipients_with_public) and
982 ^Pleroma.Constants.as_public() in activity.recipients)
986 def fetch_activities_bounded(recipients, recipients_with_public, opts \\ %{}) do
987 fetch_activities_query([], opts)
988 |> fetch_activities_bounded_query(recipients, recipients_with_public)
989 |> Pagination.fetch_paginated(opts)
993 def upload(file, opts \\ []) do
994 with {:ok, data} <- Upload.store(file, opts) do
997 Map.put(data, "actor", opts[:actor])
1002 Repo.insert(%Object{data: obj_data})
1006 defp object_to_user_data(data) do
1008 data["icon"]["url"] &&
1011 "url" => [%{"href" => data["icon"]["url"]}]
1015 data["image"]["url"] &&
1018 "url" => [%{"href" => data["image"]["url"]}]
1023 |> Map.get("attachment", [])
1024 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
1025 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
1027 locked = data["manuallyApprovesFollowers"] || false
1028 data = Transmogrifier.maybe_fix_user_object(data)
1041 follower_address: data["followers"],
1042 following_address: data["following"],
1043 bio: data["summary"]
1046 # nickname can be nil because of virtual actors
1048 if data["preferredUsername"] do
1052 "#{data["preferredUsername"]}@#{URI.parse(data["id"]).host}"
1055 Map.put(user_data, :nickname, nil)
1061 def fetch_follow_information_for_user(user) do
1062 with {:ok, following_data} <-
1063 Fetcher.fetch_and_contain_remote_object_from_id(user.following_address),
1064 following_count when is_integer(following_count) <- following_data["totalItems"],
1065 {:ok, hide_follows} <- collection_private(following_data),
1066 {:ok, followers_data} <-
1067 Fetcher.fetch_and_contain_remote_object_from_id(user.follower_address),
1068 followers_count when is_integer(followers_count) <- followers_data["totalItems"],
1069 {:ok, hide_followers} <- collection_private(followers_data) do
1072 hide_follows: hide_follows,
1073 follower_count: followers_count,
1074 following_count: following_count,
1075 hide_followers: hide_followers
1086 defp maybe_update_follow_information(data) do
1087 with {:enabled, true} <-
1088 {:enabled, Pleroma.Config.get([:instance, :external_user_synchronization])},
1089 {:ok, info} <- fetch_follow_information_for_user(data) do
1090 info = Map.merge(data.info, info)
1091 Map.put(data, :info, info)
1093 {:enabled, false} ->
1098 "Follower/Following counter update for #{data.ap_id} failed.\n" <> inspect(e)
1105 defp collection_private(data) do
1106 if is_map(data["first"]) and
1107 data["first"]["type"] in ["CollectionPage", "OrderedCollectionPage"] do
1110 with {:ok, %{"type" => type}} when type in ["CollectionPage", "OrderedCollectionPage"] <-
1111 Fetcher.fetch_and_contain_remote_object_from_id(data["first"]) do
1114 {:error, {:ok, %{status: code}}} when code in [401, 403] ->
1126 def user_data_from_user_object(data) do
1127 with {:ok, data} <- MRF.filter(data),
1128 {:ok, data} <- object_to_user_data(data) do
1135 def fetch_and_prepare_user_from_ap_id(ap_id) do
1136 with {:ok, data} <- Fetcher.fetch_and_contain_remote_object_from_id(ap_id),
1137 {:ok, data} <- user_data_from_user_object(data),
1138 data <- maybe_update_follow_information(data) do
1141 e -> Logger.error("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
1145 def make_user_from_ap_id(ap_id) do
1146 if _user = User.get_cached_by_ap_id(ap_id) do
1147 Transmogrifier.upgrade_user_from_ap_id(ap_id)
1149 with {:ok, data} <- fetch_and_prepare_user_from_ap_id(ap_id) do
1150 User.insert_or_update_user(data)
1157 def make_user_from_nickname(nickname) do
1158 with {:ok, %{"ap_id" => ap_id}} when not is_nil(ap_id) <- WebFinger.finger(nickname) do
1159 make_user_from_ap_id(ap_id)
1161 _e -> {:error, "No AP id in WebFinger"}
1165 # filter out broken threads
1166 def contain_broken_threads(%Activity{} = activity, %User{} = user) do
1167 entire_thread_visible_for_user?(activity, user)
1170 # do post-processing on a specific activity
1171 def contain_activity(%Activity{} = activity, %User{} = user) do
1172 contain_broken_threads(activity, user)
1175 def fetch_direct_messages_query do
1177 |> restrict_type(%{"type" => "Create"})
1178 |> restrict_visibility(%{visibility: "direct"})
1179 |> order_by([activity], asc: activity.id)