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
31 actor = User.get_cached_by_ap_id(data["actor"])
35 |> Enum.filter(fn recipient ->
36 case User.get_cached_by_ap_id(recipient) do
41 User.following?(user, actor)
48 defp get_recipients(%{"type" => "Create"} = data) do
51 actor = data["actor"] || []
52 recipients = (to ++ cc ++ [actor]) |> Enum.uniq()
56 defp get_recipients(data) do
63 defp check_actor_is_active(actor) do
64 if not is_nil(actor) do
65 with user <- User.get_cached_by_ap_id(actor),
66 false <- user.info.deactivated do
76 defp check_remote_limit(%{"object" => %{"content" => content}}) when not is_nil(content) do
77 limit = Config.get([:instance, :remote_limit])
78 String.length(content) <= limit
81 defp check_remote_limit(_), do: true
83 def increase_note_count_if_public(actor, object) do
84 if is_public?(object), do: User.increase_note_count(actor), else: {:ok, actor}
87 def decrease_note_count_if_public(actor, object) do
88 if is_public?(object), do: User.decrease_note_count(actor), else: {:ok, actor}
91 def increase_replies_count_if_reply(%{
92 "object" => %{"inReplyTo" => reply_ap_id} = object,
95 if is_public?(object) do
96 Object.increase_replies_count(reply_ap_id)
100 def increase_replies_count_if_reply(_create_data), do: :noop
102 def decrease_replies_count_if_reply(%Object{
103 data: %{"inReplyTo" => reply_ap_id} = object
105 if is_public?(object) do
106 Object.decrease_replies_count(reply_ap_id)
110 def decrease_replies_count_if_reply(_object), do: :noop
112 def increase_poll_votes_if_vote(%{
113 "object" => %{"inReplyTo" => reply_ap_id, "name" => name},
116 Object.increase_vote_count(reply_ap_id, name)
119 def increase_poll_votes_if_vote(_create_data), do: :noop
121 def insert(map, local \\ true, fake \\ false) when is_map(map) do
122 with nil <- Activity.normalize(map),
123 map <- lazy_put_activity_defaults(map, fake),
124 :ok <- check_actor_is_active(map["actor"]),
125 {_, true} <- {:remote_limit_error, check_remote_limit(map)},
126 {:ok, map} <- MRF.filter(map),
127 {recipients, _, _} = get_recipients(map),
128 {:fake, false, map, recipients} <- {:fake, fake, map, recipients},
129 {:ok, map, object} <- insert_full_object(map) do
131 Repo.insert(%Activity{
135 recipients: recipients
138 # Splice in the child object if we have one.
140 if !is_nil(object) do
141 Map.put(activity, :object, object)
146 PleromaJobQueue.enqueue(:background, Pleroma.Web.RichMedia.Helpers, [:fetch, activity])
148 Notification.create_notifications(activity)
152 |> Conversation.create_or_bump_for()
153 |> get_participations()
156 stream_out_participations(participations)
159 %Activity{} = activity ->
162 {:fake, true, map, recipients} ->
163 activity = %Activity{
167 recipients: recipients,
171 Pleroma.Web.RichMedia.Helpers.fetch_data_for_activity(activity)
179 defp get_participations({:ok, %{participations: participations}}), do: participations
180 defp get_participations(_), do: []
182 def stream_out_participations(participations) do
185 |> Repo.preload(:user)
187 Enum.each(participations, fn participation ->
188 Pleroma.Web.Streamer.stream("participation", participation)
192 def stream_out_participations(%Object{data: %{"context" => context}}, user) do
193 with %Conversation{} = conversation <- Conversation.get_for_ap_id(context),
194 conversation = Repo.preload(conversation, :participations),
196 fetch_latest_activity_id_for_context(conversation.ap_id, %{
198 "blocking_user" => user
200 if last_activity_id do
201 stream_out_participations(conversation.participations)
206 def stream_out_participations(_, _), do: :noop
208 def stream_out(activity) do
209 public = "https://www.w3.org/ns/activitystreams#Public"
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 Enum.member?(activity.data["to"], 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 # TODO: Write test, replace with visibility test
241 if !Enum.member?(activity.data["cc"] || [], public) &&
244 User.get_cached_by_ap_id(activity.data["actor"]).follower_address
246 do: Pleroma.Web.Streamer.stream("direct", activity)
252 def create(%{to: to, actor: actor, context: context, object: object} = params, fake \\ false) do
253 additional = params[:additional] || %{}
254 # only accept false as false value
255 local = !(params[:local] == false)
256 published = params[:published]
260 %{to: to, actor: actor, published: published, context: context, object: object},
263 {:ok, activity} <- insert(create_data, local, fake),
264 {:fake, false, activity} <- {:fake, fake, activity},
265 _ <- increase_replies_count_if_reply(create_data),
266 _ <- increase_poll_votes_if_vote(create_data),
267 # Changing note count prior to enqueuing federation task in order to avoid
268 # race conditions on updating user.info
269 {:ok, _actor} <- increase_note_count_if_public(actor, activity),
270 :ok <- maybe_federate(activity) do
273 {:fake, true, activity} ->
278 def accept(%{to: to, actor: actor, object: object} = params) do
279 # only accept false as false value
280 local = !(params[:local] == false)
282 with data <- %{"to" => to, "type" => "Accept", "actor" => actor.ap_id, "object" => object},
283 {:ok, activity} <- insert(data, local),
284 :ok <- maybe_federate(activity) do
289 def reject(%{to: to, actor: actor, object: object} = params) do
290 # only accept false as false value
291 local = !(params[:local] == false)
293 with data <- %{"to" => to, "type" => "Reject", "actor" => actor.ap_id, "object" => object},
294 {:ok, activity} <- insert(data, local),
295 :ok <- maybe_federate(activity) do
300 def update(%{to: to, cc: cc, actor: actor, object: object} = params) do
301 # only accept false as false value
302 local = !(params[:local] == false)
311 {:ok, activity} <- insert(data, local),
312 :ok <- maybe_federate(activity) do
317 # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
319 %User{ap_id: ap_id} = user,
320 %Object{data: %{"id" => _}} = object,
324 with nil <- get_existing_like(ap_id, object),
325 like_data <- make_like_data(user, object, activity_id),
326 {:ok, activity} <- insert(like_data, local),
327 {:ok, object} <- add_like_to_object(activity, object),
328 :ok <- maybe_federate(activity) do
329 {:ok, activity, object}
331 %Activity{} = activity -> {:ok, activity, object}
332 error -> {:error, error}
342 with %Activity{} = like_activity <- get_existing_like(actor.ap_id, object),
343 unlike_data <- make_unlike_data(actor, like_activity, activity_id),
344 {:ok, unlike_activity} <- insert(unlike_data, local),
345 {:ok, _activity} <- Repo.delete(like_activity),
346 {:ok, object} <- remove_like_from_object(like_activity, object),
347 :ok <- maybe_federate(unlike_activity) do
348 {:ok, unlike_activity, like_activity, object}
355 %User{ap_id: _} = user,
356 %Object{data: %{"id" => _}} = object,
361 with true <- is_public?(object),
362 announce_data <- make_announce_data(user, object, activity_id, public),
363 {:ok, activity} <- insert(announce_data, local),
364 {:ok, object} <- add_announce_to_object(activity, object),
365 :ok <- maybe_federate(activity) do
366 {:ok, activity, object}
368 error -> {:error, error}
378 with %Activity{} = announce_activity <- get_existing_announce(actor.ap_id, object),
379 unannounce_data <- make_unannounce_data(actor, announce_activity, activity_id),
380 {:ok, unannounce_activity} <- insert(unannounce_data, local),
381 :ok <- maybe_federate(unannounce_activity),
382 {:ok, _activity} <- Repo.delete(announce_activity),
383 {:ok, object} <- remove_announce_from_object(announce_activity, object) do
384 {:ok, unannounce_activity, object}
390 def follow(follower, followed, activity_id \\ nil, local \\ true) do
391 with data <- make_follow_data(follower, followed, activity_id),
392 {:ok, activity} <- insert(data, local),
393 :ok <- maybe_federate(activity) do
398 def unfollow(follower, followed, activity_id \\ nil, local \\ true) do
399 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
400 {:ok, follow_activity} <- update_follow_state(follow_activity, "cancelled"),
401 unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
402 {:ok, activity} <- insert(unfollow_data, local),
403 :ok <- maybe_federate(activity) do
408 def delete(%User{ap_id: ap_id, follower_address: follower_address} = user) do
410 "to" => [follower_address],
413 "object" => %{"type" => "Person", "id" => ap_id}
415 {:ok, activity} <- insert(data, true, true),
416 :ok <- maybe_federate(activity) do
421 def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
422 user = User.get_cached_by_ap_id(actor)
423 to = (object.data["to"] || []) ++ (object.data["cc"] || [])
425 with {:ok, object, activity} <- Object.delete(object),
431 "deleted_activity_id" => activity && activity.id
433 {:ok, activity} <- insert(data, local, false),
434 stream_out_participations(object, user),
435 _ <- decrease_replies_count_if_reply(object),
436 # Changing note count prior to enqueuing federation task in order to avoid
437 # race conditions on updating user.info
438 {:ok, _actor} <- decrease_note_count_if_public(user, object),
439 :ok <- maybe_federate(activity) do
444 def block(blocker, blocked, activity_id \\ nil, local \\ true) do
445 outgoing_blocks = Config.get([:activitypub, :outgoing_blocks])
446 unfollow_blocked = Config.get([:activitypub, :unfollow_blocked])
448 if unfollow_blocked do
449 follow_activity = fetch_latest_follow(blocker, blocked)
450 if follow_activity, do: unfollow(blocker, blocked, nil, local)
453 with true <- outgoing_blocks,
454 block_data <- make_block_data(blocker, blocked, activity_id),
455 {:ok, activity} <- insert(block_data, local),
456 :ok <- maybe_federate(activity) do
463 def unblock(blocker, blocked, activity_id \\ nil, local \\ true) do
464 with %Activity{} = block_activity <- fetch_latest_block(blocker, blocked),
465 unblock_data <- make_unblock_data(blocker, blocked, block_activity, activity_id),
466 {:ok, activity} <- insert(unblock_data, local),
467 :ok <- maybe_federate(activity) do
481 # only accept false as false value
482 local = !(params[:local] == false)
483 forward = !(params[:forward] == false)
485 additional = params[:additional] || %{}
497 Map.merge(additional, %{"to" => [], "cc" => [account.ap_id]})
499 Map.merge(additional, %{"to" => [], "cc" => []})
502 with flag_data <- make_flag_data(params, additional),
503 {:ok, activity} <- insert(flag_data, local),
504 :ok <- maybe_federate(activity) do
505 Enum.each(User.all_superusers(), fn superuser ->
507 |> Pleroma.Emails.AdminEmail.report(actor, account, statuses, content)
508 |> Pleroma.Emails.Mailer.deliver_async()
515 defp fetch_activities_for_context_query(context, opts) do
516 public = ["https://www.w3.org/ns/activitystreams#Public"]
519 if opts["user"], do: [opts["user"].ap_id | opts["user"].following] ++ public, else: public
521 from(activity in Activity)
522 |> maybe_preload_objects(opts)
523 |> restrict_blocked(opts)
524 |> restrict_recipients(recipients, opts["user"])
528 "?->>'type' = ? and ?->>'context' = ?",
535 |> exclude_poll_votes(opts)
536 |> order_by([activity], desc: activity.id)
539 @spec fetch_activities_for_context(String.t(), keyword() | map()) :: [Activity.t()]
540 def fetch_activities_for_context(context, opts \\ %{}) do
542 |> fetch_activities_for_context_query(opts)
546 @spec fetch_latest_activity_id_for_context(String.t(), keyword() | map()) ::
547 Pleroma.FlakeId.t() | nil
548 def fetch_latest_activity_id_for_context(context, opts \\ %{}) do
550 |> fetch_activities_for_context_query(Map.merge(%{"skip_preload" => true}, opts))
556 def fetch_public_activities(opts \\ %{}) do
557 q = fetch_activities_query(["https://www.w3.org/ns/activitystreams#Public"], opts)
560 |> restrict_unlisted()
561 |> Pagination.fetch_paginated(opts)
565 @valid_visibilities ~w[direct unlisted public private]
567 defp restrict_visibility(query, %{visibility: visibility})
568 when is_list(visibility) do
569 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
575 "activity_visibility(?, ?, ?) = ANY (?)",
585 Logger.error("Could not restrict visibility to #{visibility}")
589 defp restrict_visibility(query, %{visibility: visibility})
590 when visibility in @valid_visibilities do
594 fragment("activity_visibility(?, ?, ?) = ?", a.actor, a.recipients, a.data, ^visibility)
598 defp restrict_visibility(_query, %{visibility: visibility})
599 when visibility not in @valid_visibilities do
600 Logger.error("Could not restrict visibility to #{visibility}")
603 defp restrict_visibility(query, _visibility), do: query
605 defp restrict_thread_visibility(query, _, %{skip_thread_containment: true} = _),
608 defp restrict_thread_visibility(
610 %{"user" => %User{info: %{skip_thread_containment: true}}},
615 defp restrict_thread_visibility(query, %{"user" => %User{ap_id: ap_id}}, _) do
618 where: fragment("thread_visibility(?, (?)->>'id') = true", ^ap_id, a.data)
622 defp restrict_thread_visibility(query, _, _), do: query
624 def fetch_user_activities(user, reading_user, params \\ %{}) do
627 |> Map.put("type", ["Create", "Announce"])
628 |> Map.put("actor_id", user.ap_id)
629 |> Map.put("whole_db", true)
630 |> Map.put("pinned_activity_ids", user.info.pinned_activities)
634 ["https://www.w3.org/ns/activitystreams#Public"] ++
635 [reading_user.ap_id | reading_user.following]
637 ["https://www.w3.org/ns/activitystreams#Public"]
640 fetch_activities(recipients, params)
644 defp restrict_since(query, %{"since_id" => ""}), do: query
646 defp restrict_since(query, %{"since_id" => since_id}) do
647 from(activity in query, where: activity.id > ^since_id)
650 defp restrict_since(query, _), do: query
652 defp restrict_tag_reject(_query, %{"tag_reject" => _tag_reject, "skip_preload" => true}) do
653 raise "Can't use the child object without preloading!"
656 defp restrict_tag_reject(query, %{"tag_reject" => tag_reject})
657 when is_list(tag_reject) and tag_reject != [] do
659 [_activity, object] in query,
660 where: fragment("not (?)->'tag' \\?| (?)", object.data, ^tag_reject)
664 defp restrict_tag_reject(query, _), do: query
666 defp restrict_tag_all(_query, %{"tag_all" => _tag_all, "skip_preload" => true}) do
667 raise "Can't use the child object without preloading!"
670 defp restrict_tag_all(query, %{"tag_all" => tag_all})
671 when is_list(tag_all) and tag_all != [] do
673 [_activity, object] in query,
674 where: fragment("(?)->'tag' \\?& (?)", object.data, ^tag_all)
678 defp restrict_tag_all(query, _), do: query
680 defp restrict_tag(_query, %{"tag" => _tag, "skip_preload" => true}) do
681 raise "Can't use the child object without preloading!"
684 defp restrict_tag(query, %{"tag" => tag}) when is_list(tag) do
686 [_activity, object] in query,
687 where: fragment("(?)->'tag' \\?| (?)", object.data, ^tag)
691 defp restrict_tag(query, %{"tag" => tag}) when is_binary(tag) do
693 [_activity, object] in query,
694 where: fragment("(?)->'tag' \\? (?)", object.data, ^tag)
698 defp restrict_tag(query, _), do: query
700 defp restrict_recipients(query, [], _user), do: query
702 defp restrict_recipients(query, recipients, nil) do
703 from(activity in query, where: fragment("? && ?", ^recipients, activity.recipients))
706 defp restrict_recipients(query, recipients, user) do
709 where: fragment("? && ?", ^recipients, activity.recipients),
710 or_where: activity.actor == ^user.ap_id
714 defp restrict_local(query, %{"local_only" => true}) do
715 from(activity in query, where: activity.local == true)
718 defp restrict_local(query, _), do: query
720 defp restrict_actor(query, %{"actor_id" => actor_id}) do
721 from(activity in query, where: activity.actor == ^actor_id)
724 defp restrict_actor(query, _), do: query
726 defp restrict_type(query, %{"type" => type}) when is_binary(type) do
727 from(activity in query, where: fragment("?->>'type' = ?", activity.data, ^type))
730 defp restrict_type(query, %{"type" => type}) do
731 from(activity in query, where: fragment("?->>'type' = ANY(?)", activity.data, ^type))
734 defp restrict_type(query, _), do: query
736 defp restrict_state(query, %{"state" => state}) do
737 from(activity in query, where: fragment("?->>'state' = ?", activity.data, ^state))
740 defp restrict_state(query, _), do: query
742 defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
745 where: fragment(~s(? <@ (? #> '{"object","likes"}'\)), ^ap_id, activity.data)
749 defp restrict_favorited_by(query, _), do: query
751 defp restrict_media(_query, %{"only_media" => _val, "skip_preload" => true}) do
752 raise "Can't use the child object without preloading!"
755 defp restrict_media(query, %{"only_media" => val}) when val == "true" or val == "1" do
757 [_activity, object] in query,
758 where: fragment("not (?)->'attachment' = (?)", object.data, ^[])
762 defp restrict_media(query, _), do: query
764 defp restrict_replies(query, %{"exclude_replies" => val}) when val == "true" or val == "1" do
767 where: fragment("?->'object'->>'inReplyTo' is null", activity.data)
771 defp restrict_replies(query, _), do: query
773 defp restrict_reblogs(query, %{"exclude_reblogs" => val}) when val == "true" or val == "1" do
774 from(activity in query, where: fragment("?->>'type' != 'Announce'", activity.data))
777 defp restrict_reblogs(query, _), do: query
779 defp restrict_muted(query, %{"with_muted" => val}) when val in [true, "true", "1"], do: query
781 defp restrict_muted(query, %{"muting_user" => %User{info: info}}) do
786 where: fragment("not (? = ANY(?))", activity.actor, ^mutes),
787 where: fragment("not (?->'to' \\?| ?)", activity.data, ^mutes)
791 defp restrict_muted(query, _), do: query
793 defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
794 blocks = info.blocks || []
795 domain_blocks = info.domain_blocks || []
798 if has_named_binding?(query, :object), do: query, else: Activity.with_joined_object(query)
801 [activity, object: o] in query,
802 where: fragment("not (? = ANY(?))", activity.actor, ^blocks),
803 where: fragment("not (? && ?)", activity.recipients, ^blocks),
806 "not (?->>'type' = 'Announce' and ?->'to' \\?| ?)",
811 where: fragment("not (split_part(?, '/', 3) = ANY(?))", activity.actor, ^domain_blocks),
812 where: fragment("not (split_part(?->>'actor', '/', 3) = ANY(?))", o.data, ^domain_blocks)
816 defp restrict_blocked(query, _), do: query
818 defp restrict_unlisted(query) do
823 "not (coalesce(?->'cc', '{}'::jsonb) \\?| ?)",
825 ^["https://www.w3.org/ns/activitystreams#Public"]
830 defp restrict_pinned(query, %{"pinned" => "true", "pinned_activity_ids" => ids}) do
831 from(activity in query, where: activity.id in ^ids)
834 defp restrict_pinned(query, _), do: query
836 defp restrict_muted_reblogs(query, %{"muting_user" => %User{info: info}}) do
837 muted_reblogs = info.muted_reblogs || []
843 "not ( ?->>'type' = 'Announce' and ? = ANY(?))",
851 defp restrict_muted_reblogs(query, _), do: query
853 defp exclude_poll_votes(query, %{"include_poll_votes" => "true"}), do: query
855 defp exclude_poll_votes(query, _) do
856 if has_named_binding?(query, :object) do
857 from([activity, object: o] in query,
858 where: fragment("not(?->>'type' = ?)", o.data, "Answer")
865 defp maybe_preload_objects(query, %{"skip_preload" => true}), do: query
867 defp maybe_preload_objects(query, _) do
869 |> Activity.with_preloaded_object()
872 defp maybe_preload_bookmarks(query, %{"skip_preload" => true}), do: query
874 defp maybe_preload_bookmarks(query, opts) do
876 |> Activity.with_preloaded_bookmark(opts["user"])
879 defp maybe_set_thread_muted_field(query, %{"skip_preload" => true}), do: query
881 defp maybe_set_thread_muted_field(query, opts) do
883 |> Activity.with_set_thread_muted_field(opts["user"])
886 defp maybe_order(query, %{order: :desc}) do
888 |> order_by(desc: :id)
891 defp maybe_order(query, %{order: :asc}) do
893 |> order_by(asc: :id)
896 defp maybe_order(query, _), do: query
898 def fetch_activities_query(recipients, opts \\ %{}) do
899 base_query = from(activity in Activity)
902 skip_thread_containment: Config.get([:instance, :skip_thread_containment])
906 |> maybe_preload_objects(opts)
907 |> maybe_preload_bookmarks(opts)
908 |> maybe_set_thread_muted_field(opts)
910 |> restrict_recipients(recipients, opts["user"])
911 |> restrict_tag(opts)
912 |> restrict_tag_reject(opts)
913 |> restrict_tag_all(opts)
914 |> restrict_since(opts)
915 |> restrict_local(opts)
916 |> restrict_actor(opts)
917 |> restrict_type(opts)
918 |> restrict_state(opts)
919 |> restrict_favorited_by(opts)
920 |> restrict_blocked(opts)
921 |> restrict_muted(opts)
922 |> restrict_media(opts)
923 |> restrict_visibility(opts)
924 |> restrict_thread_visibility(opts, config)
925 |> restrict_replies(opts)
926 |> restrict_reblogs(opts)
927 |> restrict_pinned(opts)
928 |> restrict_muted_reblogs(opts)
929 |> Activity.restrict_deactivated_users()
930 |> exclude_poll_votes(opts)
933 def fetch_activities(recipients, opts \\ %{}) do
934 fetch_activities_query(recipients, opts)
935 |> Pagination.fetch_paginated(opts)
939 def fetch_activities_bounded_query(query, recipients, recipients_with_public) do
940 from(activity in query,
942 fragment("? && ?", activity.recipients, ^recipients) or
943 (fragment("? && ?", activity.recipients, ^recipients_with_public) and
944 "https://www.w3.org/ns/activitystreams#Public" in activity.recipients)
948 def fetch_activities_bounded(recipients, recipients_with_public, opts \\ %{}) do
949 fetch_activities_query([], opts)
950 |> fetch_activities_bounded_query(recipients, recipients_with_public)
951 |> Pagination.fetch_paginated(opts)
955 def upload(file, opts \\ []) do
956 with {:ok, data} <- Upload.store(file, opts) do
959 Map.put(data, "actor", opts[:actor])
964 Repo.insert(%Object{data: obj_data})
968 defp object_to_user_data(data) do
970 data["icon"]["url"] &&
973 "url" => [%{"href" => data["icon"]["url"]}]
977 data["image"]["url"] &&
980 "url" => [%{"href" => data["image"]["url"]}]
983 locked = data["manuallyApprovesFollowers"] || false
984 data = Transmogrifier.maybe_fix_user_object(data)
989 "ap_enabled" => true,
990 "source_data" => data,
996 follower_address: data["followers"],
997 following_address: data["following"],
1001 # nickname can be nil because of virtual actors
1003 if data["preferredUsername"] do
1007 "#{data["preferredUsername"]}@#{URI.parse(data["id"]).host}"
1010 Map.put(user_data, :nickname, nil)
1016 def user_data_from_user_object(data) do
1017 with {:ok, data} <- MRF.filter(data),
1018 {:ok, data} <- object_to_user_data(data) do
1025 def fetch_and_prepare_user_from_ap_id(ap_id) do
1026 with {:ok, data} <- Fetcher.fetch_and_contain_remote_object_from_id(ap_id),
1027 {:ok, data} <- user_data_from_user_object(data) do
1030 e -> Logger.error("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
1034 def make_user_from_ap_id(ap_id) do
1035 if _user = User.get_cached_by_ap_id(ap_id) do
1036 Transmogrifier.upgrade_user_from_ap_id(ap_id)
1038 with {:ok, data} <- fetch_and_prepare_user_from_ap_id(ap_id) do
1039 User.insert_or_update_user(data)
1046 def make_user_from_nickname(nickname) do
1047 with {:ok, %{"ap_id" => ap_id}} when not is_nil(ap_id) <- WebFinger.finger(nickname) do
1048 make_user_from_ap_id(ap_id)
1050 _e -> {:error, "No AP id in WebFinger"}
1054 # filter out broken threads
1055 def contain_broken_threads(%Activity{} = activity, %User{} = user) do
1056 entire_thread_visible_for_user?(activity, user)
1059 # do post-processing on a specific activity
1060 def contain_activity(%Activity{} = activity, %User{} = user) do
1061 contain_broken_threads(activity, user)
1064 def fetch_direct_messages_query do
1066 |> restrict_type(%{"type" => "Create"})
1067 |> restrict_visibility(%{visibility: "direct"})
1068 |> order_by([activity], asc: activity.id)