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(activity) do
193 public = "https://www.w3.org/ns/activitystreams#Public"
195 if activity.data["type"] in ["Create", "Announce", "Delete"] do
196 object = Object.normalize(activity)
197 # Do not stream out poll replies
198 unless object.data["type"] == "Answer" do
199 Pleroma.Web.Streamer.stream("user", activity)
200 Pleroma.Web.Streamer.stream("list", activity)
202 if Enum.member?(activity.data["to"], public) do
203 Pleroma.Web.Streamer.stream("public", activity)
206 Pleroma.Web.Streamer.stream("public:local", activity)
209 if activity.data["type"] in ["Create"] do
211 |> Map.get("tag", [])
212 |> Enum.filter(fn tag -> is_bitstring(tag) end)
213 |> Enum.each(fn tag -> Pleroma.Web.Streamer.stream("hashtag:" <> tag, activity) end)
215 if object.data["attachment"] != [] do
216 Pleroma.Web.Streamer.stream("public:media", activity)
219 Pleroma.Web.Streamer.stream("public:local:media", activity)
224 # TODO: Write test, replace with visibility test
225 if !Enum.member?(activity.data["cc"] || [], public) &&
228 User.get_cached_by_ap_id(activity.data["actor"]).follower_address
230 do: Pleroma.Web.Streamer.stream("direct", activity)
236 def create(%{to: to, actor: actor, context: context, object: object} = params, fake \\ false) do
237 additional = params[:additional] || %{}
238 # only accept false as false value
239 local = !(params[:local] == false)
240 published = params[:published]
244 %{to: to, actor: actor, published: published, context: context, object: object},
247 {:ok, activity} <- insert(create_data, local, fake),
248 {:fake, false, activity} <- {:fake, fake, activity},
249 _ <- increase_replies_count_if_reply(create_data),
250 _ <- increase_poll_votes_if_vote(create_data),
251 # Changing note count prior to enqueuing federation task in order to avoid
252 # race conditions on updating user.info
253 {:ok, _actor} <- increase_note_count_if_public(actor, activity),
254 :ok <- maybe_federate(activity) do
257 {:fake, true, activity} ->
262 def accept(%{to: to, actor: actor, object: object} = params) do
263 # only accept false as false value
264 local = !(params[:local] == false)
266 with data <- %{"to" => to, "type" => "Accept", "actor" => actor.ap_id, "object" => object},
267 {:ok, activity} <- insert(data, local),
268 :ok <- maybe_federate(activity) do
273 def reject(%{to: to, actor: actor, object: object} = params) do
274 # only accept false as false value
275 local = !(params[:local] == false)
277 with data <- %{"to" => to, "type" => "Reject", "actor" => actor.ap_id, "object" => object},
278 {:ok, activity} <- insert(data, local),
279 :ok <- maybe_federate(activity) do
284 def update(%{to: to, cc: cc, actor: actor, object: object} = params) do
285 # only accept false as false value
286 local = !(params[:local] == false)
295 {:ok, activity} <- insert(data, local),
296 :ok <- maybe_federate(activity) do
301 # TODO: This is weird, maybe we shouldn't check here if we can make the activity.
303 %User{ap_id: ap_id} = user,
304 %Object{data: %{"id" => _}} = object,
308 with nil <- get_existing_like(ap_id, object),
309 like_data <- make_like_data(user, object, activity_id),
310 {:ok, activity} <- insert(like_data, local),
311 {:ok, object} <- add_like_to_object(activity, object),
312 :ok <- maybe_federate(activity) do
313 {:ok, activity, object}
315 %Activity{} = activity -> {:ok, activity, object}
316 error -> {:error, error}
326 with %Activity{} = like_activity <- get_existing_like(actor.ap_id, object),
327 unlike_data <- make_unlike_data(actor, like_activity, activity_id),
328 {:ok, unlike_activity} <- insert(unlike_data, local),
329 {:ok, _activity} <- Repo.delete(like_activity),
330 {:ok, object} <- remove_like_from_object(like_activity, object),
331 :ok <- maybe_federate(unlike_activity) do
332 {:ok, unlike_activity, like_activity, object}
339 %User{ap_id: _} = user,
340 %Object{data: %{"id" => _}} = object,
345 with true <- is_public?(object),
346 announce_data <- make_announce_data(user, object, activity_id, public),
347 {:ok, activity} <- insert(announce_data, local),
348 {:ok, object} <- add_announce_to_object(activity, object),
349 :ok <- maybe_federate(activity) do
350 {:ok, activity, object}
352 error -> {:error, error}
362 with %Activity{} = announce_activity <- get_existing_announce(actor.ap_id, object),
363 unannounce_data <- make_unannounce_data(actor, announce_activity, activity_id),
364 {:ok, unannounce_activity} <- insert(unannounce_data, local),
365 :ok <- maybe_federate(unannounce_activity),
366 {:ok, _activity} <- Repo.delete(announce_activity),
367 {:ok, object} <- remove_announce_from_object(announce_activity, object) do
368 {:ok, unannounce_activity, object}
374 def follow(follower, followed, activity_id \\ nil, local \\ true) do
375 with data <- make_follow_data(follower, followed, activity_id),
376 {:ok, activity} <- insert(data, local),
377 :ok <- maybe_federate(activity) do
382 def unfollow(follower, followed, activity_id \\ nil, local \\ true) do
383 with %Activity{} = follow_activity <- fetch_latest_follow(follower, followed),
384 {:ok, follow_activity} <- update_follow_state(follow_activity, "cancelled"),
385 unfollow_data <- make_unfollow_data(follower, followed, follow_activity, activity_id),
386 {:ok, activity} <- insert(unfollow_data, local),
387 :ok <- maybe_federate(activity) do
392 def delete(%Object{data: %{"id" => id, "actor" => actor}} = object, local \\ true) do
393 user = User.get_cached_by_ap_id(actor)
394 to = (object.data["to"] || []) ++ (object.data["cc"] || [])
396 with {:ok, object, activity} <- Object.delete(object),
402 "deleted_activity_id" => activity && activity.id
404 {:ok, activity} <- insert(data, local),
405 _ <- decrease_replies_count_if_reply(object),
406 # Changing note count prior to enqueuing federation task in order to avoid
407 # race conditions on updating user.info
408 {:ok, _actor} <- decrease_note_count_if_public(user, object),
409 :ok <- maybe_federate(activity) do
414 def block(blocker, blocked, activity_id \\ nil, local \\ true) do
415 outgoing_blocks = Config.get([:activitypub, :outgoing_blocks])
416 unfollow_blocked = Config.get([:activitypub, :unfollow_blocked])
418 if unfollow_blocked do
419 follow_activity = fetch_latest_follow(blocker, blocked)
420 if follow_activity, do: unfollow(blocker, blocked, nil, local)
423 with true <- outgoing_blocks,
424 block_data <- make_block_data(blocker, blocked, activity_id),
425 {:ok, activity} <- insert(block_data, local),
426 :ok <- maybe_federate(activity) do
433 def unblock(blocker, blocked, activity_id \\ nil, local \\ true) do
434 with %Activity{} = block_activity <- fetch_latest_block(blocker, blocked),
435 unblock_data <- make_unblock_data(blocker, blocked, block_activity, activity_id),
436 {:ok, activity} <- insert(unblock_data, local),
437 :ok <- maybe_federate(activity) do
451 # only accept false as false value
452 local = !(params[:local] == false)
453 forward = !(params[:forward] == false)
455 additional = params[:additional] || %{}
467 Map.merge(additional, %{"to" => [], "cc" => [account.ap_id]})
469 Map.merge(additional, %{"to" => [], "cc" => []})
472 with flag_data <- make_flag_data(params, additional),
473 {:ok, activity} <- insert(flag_data, local),
474 :ok <- maybe_federate(activity) do
475 Enum.each(User.all_superusers(), fn superuser ->
477 |> Pleroma.Emails.AdminEmail.report(actor, account, statuses, content)
478 |> Pleroma.Emails.Mailer.deliver_async()
485 defp fetch_activities_for_context_query(context, opts) do
486 public = ["https://www.w3.org/ns/activitystreams#Public"]
489 if opts["user"], do: [opts["user"].ap_id | opts["user"].following] ++ public, else: public
491 from(activity in Activity)
492 |> maybe_preload_objects(opts)
493 |> restrict_blocked(opts)
494 |> restrict_recipients(recipients, opts["user"])
498 "?->>'type' = ? and ?->>'context' = ?",
505 |> exclude_poll_votes(opts)
506 |> order_by([activity], desc: activity.id)
509 @spec fetch_activities_for_context(String.t(), keyword() | map()) :: [Activity.t()]
510 def fetch_activities_for_context(context, opts \\ %{}) do
512 |> fetch_activities_for_context_query(opts)
516 @spec fetch_latest_activity_id_for_context(String.t(), keyword() | map()) ::
517 Pleroma.FlakeId.t() | nil
518 def fetch_latest_activity_id_for_context(context, opts \\ %{}) do
520 |> fetch_activities_for_context_query(Map.merge(%{"skip_preload" => true}, opts))
526 def fetch_public_activities(opts \\ %{}) do
527 q = fetch_activities_query(["https://www.w3.org/ns/activitystreams#Public"], opts)
530 |> restrict_unlisted()
531 |> Pagination.fetch_paginated(opts)
535 @valid_visibilities ~w[direct unlisted public private]
537 defp restrict_visibility(query, %{visibility: visibility})
538 when is_list(visibility) do
539 if Enum.all?(visibility, &(&1 in @valid_visibilities)) do
545 "activity_visibility(?, ?, ?) = ANY (?)",
555 Logger.error("Could not restrict visibility to #{visibility}")
559 defp restrict_visibility(query, %{visibility: visibility})
560 when visibility in @valid_visibilities do
564 fragment("activity_visibility(?, ?, ?) = ?", a.actor, a.recipients, a.data, ^visibility)
568 defp restrict_visibility(_query, %{visibility: visibility})
569 when visibility not in @valid_visibilities do
570 Logger.error("Could not restrict visibility to #{visibility}")
573 defp restrict_visibility(query, _visibility), do: query
575 defp restrict_thread_visibility(query, _, %{skip_thread_containment: true} = _),
578 defp restrict_thread_visibility(
580 %{"user" => %User{info: %{skip_thread_containment: true}}},
585 defp restrict_thread_visibility(query, %{"user" => %User{ap_id: ap_id}}, _) do
588 where: fragment("thread_visibility(?, (?)->>'id') = true", ^ap_id, a.data)
592 defp restrict_thread_visibility(query, _, _), do: query
594 def fetch_user_activities(user, reading_user, params \\ %{}) do
597 |> Map.put("type", ["Create", "Announce"])
598 |> Map.put("actor_id", user.ap_id)
599 |> Map.put("whole_db", true)
600 |> Map.put("pinned_activity_ids", user.info.pinned_activities)
604 ["https://www.w3.org/ns/activitystreams#Public"] ++
605 [reading_user.ap_id | reading_user.following]
607 ["https://www.w3.org/ns/activitystreams#Public"]
610 fetch_activities(recipients, params)
614 defp restrict_since(query, %{"since_id" => ""}), do: query
616 defp restrict_since(query, %{"since_id" => since_id}) do
617 from(activity in query, where: activity.id > ^since_id)
620 defp restrict_since(query, _), do: query
622 defp restrict_tag_reject(_query, %{"tag_reject" => _tag_reject, "skip_preload" => true}) do
623 raise "Can't use the child object without preloading!"
626 defp restrict_tag_reject(query, %{"tag_reject" => tag_reject})
627 when is_list(tag_reject) and tag_reject != [] do
629 [_activity, object] in query,
630 where: fragment("not (?)->'tag' \\?| (?)", object.data, ^tag_reject)
634 defp restrict_tag_reject(query, _), do: query
636 defp restrict_tag_all(_query, %{"tag_all" => _tag_all, "skip_preload" => true}) do
637 raise "Can't use the child object without preloading!"
640 defp restrict_tag_all(query, %{"tag_all" => tag_all})
641 when is_list(tag_all) and tag_all != [] do
643 [_activity, object] in query,
644 where: fragment("(?)->'tag' \\?& (?)", object.data, ^tag_all)
648 defp restrict_tag_all(query, _), do: query
650 defp restrict_tag(_query, %{"tag" => _tag, "skip_preload" => true}) do
651 raise "Can't use the child object without preloading!"
654 defp restrict_tag(query, %{"tag" => tag}) when is_list(tag) do
656 [_activity, object] in query,
657 where: fragment("(?)->'tag' \\?| (?)", object.data, ^tag)
661 defp restrict_tag(query, %{"tag" => tag}) when is_binary(tag) do
663 [_activity, object] in query,
664 where: fragment("(?)->'tag' \\? (?)", object.data, ^tag)
668 defp restrict_tag(query, _), do: query
670 defp restrict_recipients(query, [], _user), do: query
672 defp restrict_recipients(query, recipients, nil) do
673 from(activity in query, where: fragment("? && ?", ^recipients, activity.recipients))
676 defp restrict_recipients(query, recipients, user) do
679 where: fragment("? && ?", ^recipients, activity.recipients),
680 or_where: activity.actor == ^user.ap_id
684 defp restrict_local(query, %{"local_only" => true}) do
685 from(activity in query, where: activity.local == true)
688 defp restrict_local(query, _), do: query
690 defp restrict_actor(query, %{"actor_id" => actor_id}) do
691 from(activity in query, where: activity.actor == ^actor_id)
694 defp restrict_actor(query, _), do: query
696 defp restrict_type(query, %{"type" => type}) when is_binary(type) do
697 from(activity in query, where: fragment("?->>'type' = ?", activity.data, ^type))
700 defp restrict_type(query, %{"type" => type}) do
701 from(activity in query, where: fragment("?->>'type' = ANY(?)", activity.data, ^type))
704 defp restrict_type(query, _), do: query
706 defp restrict_state(query, %{"state" => state}) do
707 from(activity in query, where: fragment("?->>'state' = ?", activity.data, ^state))
710 defp restrict_state(query, _), do: query
712 defp restrict_favorited_by(query, %{"favorited_by" => ap_id}) do
715 where: fragment(~s(? <@ (? #> '{"object","likes"}'\)), ^ap_id, activity.data)
719 defp restrict_favorited_by(query, _), do: query
721 defp restrict_media(_query, %{"only_media" => _val, "skip_preload" => true}) do
722 raise "Can't use the child object without preloading!"
725 defp restrict_media(query, %{"only_media" => val}) when val == "true" or val == "1" do
727 [_activity, object] in query,
728 where: fragment("not (?)->'attachment' = (?)", object.data, ^[])
732 defp restrict_media(query, _), do: query
734 defp restrict_replies(query, %{"exclude_replies" => val}) when val == "true" or val == "1" do
737 where: fragment("?->'object'->>'inReplyTo' is null", activity.data)
741 defp restrict_replies(query, _), do: query
743 defp restrict_reblogs(query, %{"exclude_reblogs" => val}) when val == "true" or val == "1" do
744 from(activity in query, where: fragment("?->>'type' != 'Announce'", activity.data))
747 defp restrict_reblogs(query, _), do: query
749 defp restrict_muted(query, %{"with_muted" => val}) when val in [true, "true", "1"], do: query
751 defp restrict_muted(query, %{"muting_user" => %User{info: info}}) do
756 where: fragment("not (? = ANY(?))", activity.actor, ^mutes),
757 where: fragment("not (?->'to' \\?| ?)", activity.data, ^mutes)
761 defp restrict_muted(query, _), do: query
763 defp restrict_blocked(query, %{"blocking_user" => %User{info: info}}) do
764 blocks = info.blocks || []
765 domain_blocks = info.domain_blocks || []
768 if has_named_binding?(query, :object), do: query, else: Activity.with_joined_object(query)
771 [activity, object: o] in query,
772 where: fragment("not (? = ANY(?))", activity.actor, ^blocks),
773 where: fragment("not (? && ?)", activity.recipients, ^blocks),
776 "not (?->>'type' = 'Announce' and ?->'to' \\?| ?)",
781 where: fragment("not (split_part(?, '/', 3) = ANY(?))", activity.actor, ^domain_blocks),
782 where: fragment("not (split_part(?->>'actor', '/', 3) = ANY(?))", o.data, ^domain_blocks)
786 defp restrict_blocked(query, _), do: query
788 defp restrict_unlisted(query) do
793 "not (coalesce(?->'cc', '{}'::jsonb) \\?| ?)",
795 ^["https://www.w3.org/ns/activitystreams#Public"]
800 defp restrict_pinned(query, %{"pinned" => "true", "pinned_activity_ids" => ids}) do
801 from(activity in query, where: activity.id in ^ids)
804 defp restrict_pinned(query, _), do: query
806 defp restrict_muted_reblogs(query, %{"muting_user" => %User{info: info}}) do
807 muted_reblogs = info.muted_reblogs || []
813 "not ( ?->>'type' = 'Announce' and ? = ANY(?))",
821 defp restrict_muted_reblogs(query, _), do: query
823 defp exclude_poll_votes(query, %{"include_poll_votes" => "true"}), do: query
825 defp exclude_poll_votes(query, _) do
826 if has_named_binding?(query, :object) do
827 from([activity, object: o] in query,
828 where: fragment("not(?->>'type' = ?)", o.data, "Answer")
835 defp maybe_preload_objects(query, %{"skip_preload" => true}), do: query
837 defp maybe_preload_objects(query, _) do
839 |> Activity.with_preloaded_object()
842 defp maybe_preload_bookmarks(query, %{"skip_preload" => true}), do: query
844 defp maybe_preload_bookmarks(query, opts) do
846 |> Activity.with_preloaded_bookmark(opts["user"])
849 defp maybe_set_thread_muted_field(query, %{"skip_preload" => true}), do: query
851 defp maybe_set_thread_muted_field(query, opts) do
853 |> Activity.with_set_thread_muted_field(opts["user"])
856 defp maybe_order(query, %{order: :desc}) do
858 |> order_by(desc: :id)
861 defp maybe_order(query, %{order: :asc}) do
863 |> order_by(asc: :id)
866 defp maybe_order(query, _), do: query
868 def fetch_activities_query(recipients, opts \\ %{}) do
869 base_query = from(activity in Activity)
872 skip_thread_containment: Config.get([:instance, :skip_thread_containment])
876 |> maybe_preload_objects(opts)
877 |> maybe_preload_bookmarks(opts)
878 |> maybe_set_thread_muted_field(opts)
880 |> restrict_recipients(recipients, opts["user"])
881 |> restrict_tag(opts)
882 |> restrict_tag_reject(opts)
883 |> restrict_tag_all(opts)
884 |> restrict_since(opts)
885 |> restrict_local(opts)
886 |> restrict_actor(opts)
887 |> restrict_type(opts)
888 |> restrict_state(opts)
889 |> restrict_favorited_by(opts)
890 |> restrict_blocked(opts)
891 |> restrict_muted(opts)
892 |> restrict_media(opts)
893 |> restrict_visibility(opts)
894 |> restrict_thread_visibility(opts, config)
895 |> restrict_replies(opts)
896 |> restrict_reblogs(opts)
897 |> restrict_pinned(opts)
898 |> restrict_muted_reblogs(opts)
899 |> Activity.restrict_deactivated_users()
900 |> exclude_poll_votes(opts)
903 def fetch_activities(recipients, opts \\ %{}) do
904 fetch_activities_query(recipients, opts)
905 |> Pagination.fetch_paginated(opts)
909 def fetch_activities_bounded_query(query, recipients, recipients_with_public) do
910 from(activity in query,
912 fragment("? && ?", activity.recipients, ^recipients) or
913 (fragment("? && ?", activity.recipients, ^recipients_with_public) and
914 "https://www.w3.org/ns/activitystreams#Public" in activity.recipients)
918 def fetch_activities_bounded(recipients, recipients_with_public, opts \\ %{}) do
919 fetch_activities_query([], opts)
920 |> fetch_activities_bounded_query(recipients, recipients_with_public)
921 |> Pagination.fetch_paginated(opts)
925 def upload(file, opts \\ []) do
926 with {:ok, data} <- Upload.store(file, opts) do
929 Map.put(data, "actor", opts[:actor])
934 Repo.insert(%Object{data: obj_data})
938 defp object_to_user_data(data) do
940 data["icon"]["url"] &&
943 "url" => [%{"href" => data["icon"]["url"]}]
947 data["image"]["url"] &&
950 "url" => [%{"href" => data["image"]["url"]}]
953 locked = data["manuallyApprovesFollowers"] || false
954 data = Transmogrifier.maybe_fix_user_object(data)
959 "ap_enabled" => true,
960 "source_data" => data,
966 follower_address: data["followers"],
970 # nickname can be nil because of virtual actors
972 if data["preferredUsername"] do
976 "#{data["preferredUsername"]}@#{URI.parse(data["id"]).host}"
979 Map.put(user_data, :nickname, nil)
985 def user_data_from_user_object(data) do
986 with {:ok, data} <- MRF.filter(data),
987 {:ok, data} <- object_to_user_data(data) do
994 def fetch_and_prepare_user_from_ap_id(ap_id) do
995 with {:ok, data} <- Fetcher.fetch_and_contain_remote_object_from_id(ap_id),
996 {:ok, data} <- user_data_from_user_object(data) do
999 e -> Logger.error("Could not decode user at fetch #{ap_id}, #{inspect(e)}")
1003 def make_user_from_ap_id(ap_id) do
1004 if _user = User.get_cached_by_ap_id(ap_id) do
1005 Transmogrifier.upgrade_user_from_ap_id(ap_id)
1007 with {:ok, data} <- fetch_and_prepare_user_from_ap_id(ap_id) do
1008 User.insert_or_update_user(data)
1015 def make_user_from_nickname(nickname) do
1016 with {:ok, %{"ap_id" => ap_id}} when not is_nil(ap_id) <- WebFinger.finger(nickname) do
1017 make_user_from_ap_id(ap_id)
1019 _e -> {:error, "No AP id in WebFinger"}
1023 # filter out broken threads
1024 def contain_broken_threads(%Activity{} = activity, %User{} = user) do
1025 entire_thread_visible_for_user?(activity, user)
1028 # do post-processing on a specific activity
1029 def contain_activity(%Activity{} = activity, %User{} = user) do
1030 contain_broken_threads(activity, user)
1033 def fetch_direct_messages_query do
1035 |> restrict_type(%{"type" => "Create"})
1036 |> restrict_visibility(%{visibility: "direct"})
1037 |> order_by([activity], asc: activity.id)