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.Fetcher
12 alias Pleroma.Pagination
16 alias Pleroma.Web.ActivityPub.MRF
17 alias Pleroma.Web.ActivityPub.Transmogrifier
18 alias Pleroma.Web.WebFinger
21 import Pleroma.Web.ActivityPub.Utils
22 import Pleroma.Web.ActivityPub.Visibility
26 # For Announce activities, we filter the recipients based on following status for any actors
27 # that match actual users. See issue #164 for more information about why this is necessary.
28 defp get_recipients(%{"type" => "Announce"} = data) do
29 to = Map.get(data, "to", [])
30 cc = Map.get(data, "cc", [])
31 bcc = Map.get(data, "bcc", [])
32 actor = User.get_cached_by_ap_id(data["actor"])
35 Enum.filter(Enum.concat([to, cc, bcc]), fn recipient ->
36 case User.get_cached_by_ap_id(recipient) do
38 user -> User.following?(user, actor)
45 defp get_recipients(%{"type" => "Create"} = data) do
46 to = Map.get(data, "to", [])
47 cc = Map.get(data, "cc", [])
48 bcc = Map.get(data, "bcc", [])
49 actor = Map.get(data, "actor", [])
50 recipients = [to, cc, bcc, [actor]] |> Enum.concat() |> Enum.uniq()
54 defp get_recipients(data) do
55 to = Map.get(data, "to", [])
56 cc = Map.get(data, "cc", [])
57 bcc = Map.get(data, "bcc", [])
58 recipients = Enum.concat([to, cc, bcc])
62 defp check_actor_is_active(actor) do
63 if not is_nil(actor) do
64 with user <- User.get_cached_by_ap_id(actor),
65 false <- user.info.deactivated do
75 defp check_remote_limit(%{"object" => %{"content" => content}}) when not is_nil(content) do
76 limit = Config.get([:instance, :remote_limit])
77 String.length(content) <= limit
80 defp check_remote_limit(_), do: true
82 def increase_note_count_if_public(actor, object) do
83 if is_public?(object), do: User.increase_note_count(actor), else: {:ok, actor}
86 def decrease_note_count_if_public(actor, object) do
87 if is_public?(object), do: User.decrease_note_count(actor), else: {:ok, actor}
90 def increase_replies_count_if_reply(%{
91 "object" => %{"inReplyTo" => reply_ap_id} = object,
94 if is_public?(object) do
95 Object.increase_replies_count(reply_ap_id)
99 def increase_replies_count_if_reply(_create_data), do: :noop
101 def decrease_replies_count_if_reply(%Object{
102 data: %{"inReplyTo" => reply_ap_id} = object
104 if is_public?(object) do
105 Object.decrease_replies_count(reply_ap_id)
109 def decrease_replies_count_if_reply(_object), do: :noop
111 def increase_poll_votes_if_vote(%{
112 "object" => %{"inReplyTo" => reply_ap_id, "name" => name},
115 Object.increase_vote_count(reply_ap_id, name)
118 def increase_poll_votes_if_vote(_create_data), do: :noop
120 def insert(map, local \\ true, fake \\ false) when is_map(map) do
121 with nil <- Activity.normalize(map),
122 map <- lazy_put_activity_defaults(map, fake),
123 :ok <- check_actor_is_active(map["actor"]),
124 {_, true} <- {:remote_limit_error, check_remote_limit(map)},
125 {:ok, map} <- MRF.filter(map),
126 {recipients, _, _} = get_recipients(map),
127 {:fake, false, map, recipients} <- {:fake, fake, map, recipients},
128 {:ok, map, object} <- insert_full_object(map) do
130 Repo.insert(%Activity{
134 recipients: recipients
137 # Splice in the child object if we have one.
139 if !is_nil(object) do
140 Map.put(activity, :object, object)
145 PleromaJobQueue.enqueue(:background, Pleroma.Web.RichMedia.Helpers, [:fetch, activity])
147 Notification.create_notifications(activity)
151 |> Conversation.create_or_bump_for()
152 |> get_participations()
155 stream_out_participations(participations)
158 %Activity{} = activity ->
161 {:fake, true, map, recipients} ->
162 activity = %Activity{
166 recipients: recipients,
170 Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
178 defp get_participations({:ok, %{participations: participations}}), do: participations
179 defp get_participations(_), do: []
181 def stream_out_participations(participations) do
184 |> Repo.preload(:user)
186 Enum.each(participations, fn participation ->
187 Pleroma.Web.Streamer.stream("participation", participation)
191 def stream_out_participations(%Object{data: %{"context" => context}}, user) do
192 with %Conversation{} = conversation <- Conversation.get_for_ap_id(context),
193 conversation = Repo.preload(conversation, :participations),
195 fetch_latest_activity_id_for_context(conversation.ap_id, %{
197 "blocking_user" => user
199 if last_activity_id do
200 stream_out_participations(conversation.participations)
205 def stream_out_participations(_, _), do: :noop
207 def stream_out(activity) do
208 public = "https://www.w3.org/ns/activitystreams#Public"
210 if activity.data["type"] in ["Create", "Announce", "Delete"] do
211 object = Object.normalize(activity)
212 # Do not stream out poll replies
213 unless object.data["type"] == "Answer" do
214 Pleroma.Web.Streamer.stream("user", activity)
215 Pleroma.Web.Streamer.stream("list", activity)
217 if Enum.member?(activity.data["to"], public) do
218 Pleroma.Web.Streamer.stream("public", activity)
221 Pleroma.Web.Streamer.stream("public:local", activity)
224 if activity.data["type"] in ["Create"] do
226 |> Map.get("tag", [])
227 |> Enum.filter(fn tag -> is_bitstring(tag) end)
228 |> Enum.each(fn tag -> Pleroma.Web.Streamer.stream("hashtag:" <> tag, activity) end)
230 if object.data["attachment"] != [] do
231 Pleroma.Web.Streamer.stream("public:media", activity)
234 Pleroma.Web.Streamer.stream("public:local:media", activity)
239 # TODO: Write test, replace with visibility test
240 if !Enum.member?(activity.data["cc"] || [], public) &&
243 User.get_cached_by_ap_id(activity.data["actor"]).follower_address
245 do: Pleroma.Web.Streamer.stream("direct", activity)
251 def create(%{to: to, actor: actor, context: context, object: object} = params, fake \\ false) do
252 additional = params[:additional] || %{}
253 # only accept false as false value
254 local = !(params[:local] == false)
255 published = params[:published]
259 %{to: to, actor: actor, published: published, context: context, object: object},
262 {:ok, activity} <- insert(create_data, local, fake),
263 {:fake, false, activity} <- {:fake, fake, activity},
264 _ <- increase_replies_count_if_reply(create_data),
265 _ <- increase_poll_votes_if_vote(create_data),
266 # Changing note count prior to enqueuing federation task in order to avoid
267 # race conditions on updating user.info
268 {:ok, _actor} <- increase_note_count_if_public(actor, activity),
269 :ok <- maybe_federate(activity) do
272 {: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}
341 with %Activity{} = like_activity <- get_existing_like(actor.ap_id, object),
342 unlike_data <- make_unlike_data(actor, like_activity, activity_id),
343 {:ok, unlike_activity} <- insert(unlike_data, local),
344 {:ok, _activity} <- Repo.delete(like_activity),
345 {:ok, object} <- remove_like_from_object(like_activity, object),
346 :ok <- maybe_federate(unlike_activity) do
347 {:ok, unlike_activity, like_activity, object}
354 %User{ap_id: _} = user,
355 %Object{data: %{"id" => _}} = object,
360 with true <- is_public?(object),
361 announce_data <- make_announce_data(user, object, activity_id, public),
362 {:ok, activity} <- insert(announce_data, local),
363 {:ok, object} <- add_announce_to_object(activity, object),
364 :ok <- maybe_federate(activity) do
365 {:ok, activity, object}
367 error -> {:error, error}
377 with %Activity{} = announce_activity <- get_existing_announce(actor.ap_id, object),
378 unannounce_data <- make_unannounce_data(actor, announce_activity, activity_id),
379 {:ok, unannounce_activity} <- insert(unannounce_data, local),
380 :ok <- maybe_federate(unannounce_activity),
381 {:ok, _activity} <- Repo.delete(announce_activity),
382 {:ok, object} <- remove_announce_from_object(announce_activity, object) do
383 {:ok, unannounce_activity, object}
389 def follow(follower, followed, activity_id \\ nil, local \\ true) do
390 with data <- make_follow_data(follower, followed, activity_id),
391 {:ok, activity} <- insert(data, local),
392 :ok <- maybe_federate(activity) do
397 def unfollow(follower, followed, activity_id \\ nil, local \\ true) do
398 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
399 {:ok, follow_activity} <- update_follow_state(follow_activity, "cancelled"),
400 unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
401 {:ok, activity} <- insert(unfollow_data, local),
402 :ok <- maybe_federate(activity) do
407 def delete(%User{ap_id: ap_id, follower_address: follower_address} = user) do
409 "to" => [follower_address],
412 "object" => %{"type" => "Person", "id" => ap_id}
414 {:ok, activity} <- insert(data, true, true),
415 :ok <- maybe_federate(activity) do
420 def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
421 user = User.get_cached_by_ap_id(actor)
422 to = (object.data["to"] || []) ++ (object.data["cc"] || [])
424 with {:ok, object, activity} <- Object.delete(object),
430 "deleted_activity_id" => activity && activity.id
432 {:ok, activity} <- insert(data, local, false),
433 stream_out_participations(object, user),
434 _ <- decrease_replies_count_if_reply(object),
435 # Changing note count prior to enqueuing federation task in order to avoid
436 # race conditions on updating user.info
437 {:ok, _actor} <- decrease_note_count_if_public(user, object),
438 :ok <- maybe_federate(activity) do
443 def block(blocker, blocked, activity_id \\ nil, local \\ true) do
444 outgoing_blocks = Config.get([:activitypub, :outgoing_blocks])
445 unfollow_blocked = Config.get([:activitypub, :unfollow_blocked])
447 if unfollow_blocked do
448 follow_activity = fetch_latest_follow(blocker, blocked)
449 if follow_activity, do: unfollow(blocker, blocked, nil, local)
452 with true <- outgoing_blocks,
453 block_data <- make_block_data(blocker, blocked, activity_id),
454 {:ok, activity} <- insert(block_data, local),
455 :ok <- maybe_federate(activity) do
462 def unblock(blocker, blocked, activity_id \\ nil, local \\ true) do
463 with %Activity{} = block_activity <- fetch_latest_block(blocker, blocked),
464 unblock_data <- make_unblock_data(blocker, blocked, block_activity, activity_id),
465 {:ok, activity} <- insert(unblock_data, local),
466 :ok <- maybe_federate(activity) do
480 # only accept false as false value
481 local = !(params[:local] == false)
482 forward = !(params[:forward] == false)
484 additional = params[:additional] || %{}
496 Map.merge(additional, %{"to" => [], "cc" => [account.ap_id]})
498 Map.merge(additional, %{"to" => [], "cc" => []})
501 with flag_data <- make_flag_data(params, additional),
502 {:ok, activity} <- insert(flag_data, local),
503 :ok <- maybe_federate(activity) do
504 Enum.each(User.all_superusers(), fn superuser ->
506 |> Pleroma.Emails.AdminEmail.report(actor, account, statuses, content)
507 |> Pleroma.Emails.Mailer.deliver_async()
514 defp fetch_activities_for_context_query(context, opts) do
515 public = ["https://www.w3.org/ns/activitystreams#Public"]
518 if opts["user"], do: [opts["user"].ap_id | opts["user"].following] ++ public, else: public
520 from(activity in Activity)
521 |> maybe_preload_objects(opts)
522 |> restrict_blocked(opts)
523 |> restrict_recipients(recipients, opts["user"])
527 "?->>'type' = ? and ?->>'context' = ?",
534 |> exclude_poll_votes(opts)
535 |> order_by([activity], desc: activity.id)
538 @spec fetch_activities_for_context(String.t(), keyword() | map()) :: [Activity.t()]
539 def fetch_activities_for_context(context, opts \\ %{}) do
541 |> fetch_activities_for_context_query(opts)
545 @spec fetch_latest_activity_id_for_context(String.t(), keyword() | map()) ::
546 Pleroma.FlakeId.t() | nil
547 def fetch_latest_activity_id_for_context(context, opts \\ %{}) do
549 |> fetch_activities_for_context_query(Map.merge(%{"skip_preload" => true}, opts))
555 def fetch_public_activities(opts \\ %{}) do
556 q = fetch_activities_query(["https://www.w3.org/ns/activitystreams#Public"], opts)
559 |> restrict_unlisted()
560 |> Pagination.fetch_paginated(opts)
564 @valid_visibilities ~w[direct unlisted public private]
566 defp restrict_visibility(query, %{visibility: visibility})
567 when is_list(visibility) do
568 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
574 "activity_visibility(?, ?, ?) = ANY (?)",
584 Logger.error("Could not restrict visibility to #{visibility}")
588 defp restrict_visibility(query, %{visibility: visibility})
589 when visibility in @valid_visibilities do
593 fragment("activity_visibility(?, ?, ?) = ?", a.actor, a.recipients, a.data, ^visibility)
597 defp restrict_visibility(_query, %{visibility: visibility})
598 when visibility not in @valid_visibilities do
599 Logger.error("Could not restrict visibility to #{visibility}")
602 defp restrict_visibility(query, _visibility), do: query
604 defp restrict_thread_visibility(query, _, %{skip_thread_containment: true} = _),
607 defp restrict_thread_visibility(
609 %{"user" => %User{info: %{skip_thread_containment: true}}},
614 defp restrict_thread_visibility(query, %{"user" => %User{ap_id: ap_id}}, _) do
617 where: fragment("thread_visibility(?, (?)->>'id') = true", ^ap_id, a.data)
621 defp restrict_thread_visibility(query, _, _), do: query
623 def fetch_user_activities(user, reading_user, params \\ %{}) do
626 |> Map.put("type", ["Create", "Announce"])
627 |> Map.put("actor_id", user.ap_id)
628 |> Map.put("whole_db", true)
629 |> Map.put("pinned_activity_ids", user.info.pinned_activities)
633 ["https://www.w3.org/ns/activitystreams#Public"] ++
634 [reading_user.ap_id | reading_user.following]
636 ["https://www.w3.org/ns/activitystreams#Public"]
639 fetch_activities(recipients, params)
643 defp restrict_since(query, %{"since_id" => ""}), do: query
645 defp restrict_since(query, %{"since_id" => since_id}) do
646 from(activity in query, where: activity.id > ^since_id)
649 defp restrict_since(query, _), do: query
651 defp restrict_tag_reject(_query, %{"tag_reject" => _tag_reject, "skip_preload" => true}) do
652 raise "Can't use the child object without preloading!"
655 defp restrict_tag_reject(query, %{"tag_reject" => tag_reject})
656 when is_list(tag_reject) and tag_reject != [] do
658 [_activity, object] in query,
659 where: fragment("not (?)->'tag' \\?| (?)", object.data, ^tag_reject)
663 defp restrict_tag_reject(query, _), do: query
665 defp restrict_tag_all(_query, %{"tag_all" => _tag_all, "skip_preload" => true}) do
666 raise "Can't use the child object without preloading!"
669 defp restrict_tag_all(query, %{"tag_all" => tag_all})
670 when is_list(tag_all) and tag_all != [] do
672 [_activity, object] in query,
673 where: fragment("(?)->'tag' \\?& (?)", object.data, ^tag_all)
677 defp restrict_tag_all(query, _), do: query
679 defp restrict_tag(_query, %{"tag" => _tag, "skip_preload" => true}) do
680 raise "Can't use the child object without preloading!"
683 defp restrict_tag(query, %{"tag" => tag}) when is_list(tag) do
685 [_activity, object] in query,
686 where: fragment("(?)->'tag' \\?| (?)", object.data, ^tag)
690 defp restrict_tag(query, %{"tag" => tag}) when is_binary(tag) do
692 [_activity, object] in query,
693 where: fragment("(?)->'tag' \\? (?)", object.data, ^tag)
697 defp restrict_tag(query, _), do: query
699 defp restrict_recipients(query, [], _user), do: query
701 defp restrict_recipients(query, recipients, nil) do
702 from(activity in query, where: fragment("? && ?", ^recipients, activity.recipients))
705 defp restrict_recipients(query, recipients, user) do
708 where: fragment("? && ?", ^recipients, activity.recipients),
709 or_where: activity.actor == ^user.ap_id
713 defp restrict_local(query, %{"local_only" => true}) do
714 from(activity in query, where: activity.local == true)
717 defp restrict_local(query, _), do: query
719 defp restrict_actor(query, %{"actor_id" => actor_id}) do
720 from(activity in query, where: activity.actor == ^actor_id)
723 defp restrict_actor(query, _), do: query
725 defp restrict_type(query, %{"type" => type}) when is_binary(type) do
726 from(activity in query, where: fragment("?->>'type' = ?", activity.data, ^type))
729 defp restrict_type(query, %{"type" => type}) do
730 from(activity in query, where: fragment("?->>'type' = ANY(?)", activity.data, ^type))
733 defp restrict_type(query, _), do: query
735 defp restrict_state(query, %{"state" => state}) do
736 from(activity in query, where: fragment("?->>'state' = ?", activity.data, ^state))
739 defp restrict_state(query, _), do: query
741 defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
744 where: fragment(~s(? <@ (? #> '{"object","likes"}'\)), ^ap_id, activity.data)
748 defp restrict_favorited_by(query, _), do: query
750 defp restrict_media(_query, %{"only_media" => _val, "skip_preload" => true}) do
751 raise "Can't use the child object without preloading!"
754 defp restrict_media(query, %{"only_media" => val}) when val == "true" or val == "1" do
756 [_activity, object] in query,
757 where: fragment("not (?)->'attachment' = (?)", object.data, ^[])
761 defp restrict_media(query, _), do: query
763 defp restrict_replies(query, %{"exclude_replies" => val}) when val == "true" or val == "1" do
766 where: fragment("?->'object'->>'inReplyTo' is null", activity.data)
770 defp restrict_replies(query, _), do: query
772 defp restrict_reblogs(query, %{"exclude_reblogs" => val}) when val == "true" or val == "1" do
773 from(activity in query, where: fragment("?->>'type' != 'Announce'", activity.data))
776 defp restrict_reblogs(query, _), do: query
778 defp restrict_muted(query, %{"with_muted" => val}) when val in [true, "true", "1"], do: query
780 defp restrict_muted(query, %{"muting_user" => %User{info: info}}) do
785 where: fragment("not (? = ANY(?))", activity.actor, ^mutes),
786 where: fragment("not (?->'to' \\?| ?)", activity.data, ^mutes)
790 defp restrict_muted(query, _), do: query
792 defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
793 blocks = info.blocks || []
794 domain_blocks = info.domain_blocks || []
797 if has_named_binding?(query, :object), do: query, else: Activity.with_joined_object(query)
800 [activity, object: o] in query,
801 where: fragment("not (? = ANY(?))", activity.actor, ^blocks),
802 where: fragment("not (? && ?)", activity.recipients, ^blocks),
805 "not (?->>'type' = 'Announce' and ?->'to' \\?| ?)",
810 where: fragment("not (split_part(?, '/', 3) = ANY(?))", activity.actor, ^domain_blocks),
811 where: fragment("not (split_part(?->>'actor', '/', 3) = ANY(?))", o.data, ^domain_blocks)
815 defp restrict_blocked(query, _), do: query
817 defp restrict_unlisted(query) do
822 "not (coalesce(?->'cc', '{}'::jsonb) \\?| ?)",
824 ^["https://www.w3.org/ns/activitystreams#Public"]
829 defp restrict_pinned(query, %{"pinned" => "true", "pinned_activity_ids" => ids}) do
830 from(activity in query, where: activity.id in ^ids)
833 defp restrict_pinned(query, _), do: query
835 defp restrict_muted_reblogs(query, %{"muting_user" => %User{info: info}}) do
836 muted_reblogs = info.muted_reblogs || []
842 "not ( ?->>'type' = 'Announce' and ? = ANY(?))",
850 defp restrict_muted_reblogs(query, _), do: query
852 defp exclude_poll_votes(query, %{"include_poll_votes" => "true"}), do: query
854 defp exclude_poll_votes(query, _) do
855 if has_named_binding?(query, :object) do
856 from([activity, object: o] in query,
857 where: fragment("not(?->>'type' = ?)", o.data, "Answer")
864 defp maybe_preload_objects(query, %{"skip_preload" => true}), do: query
866 defp maybe_preload_objects(query, _) do
868 |> Activity.with_preloaded_object()
871 defp maybe_preload_bookmarks(query, %{"skip_preload" => true}), do: query
873 defp maybe_preload_bookmarks(query, opts) do
875 |> Activity.with_preloaded_bookmark(opts["user"])
878 defp maybe_set_thread_muted_field(query, %{"skip_preload" => true}), do: query
880 defp maybe_set_thread_muted_field(query, opts) do
882 |> Activity.with_set_thread_muted_field(opts["user"])
885 defp maybe_order(query, %{order: :desc}) do
887 |> order_by(desc: :id)
890 defp maybe_order(query, %{order: :asc}) do
892 |> order_by(asc: :id)
895 defp maybe_order(query, _), do: query
897 def fetch_activities_query(recipients, opts \\ %{}) do
899 skip_thread_containment: Config.get([:instance, :skip_thread_containment])
903 |> maybe_preload_objects(opts)
904 |> maybe_preload_bookmarks(opts)
905 |> maybe_set_thread_muted_field(opts)
907 |> restrict_recipients(recipients, opts["user"])
908 |> restrict_tag(opts)
909 |> restrict_tag_reject(opts)
910 |> restrict_tag_all(opts)
911 |> restrict_since(opts)
912 |> restrict_local(opts)
913 |> restrict_actor(opts)
914 |> restrict_type(opts)
915 |> restrict_state(opts)
916 |> restrict_favorited_by(opts)
917 |> restrict_blocked(opts)
918 |> restrict_muted(opts)
919 |> restrict_media(opts)
920 |> restrict_visibility(opts)
921 |> restrict_thread_visibility(opts, config)
922 |> restrict_replies(opts)
923 |> restrict_reblogs(opts)
924 |> restrict_pinned(opts)
925 |> restrict_muted_reblogs(opts)
926 |> Activity.restrict_deactivated_users()
927 |> exclude_poll_votes(opts)
930 def fetch_activities(recipients, opts \\ %{}) do
931 list_memberships = Pleroma.List.memberships(opts["user"])
933 fetch_activities_query(recipients ++ list_memberships, opts)
934 |> Pagination.fetch_paginated(opts)
936 |> maybe_update_cc(list_memberships, opts["user"])
939 defp maybe_update_cc(activities, list_memberships, %User{ap_id: user_ap_id})
940 when is_list(list_memberships) and length(list_memberships) > 0 do
941 Enum.map(activities, fn
942 %{data: %{"bcc" => bcc}} = activity when is_list(bcc) and length(bcc) > 0 ->
943 if Enum.any?(bcc, &(&1 in list_memberships)) do
944 update_in(activity.data["cc"], &[user_ap_id | &1])
954 defp maybe_update_cc(activities, _, _), do: activities
956 def fetch_activities_bounded_query(query, recipients, recipients_with_public) do
957 from(activity in query,
959 fragment("? && ?", activity.recipients, ^recipients) or
960 (fragment("? && ?", activity.recipients, ^recipients_with_public) and
961 "https://www.w3.org/ns/activitystreams#Public" in activity.recipients)
965 def fetch_activities_bounded(recipients, recipients_with_public, opts \\ %{}) do
966 fetch_activities_query([], opts)
967 |> fetch_activities_bounded_query(recipients, recipients_with_public)
968 |> Pagination.fetch_paginated(opts)
972 def upload(file, opts \\ []) do
973 with {:ok, data} <- Upload.store(file, opts) do
976 Map.put(data, "actor", opts[:actor])
981 Repo.insert(%Object{data: obj_data})
985 defp object_to_user_data(data) do
987 data["icon"]["url"] &&
990 "url" => [%{"href" => data["icon"]["url"]}]
994 data["image"]["url"] &&
997 "url" => [%{"href" => data["image"]["url"]}]
1000 locked = data["manuallyApprovesFollowers"] || false
1001 data = Transmogrifier.maybe_fix_user_object(data)
1006 "ap_enabled" => true,
1007 "source_data" => data,
1013 follower_address: data["followers"],
1014 bio: data["summary"]
1017 # nickname can be nil because of virtual actors
1019 if data["preferredUsername"] do
1023 "#{data["preferredUsername"]}@#{URI.parse(data["id"]).host}"
1026 Map.put(user_data, :nickname, nil)
1032 def user_data_from_user_object(data) do
1033 with {:ok, data} <- MRF.filter(data),
1034 {:ok, data} <- object_to_user_data(data) do
1041 def fetch_and_prepare_user_from_ap_id(ap_id) do
1042 with {:ok, data} <- Fetcher.fetch_and_contain_remote_object_from_id(ap_id),
1043 {:ok, data} <- user_data_from_user_object(data) do
1046 e -> Logger.error("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
1050 def make_user_from_ap_id(ap_id) do
1051 if _user = User.get_cached_by_ap_id(ap_id) do
1052 Transmogrifier.upgrade_user_from_ap_id(ap_id)
1054 with {:ok, data} <- fetch_and_prepare_user_from_ap_id(ap_id) do
1055 User.insert_or_update_user(data)
1062 def make_user_from_nickname(nickname) do
1063 with {:ok, %{"ap_id" => ap_id}} when not is_nil(ap_id) <- WebFinger.finger(nickname) do
1064 make_user_from_ap_id(ap_id)
1066 _e -> {:error, "No AP id in WebFinger"}
1070 # filter out broken threads
1071 def contain_broken_threads(%Activity{} = activity, %User{} = user) do
1072 entire_thread_visible_for_user?(activity, user)
1075 # do post-processing on a specific activity
1076 def contain_activity(%Activity{} = activity, %User{} = user) do
1077 contain_broken_threads(activity, user)
1080 def fetch_direct_messages_query do
1082 |> restrict_type(%{"type" => "Create"})
1083 |> restrict_visibility(%{visibility: "direct"})
1084 |> order_by([activity], asc: activity.id)