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
20 alias Pleroma.Workers.BackgroundWorker
23 import Pleroma.Web.ActivityPub.Utils
24 import Pleroma.Web.ActivityPub.Visibility
27 require Pleroma.Constants
29 # For Announce activities, we filter the recipients based on following status for any actors
30 # that match actual users. See issue #164 for more information about why this is necessary.
31 defp get_recipients(%{"type" => "Announce"} = data) do
32 to = Map.get(data, "to", [])
33 cc = Map.get(data, "cc", [])
34 bcc = Map.get(data, "bcc", [])
35 actor = User.get_cached_by_ap_id(data["actor"])
38 Enum.filter(Enum.concat([to, cc, bcc]), fn recipient ->
39 case User.get_cached_by_ap_id(recipient) do
41 user -> User.following?(user, actor)
48 defp get_recipients(%{"type" => "Create"} = data) do
49 to = Map.get(data, "to", [])
50 cc = Map.get(data, "cc", [])
51 bcc = Map.get(data, "bcc", [])
52 actor = Map.get(data, "actor", [])
53 recipients = [to, cc, bcc, [actor]] |> Enum.concat() |> Enum.uniq()
57 defp get_recipients(data) do
58 to = Map.get(data, "to", [])
59 cc = Map.get(data, "cc", [])
60 bcc = Map.get(data, "bcc", [])
61 recipients = Enum.concat([to, cc, bcc])
65 defp check_actor_is_active(actor) do
66 if not is_nil(actor) do
67 with user <- User.get_cached_by_ap_id(actor),
68 false <- user.info.deactivated do
78 defp check_remote_limit(%{"object" => %{"content" => content}}) when not is_nil(content) do
79 limit = Config.get([:instance, :remote_limit])
80 String.length(content) <= limit
83 defp check_remote_limit(_), do: true
85 def increase_note_count_if_public(actor, object) do
86 if is_public?(object), do: User.increase_note_count(actor), else: {:ok, actor}
89 def decrease_note_count_if_public(actor, object) do
90 if is_public?(object), do: User.decrease_note_count(actor), else: {:ok, actor}
93 def increase_replies_count_if_reply(%{
94 "object" => %{"inReplyTo" => reply_ap_id} = object,
97 if is_public?(object) do
98 Object.increase_replies_count(reply_ap_id)
102 def increase_replies_count_if_reply(_create_data), do: :noop
104 def decrease_replies_count_if_reply(%Object{
105 data: %{"inReplyTo" => reply_ap_id} = object
107 if is_public?(object) do
108 Object.decrease_replies_count(reply_ap_id)
112 def decrease_replies_count_if_reply(_object), do: :noop
114 def increase_poll_votes_if_vote(%{
115 "object" => %{"inReplyTo" => reply_ap_id, "name" => name},
118 Object.increase_vote_count(reply_ap_id, name)
121 def increase_poll_votes_if_vote(_create_data), do: :noop
123 def insert(map, local \\ true, fake \\ false, bypass_actor_check \\ false) when is_map(map) do
124 with nil <- Activity.normalize(map),
125 map <- lazy_put_activity_defaults(map, fake),
126 true <- bypass_actor_check || check_actor_is_active(map["actor"]),
127 {_, true} <- {:remote_limit_error, check_remote_limit(map)},
128 {:ok, map} <- MRF.filter(map),
129 {recipients, _, _} = get_recipients(map),
130 {:fake, false, map, recipients} <- {:fake, fake, map, recipients},
131 :ok <- Containment.contain_child(map),
132 {:ok, map, object} <- insert_full_object(map) do
134 Repo.insert(%Activity{
138 recipients: recipients
141 # Splice in the child object if we have one.
143 if not is_nil(object) do
144 Map.put(activity, :object, object)
149 BackgroundWorker.enqueue("fetch_data_for_activity", %{"activity_id" => activity.id})
151 Notification.create_notifications(activity)
155 |> Conversation.create_or_bump_for()
156 |> get_participations()
159 stream_out_participations(participations)
162 %Activity{} = activity ->
165 {:fake, true, map, recipients} ->
166 activity = %Activity{
170 recipients: recipients,
174 Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
182 defp get_participations({:ok, %{participations: participations}}), do: participations
183 defp get_participations(_), do: []
185 def stream_out_participations(participations) do
188 |> Repo.preload(:user)
190 Enum.each(participations, fn participation ->
191 Pleroma.Web.Streamer.stream("participation", participation)
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) do
212 if activity.data["type"] in ["Create", "Announce", "Delete"] do
213 object = Object.normalize(activity)
214 # Do not stream out poll replies
215 unless object.data["type"] == "Answer" do
216 Pleroma.Web.Streamer.stream("user", activity)
217 Pleroma.Web.Streamer.stream("list", activity)
219 if get_visibility(activity) == "public" do
220 Pleroma.Web.Streamer.stream("public", activity)
223 Pleroma.Web.Streamer.stream("public:local", activity)
226 if activity.data["type"] in ["Create"] do
228 |> Map.get("tag", [])
229 |> Enum.filter(fn tag -> is_bitstring(tag) end)
230 |> Enum.each(fn tag -> Pleroma.Web.Streamer.stream("hashtag:" <> tag, activity) end)
232 if object.data["attachment"] != [] do
233 Pleroma.Web.Streamer.stream("public:media", activity)
236 Pleroma.Web.Streamer.stream("public:local:media", activity)
241 if get_visibility(activity) == "direct",
242 do: Pleroma.Web.Streamer.stream("direct", activity)
248 def create(%{to: to, actor: actor, context: context, object: object} = params, fake \\ false) do
249 additional = params[:additional] || %{}
250 # only accept false as false value
251 local = !(params[:local] == false)
252 published = params[:published]
256 %{to: to, actor: actor, published: published, context: context, object: object},
259 {:ok, activity} <- insert(create_data, local, fake),
260 {:fake, false, activity} <- {:fake, fake, activity},
261 _ <- increase_replies_count_if_reply(create_data),
262 _ <- increase_poll_votes_if_vote(create_data),
263 # Changing note count prior to enqueuing federation task in order to avoid
264 # race conditions on updating user.info
265 {:ok, _actor} <- increase_note_count_if_public(actor, activity),
266 :ok <- maybe_federate(activity) do
269 {:fake, true, activity} ->
277 def accept(%{to: to, actor: actor, object: object} = params) do
278 # only accept false as false value
279 local = !(params[:local] == false)
281 with data <- %{"to" => to, "type" => "Accept", "actor" => actor.ap_id, "object" => object},
282 {:ok, activity} <- insert(data, local),
283 :ok <- maybe_federate(activity) do
288 def reject(%{to: to, actor: actor, object: object} = params) do
289 # only accept false as false value
290 local = !(params[:local] == false)
292 with data <- %{"to" => to, "type" => "Reject", "actor" => actor.ap_id, "object" => object},
293 {:ok, activity} <- insert(data, local),
294 :ok <- maybe_federate(activity) do
299 def update(%{to: to, cc: cc, actor: actor, object: object} = params) do
300 # only accept false as false value
301 local = !(params[:local] == false)
310 {:ok, activity} <- insert(data, local),
311 :ok <- maybe_federate(activity) do
316 # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
318 %User{ap_id: ap_id} = user,
319 %Object{data: %{"id" => _}} = object,
323 with nil <- get_existing_like(ap_id, object),
324 like_data <- make_like_data(user, object, activity_id),
325 {:ok, activity} <- insert(like_data, local),
326 {:ok, object} <- add_like_to_object(activity, object),
327 :ok <- maybe_federate(activity) do
328 {:ok, activity, object}
330 %Activity{} = activity -> {:ok, activity, object}
331 error -> {:error, error}
335 def unlike(%User{} = actor, %Object{} = object, activity_id \\ nil, local \\ true) do
336 with %Activity{} = like_activity <- get_existing_like(actor.ap_id, object),
337 unlike_data <- make_unlike_data(actor, like_activity, activity_id),
338 {:ok, unlike_activity} <- insert(unlike_data, local),
339 {:ok, _activity} <- Repo.delete(like_activity),
340 {:ok, object} <- remove_like_from_object(like_activity, object),
341 :ok <- maybe_federate(unlike_activity) do
342 {:ok, unlike_activity, like_activity, object}
349 %User{ap_id: _} = user,
350 %Object{data: %{"id" => _}} = object,
355 with true <- is_public?(object),
356 announce_data <- make_announce_data(user, object, activity_id, public),
357 {:ok, activity} <- insert(announce_data, local),
358 {:ok, object} <- add_announce_to_object(activity, object),
359 :ok <- maybe_federate(activity) do
360 {:ok, activity, object}
362 error -> {:error, error}
372 with %Activity{} = announce_activity <- get_existing_announce(actor.ap_id, object),
373 unannounce_data <- make_unannounce_data(actor, announce_activity, activity_id),
374 {:ok, unannounce_activity} <- insert(unannounce_data, local),
375 :ok <- maybe_federate(unannounce_activity),
376 {:ok, _activity} <- Repo.delete(announce_activity),
377 {:ok, object} <- remove_announce_from_object(announce_activity, object) do
378 {:ok, unannounce_activity, object}
384 def follow(follower, followed, activity_id \\ nil, local \\ true) do
385 with data <- make_follow_data(follower, followed, activity_id),
386 {:ok, activity} <- insert(data, local),
387 :ok <- maybe_federate(activity),
388 _ <- User.set_follow_state_cache(follower.ap_id, followed.ap_id, activity.data["state"]) do
393 def unfollow(follower, followed, activity_id \\ nil, local \\ true) do
394 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
395 {:ok, follow_activity} <- update_follow_state(follow_activity, "cancelled"),
396 unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
397 {:ok, activity} <- insert(unfollow_data, local),
398 :ok <- maybe_federate(activity) do
403 def delete(%User{ap_id: ap_id, follower_address: follower_address} = user) do
405 "to" => [follower_address],
408 "object" => %{"type" => "Person", "id" => ap_id}
410 {:ok, activity} <- insert(data, true, true, true),
411 :ok <- maybe_federate(activity) do
416 def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
417 user = User.get_cached_by_ap_id(actor)
418 to = (object.data["to"] || []) ++ (object.data["cc"] || [])
420 with {:ok, object, activity} <- Object.delete(object),
426 "deleted_activity_id" => activity && activity.id
428 {:ok, activity} <- insert(data, local, false),
429 stream_out_participations(object, user),
430 _ <- decrease_replies_count_if_reply(object),
431 # Changing note count prior to enqueuing federation task in order to avoid
432 # race conditions on updating user.info
433 {:ok, _actor} <- decrease_note_count_if_public(user, object),
434 :ok <- maybe_federate(activity) do
439 def block(blocker, blocked, activity_id \\ nil, local \\ true) do
440 outgoing_blocks = Config.get([:activitypub, :outgoing_blocks])
441 unfollow_blocked = Config.get([:activitypub, :unfollow_blocked])
443 if unfollow_blocked do
444 follow_activity = fetch_latest_follow(blocker, blocked)
445 if follow_activity, do: unfollow(blocker, blocked, nil, local)
448 with true <- outgoing_blocks,
449 block_data <- make_block_data(blocker, blocked, activity_id),
450 {:ok, activity} <- insert(block_data, local),
451 :ok <- maybe_federate(activity) do
458 def unblock(blocker, blocked, activity_id \\ nil, local \\ true) do
459 with %Activity{} = block_activity <- fetch_latest_block(blocker, blocked),
460 unblock_data <- make_unblock_data(blocker, blocked, block_activity, activity_id),
461 {:ok, activity} <- insert(unblock_data, local),
462 :ok <- maybe_federate(activity) do
476 # only accept false as false value
477 local = !(params[:local] == false)
478 forward = !(params[:forward] == false)
480 additional = params[:additional] || %{}
492 Map.merge(additional, %{"to" => [], "cc" => [account.ap_id]})
494 Map.merge(additional, %{"to" => [], "cc" => []})
497 with flag_data <- make_flag_data(params, additional),
498 {:ok, activity} <- insert(flag_data, local),
499 :ok <- maybe_federate(activity) do
500 Enum.each(User.all_superusers(), fn superuser ->
502 |> Pleroma.Emails.AdminEmail.report(actor, account, statuses, content)
503 |> Pleroma.Emails.Mailer.deliver_async()
510 defp fetch_activities_for_context_query(context, opts) do
511 public = [Pleroma.Constants.as_public()]
514 if opts["user"], do: [opts["user"].ap_id | opts["user"].following] ++ public, else: public
516 from(activity in Activity)
517 |> maybe_preload_objects(opts)
518 |> maybe_preload_bookmarks(opts)
519 |> maybe_set_thread_muted_field(opts)
520 |> restrict_blocked(opts)
521 |> restrict_recipients(recipients, opts["user"])
525 "?->>'type' = ? and ?->>'context' = ?",
532 |> 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("user", reading_user)
627 |> Map.put("actor_id", user.ap_id)
628 |> Map.put("whole_db", true)
629 |> Map.put("pinned_activity_ids", user.info.pinned_activities)
632 user_activities_recipients(%{
633 "godmode" => params["godmode"],
634 "reading_user" => reading_user
637 fetch_activities(recipients, params)
641 defp user_activities_recipients(%{"godmode" => true}) do
645 defp user_activities_recipients(%{"reading_user" => reading_user}) do
647 [Pleroma.Constants.as_public()] ++ [reading_user.ap_id | reading_user.following]
649 [Pleroma.Constants.as_public()]
653 defp restrict_since(query, %{"since_id" => ""}), do: query
655 defp restrict_since(query, %{"since_id" => since_id}) do
656 from(activity in query, where: activity.id > ^since_id)
659 defp restrict_since(query, _), do: query
661 defp restrict_tag_reject(_query, %{"tag_reject" => _tag_reject, "skip_preload" => true}) do
662 raise "Can't use the child object without preloading!"
665 defp restrict_tag_reject(query, %{"tag_reject" => tag_reject})
666 when is_list(tag_reject) and tag_reject != [] do
668 [_activity, object] in query,
669 where: fragment("not (?)->'tag' \\?| (?)", object.data, ^tag_reject)
673 defp restrict_tag_reject(query, _), do: query
675 defp restrict_tag_all(_query, %{"tag_all" => _tag_all, "skip_preload" => true}) do
676 raise "Can't use the child object without preloading!"
679 defp restrict_tag_all(query, %{"tag_all" => tag_all})
680 when is_list(tag_all) and tag_all != [] do
682 [_activity, object] in query,
683 where: fragment("(?)->'tag' \\?& (?)", object.data, ^tag_all)
687 defp restrict_tag_all(query, _), do: query
689 defp restrict_tag(_query, %{"tag" => _tag, "skip_preload" => true}) do
690 raise "Can't use the child object without preloading!"
693 defp restrict_tag(query, %{"tag" => tag}) when is_list(tag) do
695 [_activity, object] in query,
696 where: fragment("(?)->'tag' \\?| (?)", object.data, ^tag)
700 defp restrict_tag(query, %{"tag" => tag}) when is_binary(tag) do
702 [_activity, object] in query,
703 where: fragment("(?)->'tag' \\? (?)", object.data, ^tag)
707 defp restrict_tag(query, _), do: query
709 defp restrict_recipients(query, [], _user), do: query
711 defp restrict_recipients(query, recipients, nil) do
712 from(activity in query, where: fragment("? && ?", ^recipients, activity.recipients))
715 defp restrict_recipients(query, recipients, user) do
718 where: fragment("? && ?", ^recipients, activity.recipients),
719 or_where: activity.actor == ^user.ap_id
723 defp restrict_local(query, %{"local_only" => true}) do
724 from(activity in query, where: activity.local == true)
727 defp restrict_local(query, _), do: query
729 defp restrict_actor(query, %{"actor_id" => actor_id}) do
730 from(activity in query, where: activity.actor == ^actor_id)
733 defp restrict_actor(query, _), do: query
735 defp restrict_type(query, %{"type" => type}) when is_binary(type) do
736 from(activity in query, where: fragment("?->>'type' = ?", activity.data, ^type))
739 defp restrict_type(query, %{"type" => type}) do
740 from(activity in query, where: fragment("?->>'type' = ANY(?)", activity.data, ^type))
743 defp restrict_type(query, _), do: query
745 defp restrict_state(query, %{"state" => state}) do
746 from(activity in query, where: fragment("?->>'state' = ?", activity.data, ^state))
749 defp restrict_state(query, _), do: query
751 defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
753 [_activity, object] in query,
754 where: fragment("(?)->'likes' \\? (?)", object.data, ^ap_id)
758 defp restrict_favorited_by(query, _), do: query
760 defp restrict_media(_query, %{"only_media" => _val, "skip_preload" => true}) do
761 raise "Can't use the child object without preloading!"
764 defp restrict_media(query, %{"only_media" => val}) when val == "true" or val == "1" do
766 [_activity, object] in query,
767 where: fragment("not (?)->'attachment' = (?)", object.data, ^[])
771 defp restrict_media(query, _), do: query
773 defp restrict_replies(query, %{"exclude_replies" => val}) when val == "true" or val == "1" do
776 where: fragment("?->'object'->>'inReplyTo' is null", activity.data)
780 defp restrict_replies(query, _), do: query
782 defp restrict_reblogs(query, %{"exclude_reblogs" => val}) when val == "true" or val == "1" do
783 from(activity in query, where: fragment("?->>'type' != 'Announce'", activity.data))
786 defp restrict_reblogs(query, _), do: query
788 defp restrict_muted(query, %{"with_muted" => val}) when val in [true, "true", "1"], do: query
790 defp restrict_muted(query, %{"muting_user" => %User{info: info}} = opts) do
794 from([activity] in query,
795 where: fragment("not (? = ANY(?))", activity.actor, ^mutes),
796 where: fragment("not (?->'to' \\?| ?)", activity.data, ^mutes)
799 unless opts["skip_preload"] do
800 from([thread_mute: tm] in query, where: is_nil(tm.user_id))
806 defp restrict_muted(query, _), do: query
808 defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
809 blocks = info.blocks || []
810 domain_blocks = info.domain_blocks || []
813 if has_named_binding?(query, :object), do: query, else: Activity.with_joined_object(query)
816 [activity, object: o] in query,
817 where: fragment("not (? = ANY(?))", activity.actor, ^blocks),
818 where: fragment("not (? && ?)", activity.recipients, ^blocks),
821 "not (?->>'type' = 'Announce' and ?->'to' \\?| ?)",
826 where: fragment("not (split_part(?, '/', 3) = ANY(?))", activity.actor, ^domain_blocks),
827 where: fragment("not (split_part(?->>'actor', '/', 3) = ANY(?))", o.data, ^domain_blocks)
831 defp restrict_blocked(query, _), do: query
833 defp restrict_unlisted(query) do
838 "not (coalesce(?->'cc', '{}'::jsonb) \\?| ?)",
840 ^[Pleroma.Constants.as_public()]
845 defp restrict_pinned(query, %{"pinned" => "true", "pinned_activity_ids" => ids}) do
846 from(activity in query, where: activity.id in ^ids)
849 defp restrict_pinned(query, _), do: query
851 defp restrict_muted_reblogs(query, %{"muting_user" => %User{info: info}}) do
852 muted_reblogs = info.muted_reblogs || []
858 "not ( ?->>'type' = 'Announce' and ? = ANY(?))",
866 defp restrict_muted_reblogs(query, _), do: query
868 defp exclude_poll_votes(query, %{"include_poll_votes" => "true"}), do: query
870 defp exclude_poll_votes(query, _) do
871 if has_named_binding?(query, :object) do
872 from([activity, object: o] in query,
873 where: fragment("not(?->>'type' = ?)", o.data, "Answer")
880 defp exclude_id(query, %{"exclude_id" => id}) when is_binary(id) do
881 from(activity in query, where: activity.id != ^id)
884 defp exclude_id(query, _), do: query
886 defp maybe_preload_objects(query, %{"skip_preload" => true}), do: query
888 defp maybe_preload_objects(query, _) do
890 |> Activity.with_preloaded_object()
893 defp maybe_preload_bookmarks(query, %{"skip_preload" => true}), do: query
895 defp maybe_preload_bookmarks(query, opts) do
897 |> Activity.with_preloaded_bookmark(opts["user"])
900 defp maybe_set_thread_muted_field(query, %{"skip_preload" => true}), do: query
902 defp maybe_set_thread_muted_field(query, opts) do
904 |> Activity.with_set_thread_muted_field(opts["muting_user"] || opts["user"])
907 defp maybe_order(query, %{order: :desc}) do
909 |> order_by(desc: :id)
912 defp maybe_order(query, %{order: :asc}) do
914 |> order_by(asc: :id)
917 defp maybe_order(query, _), do: query
919 def fetch_activities_query(recipients, opts \\ %{}) do
921 skip_thread_containment: Config.get([:instance, :skip_thread_containment])
925 |> maybe_preload_objects(opts)
926 |> maybe_preload_bookmarks(opts)
927 |> maybe_set_thread_muted_field(opts)
929 |> restrict_recipients(recipients, opts["user"])
930 |> restrict_tag(opts)
931 |> restrict_tag_reject(opts)
932 |> restrict_tag_all(opts)
933 |> restrict_since(opts)
934 |> restrict_local(opts)
935 |> restrict_actor(opts)
936 |> restrict_type(opts)
937 |> restrict_state(opts)
938 |> restrict_favorited_by(opts)
939 |> restrict_blocked(opts)
940 |> restrict_muted(opts)
941 |> restrict_media(opts)
942 |> restrict_visibility(opts)
943 |> restrict_thread_visibility(opts, config)
944 |> restrict_replies(opts)
945 |> restrict_reblogs(opts)
946 |> restrict_pinned(opts)
947 |> restrict_muted_reblogs(opts)
948 |> Activity.restrict_deactivated_users()
949 |> exclude_poll_votes(opts)
952 def fetch_activities(recipients, opts \\ %{}) do
953 list_memberships = Pleroma.List.memberships(opts["user"])
955 fetch_activities_query(recipients ++ list_memberships, opts)
956 |> Pagination.fetch_paginated(opts)
958 |> maybe_update_cc(list_memberships, opts["user"])
961 defp maybe_update_cc(activities, list_memberships, %User{ap_id: user_ap_id})
962 when is_list(list_memberships) and length(list_memberships) > 0 do
963 Enum.map(activities, fn
964 %{data: %{"bcc" => bcc}} = activity when is_list(bcc) and length(bcc) > 0 ->
965 if Enum.any?(bcc, &(&1 in list_memberships)) do
966 update_in(activity.data["cc"], &[user_ap_id | &1])
976 defp maybe_update_cc(activities, _, _), do: activities
978 def fetch_activities_bounded_query(query, recipients, recipients_with_public) do
979 from(activity in query,
981 fragment("? && ?", activity.recipients, ^recipients) or
982 (fragment("? && ?", activity.recipients, ^recipients_with_public) and
983 ^Pleroma.Constants.as_public() in activity.recipients)
987 def fetch_activities_bounded(recipients, recipients_with_public, opts \\ %{}) do
988 fetch_activities_query([], opts)
989 |> fetch_activities_bounded_query(recipients, recipients_with_public)
990 |> Pagination.fetch_paginated(opts)
994 def upload(file, opts \\ []) do
995 with {:ok, data} <- Upload.store(file, opts) do
998 Map.put(data, "actor", opts[:actor])
1003 Repo.insert(%Object{data: obj_data})
1007 defp object_to_user_data(data) do
1009 data["icon"]["url"] &&
1012 "url" => [%{"href" => data["icon"]["url"]}]
1016 data["image"]["url"] &&
1019 "url" => [%{"href" => data["image"]["url"]}]
1024 |> Map.get("attachment", [])
1025 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
1026 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
1028 locked = data["manuallyApprovesFollowers"] || false
1029 data = Transmogrifier.maybe_fix_user_object(data)
1042 follower_address: data["followers"],
1043 following_address: data["following"],
1044 bio: data["summary"]
1047 # nickname can be nil because of virtual actors
1049 if data["preferredUsername"] do
1053 "#{data["preferredUsername"]}@#{URI.parse(data["id"]).host}"
1056 Map.put(user_data, :nickname, nil)
1062 def fetch_follow_information_for_user(user) do
1063 with {:ok, following_data} <-
1064 Fetcher.fetch_and_contain_remote_object_from_id(user.following_address),
1065 following_count when is_integer(following_count) <- following_data["totalItems"],
1066 {:ok, hide_follows} <- collection_private(following_data),
1067 {:ok, followers_data} <-
1068 Fetcher.fetch_and_contain_remote_object_from_id(user.follower_address),
1069 followers_count when is_integer(followers_count) <- followers_data["totalItems"],
1070 {:ok, hide_followers} <- collection_private(followers_data) do
1073 hide_follows: hide_follows,
1074 follower_count: followers_count,
1075 following_count: following_count,
1076 hide_followers: hide_followers
1087 defp maybe_update_follow_information(data) do
1088 with {:enabled, true} <-
1089 {:enabled, Pleroma.Config.get([:instance, :external_user_synchronization])},
1090 {:ok, info} <- fetch_follow_information_for_user(data) do
1091 info = Map.merge(data.info, info)
1092 Map.put(data, :info, info)
1094 {:enabled, false} ->
1099 "Follower/Following counter update for #{data.ap_id} failed.\n" <> inspect(e)
1106 defp collection_private(data) do
1107 if is_map(data["first"]) and
1108 data["first"]["type"] in ["CollectionPage", "OrderedCollectionPage"] do
1111 with {:ok, %{"type" => type}} when type in ["CollectionPage", "OrderedCollectionPage"] <-
1112 Fetcher.fetch_and_contain_remote_object_from_id(data["first"]) do
1115 {:error, {:ok, %{status: code}}} when code in [401, 403] ->
1127 def user_data_from_user_object(data) do
1128 with {:ok, data} <- MRF.filter(data),
1129 {:ok, data} <- object_to_user_data(data) do
1136 def fetch_and_prepare_user_from_ap_id(ap_id) do
1137 with {:ok, data} <- Fetcher.fetch_and_contain_remote_object_from_id(ap_id),
1138 {:ok, data} <- user_data_from_user_object(data),
1139 data <- maybe_update_follow_information(data) do
1142 e -> Logger.error("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
1146 def make_user_from_ap_id(ap_id) do
1147 if _user = User.get_cached_by_ap_id(ap_id) do
1148 Transmogrifier.upgrade_user_from_ap_id(ap_id)
1150 with {:ok, data} <- fetch_and_prepare_user_from_ap_id(ap_id) do
1151 User.insert_or_update_user(data)
1158 def make_user_from_nickname(nickname) do
1159 with {:ok, %{"ap_id" => ap_id}} when not is_nil(ap_id) <- WebFinger.finger(nickname) do
1160 make_user_from_ap_id(ap_id)
1162 _e -> {:error, "No AP id in WebFinger"}
1166 # filter out broken threads
1167 def contain_broken_threads(%Activity{} = activity, %User{} = user) do
1168 entire_thread_visible_for_user?(activity, user)
1171 # do post-processing on a specific activity
1172 def contain_activity(%Activity{} = activity, %User{} = user) do
1173 contain_broken_threads(activity, user)
1176 def fetch_direct_messages_query do
1178 |> restrict_type(%{"type" => "Create"})
1179 |> restrict_visibility(%{visibility: "direct"})
1180 |> order_by([activity], asc: activity.id)