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.Utils do
9 alias Pleroma.Notification
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.ActivityPub.Visibility
16 alias Pleroma.Web.AdminAPI.AccountView
17 alias Pleroma.Web.Endpoint
18 alias Pleroma.Web.Router.Helpers
23 require Pleroma.Constants
25 @supported_object_types ["Article", "Note", "Video", "Page", "Question", "Answer", "Audio"]
26 @strip_status_report_states ~w(closed resolved)
27 @supported_report_states ~w(open closed resolved)
28 @valid_visibilities ~w(public unlisted private direct)
30 # Some implementations send the actor URI as the actor field, others send the entire actor object,
31 # so figure out what the actor's URI is based on what we have.
32 def get_ap_id(%{"id" => id} = _), do: id
33 def get_ap_id(id), do: id
35 def normalize_params(params) do
36 Map.put(params, "actor", get_ap_id(params["actor"]))
39 @spec determine_explicit_mentions(map()) :: map()
40 def determine_explicit_mentions(%{"tag" => tag} = _) when is_list(tag) do
42 %{"type" => "Mention", "href" => href} -> [href]
47 def determine_explicit_mentions(%{"tag" => tag} = object) when is_map(tag) do
49 |> Map.put("tag", [tag])
50 |> determine_explicit_mentions()
53 def determine_explicit_mentions(_), do: []
55 @spec label_in_collection?(any(), any()) :: boolean()
56 defp label_in_collection?(ap_id, coll) when is_binary(coll), do: ap_id == coll
57 defp label_in_collection?(ap_id, coll) when is_list(coll), do: ap_id in coll
58 defp label_in_collection?(_, _), do: false
60 @spec label_in_message?(String.t(), map()) :: boolean()
61 def label_in_message?(label, params),
63 [params["to"], params["cc"], params["bto"], params["bcc"]]
64 |> Enum.any?(&label_in_collection?(label, &1))
66 @spec unaddressed_message?(map()) :: boolean()
67 def unaddressed_message?(params),
69 [params["to"], params["cc"], params["bto"], params["bcc"]]
70 |> Enum.all?(&is_nil(&1))
72 @spec recipient_in_message(User.t(), User.t(), map()) :: boolean()
73 def recipient_in_message(%User{ap_id: ap_id} = recipient, %User{} = actor, params),
75 label_in_message?(ap_id, params) || unaddressed_message?(params) ||
76 User.following?(recipient, actor)
78 defp extract_list(target) when is_binary(target), do: [target]
79 defp extract_list(lst) when is_list(lst), do: lst
80 defp extract_list(_), do: []
82 def maybe_splice_recipient(ap_id, params) do
84 !label_in_collection?(ap_id, params["to"]) &&
85 !label_in_collection?(ap_id, params["cc"])
88 cc_list = extract_list(params["cc"])
89 Map.put(params, "cc", [ap_id | cc_list])
95 def make_json_ld_header do
98 "https://www.w3.org/ns/activitystreams",
99 "#{Web.base_url()}/schemas/litepub-0.1.jsonld",
108 DateTime.utc_now() |> DateTime.to_iso8601()
111 def generate_activity_id do
112 generate_id("activities")
115 def generate_context_id do
116 generate_id("contexts")
119 def generate_object_id do
120 Helpers.o_status_url(Endpoint, :object, UUID.generate())
123 def generate_id(type) do
124 "#{Web.base_url()}/#{type}/#{UUID.generate()}"
127 def get_notified_from_object(%{"type" => type} = object) when type in @supported_object_types do
128 fake_create_activity = %{
129 "to" => object["to"],
130 "cc" => object["cc"],
135 get_notified_from_object(fake_create_activity)
138 def get_notified_from_object(object) do
139 Notification.get_notified_from_activity(%Activity{data: object}, false)
142 def create_context(context) do
143 context = context || generate_id("contexts")
145 # Ecto has problems accessing the constraint inside the jsonb,
146 # so we explicitly check for the existed object before insert
147 object = Object.get_cached_by_ap_id(context)
149 with true <- is_nil(object),
150 changeset <- Object.context_mapping(context),
151 {:ok, inserted_object} <- Repo.insert(changeset) do
160 Enqueues an activity for federation if it's local
162 @spec maybe_federate(any()) :: :ok
163 def maybe_federate(%Activity{local: true} = activity) do
164 if Pleroma.Config.get!([:instance, :federating]) do
165 Pleroma.Web.Federator.publish(activity)
171 def maybe_federate(_), do: :ok
174 Adds an id and a published data if they aren't there,
175 also adds it to an included object
177 @spec lazy_put_activity_defaults(map(), boolean) :: map()
178 def lazy_put_activity_defaults(map, fake? \\ false)
180 def lazy_put_activity_defaults(map, true) do
182 |> Map.put_new("id", "pleroma:fakeid")
183 |> Map.put_new_lazy("published", &make_date/0)
184 |> Map.put_new("context", "pleroma:fakecontext")
185 |> Map.put_new("context_id", -1)
186 |> lazy_put_object_defaults(true)
189 def lazy_put_activity_defaults(map, _fake?) do
190 %{data: %{"id" => context}, id: context_id} = create_context(map["context"])
193 |> Map.put_new_lazy("id", &generate_activity_id/0)
194 |> Map.put_new_lazy("published", &make_date/0)
195 |> Map.put_new("context", context)
196 |> Map.put_new("context_id", context_id)
197 |> lazy_put_object_defaults(false)
200 # Adds an id and published date if they aren't there.
202 @spec lazy_put_object_defaults(map(), boolean()) :: map()
203 defp lazy_put_object_defaults(%{"object" => map} = activity, true)
207 |> Map.put_new("id", "pleroma:fake_object_id")
208 |> Map.put_new_lazy("published", &make_date/0)
209 |> Map.put_new("context", activity["context"])
210 |> Map.put_new("context_id", activity["context_id"])
211 |> Map.put_new("fake", true)
213 %{activity | "object" => object}
216 defp lazy_put_object_defaults(%{"object" => map} = activity, _)
220 |> Map.put_new_lazy("id", &generate_object_id/0)
221 |> Map.put_new_lazy("published", &make_date/0)
222 |> Map.put_new("context", activity["context"])
223 |> Map.put_new("context_id", activity["context_id"])
225 %{activity | "object" => object}
228 defp lazy_put_object_defaults(activity, _), do: activity
231 Inserts a full object if it is contained in an activity.
233 def insert_full_object(%{"object" => %{"type" => type} = object_data} = map)
234 when is_map(object_data) and type in @supported_object_types do
235 with {:ok, object} <- Object.create(object_data) do
236 map = Map.put(map, "object", object.data["id"])
242 def insert_full_object(map), do: {:ok, map, nil}
244 #### Like-related helpers
247 Returns an existing like if a user already liked an object
249 @spec get_existing_like(String.t(), map()) :: Activity.t() | nil
250 def get_existing_like(actor, %{data: %{"id" => id}}) do
252 |> Activity.Queries.by_actor()
253 |> Activity.Queries.by_object_id(id)
254 |> Activity.Queries.by_type("Like")
259 @spec make_like_data(User.t(), map(), String.t()) :: map()
261 %User{ap_id: ap_id} = actor,
262 %{data: %{"actor" => object_actor_id, "id" => id}} = object,
265 object_actor = User.get_cached_by_ap_id(object_actor_id)
268 if Visibility.is_public?(object) do
269 [actor.follower_address, object.data["actor"]]
271 [object.data["actor"]]
275 (object.data["to"] ++ (object.data["cc"] || []))
276 |> List.delete(actor.ap_id)
277 |> List.delete(object_actor.follower_address)
285 "context" => object.data["context"]
287 |> maybe_put("id", activity_id)
290 @spec update_element_in_object(String.t(), list(any), Object.t()) ::
291 {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
292 def update_element_in_object(property, element, object) do
296 %{"#{property}_count" => length(element), "#{property}s" => element}
300 |> Changeset.change(data: data)
301 |> Object.update_and_set_cache()
304 @spec add_like_to_object(Activity.t(), Object.t()) ::
305 {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
306 def add_like_to_object(%Activity{data: %{"actor" => actor}}, object) do
307 [actor | fetch_likes(object)]
309 |> update_likes_in_object(object)
312 @spec remove_like_from_object(Activity.t(), Object.t()) ::
313 {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
314 def remove_like_from_object(%Activity{data: %{"actor" => actor}}, object) do
317 |> List.delete(actor)
318 |> update_likes_in_object(object)
321 defp update_likes_in_object(likes, object) do
322 update_element_in_object("like", likes, object)
325 defp fetch_likes(object) do
326 if is_list(object.data["likes"]) do
333 #### Follow-related helpers
336 Updates a follow activity's state (for locked accounts).
338 @spec update_follow_state_for_all(Activity.t(), String.t()) :: {:ok, Activity} | {:error, any()}
339 def update_follow_state_for_all(
340 %Activity{data: %{"actor" => actor, "object" => object}} = activity,
344 |> Activity.Queries.by_type()
345 |> Activity.Queries.by_actor(actor)
346 |> Activity.Queries.by_object_id(object)
347 |> where(fragment("data->>'state' = 'pending'"))
348 |> update(set: [data: fragment("jsonb_set(data, '{state}', ?)", ^state)])
349 |> Repo.update_all([])
351 User.set_follow_state_cache(actor, object, state)
353 activity = Activity.get_by_id(activity.id)
358 def update_follow_state(
359 %Activity{data: %{"actor" => actor, "object" => object}} = activity,
362 new_data = Map.put(activity.data, "state", state)
363 changeset = Changeset.change(activity, data: new_data)
365 with {:ok, activity} <- Repo.update(changeset) do
366 User.set_follow_state_cache(actor, object, state)
372 Makes a follow activity data for the given follower and followed
374 def make_follow_data(
375 %User{ap_id: follower_id},
376 %User{ap_id: followed_id} = _followed,
381 "actor" => follower_id,
382 "to" => [followed_id],
383 "cc" => [Pleroma.Constants.as_public()],
384 "object" => followed_id,
387 |> maybe_put("id", activity_id)
390 def fetch_latest_follow(%User{ap_id: follower_id}, %User{ap_id: followed_id}) do
392 |> Activity.Queries.by_type()
393 |> where(actor: ^follower_id)
394 # this is to use the index
395 |> Activity.Queries.by_object_id(followed_id)
396 |> order_by([activity], fragment("? desc nulls last", activity.id))
401 #### Announce-related helpers
404 Retruns an existing announce activity if the notice has already been announced
406 @spec get_existing_announce(String.t(), map()) :: Activity.t() | nil
407 def get_existing_announce(actor, %{data: %{"id" => ap_id}}) do
409 |> Activity.Queries.by_type()
410 |> where(actor: ^actor)
411 # this is to use the index
412 |> Activity.Queries.by_object_id(ap_id)
417 Make announce activity data for the given actor and object
419 # for relayed messages, we only want to send to subscribers
420 def make_announce_data(
421 %User{ap_id: ap_id} = user,
422 %Object{data: %{"id" => id}} = object,
427 "type" => "Announce",
430 "to" => [user.follower_address],
432 "context" => object.data["context"]
434 |> maybe_put("id", activity_id)
437 def make_announce_data(
438 %User{ap_id: ap_id} = user,
439 %Object{data: %{"id" => id}} = object,
444 "type" => "Announce",
447 "to" => [user.follower_address, object.data["actor"]],
448 "cc" => [Pleroma.Constants.as_public()],
449 "context" => object.data["context"]
451 |> maybe_put("id", activity_id)
455 Make unannounce activity data for the given actor and object
457 def make_unannounce_data(
458 %User{ap_id: ap_id} = user,
459 %Activity{data: %{"context" => context, "object" => object}} = activity,
462 object = Object.normalize(object)
467 "object" => activity.data,
468 "to" => [user.follower_address, object.data["actor"]],
469 "cc" => [Pleroma.Constants.as_public()],
472 |> maybe_put("id", activity_id)
475 def make_unlike_data(
476 %User{ap_id: ap_id} = user,
477 %Activity{data: %{"context" => context, "object" => object}} = activity,
480 object = Object.normalize(object)
485 "object" => activity.data,
486 "to" => [user.follower_address, object.data["actor"]],
487 "cc" => [Pleroma.Constants.as_public()],
490 |> maybe_put("id", activity_id)
493 @spec add_announce_to_object(Activity.t(), Object.t()) ::
494 {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
495 def add_announce_to_object(
496 %Activity{data: %{"actor" => actor}},
499 unless actor |> User.get_cached_by_ap_id() |> User.invisible?() do
500 announcements = take_announcements(object)
502 with announcements <- Enum.uniq([actor | announcements]) do
503 update_element_in_object("announcement", announcements, object)
510 def add_announce_to_object(_, object), do: {:ok, object}
512 @spec remove_announce_from_object(Activity.t(), Object.t()) ::
513 {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
514 def remove_announce_from_object(%Activity{data: %{"actor" => actor}}, object) do
515 with announcements <- List.delete(take_announcements(object), actor) do
516 update_element_in_object("announcement", announcements, object)
520 defp take_announcements(%{data: %{"announcements" => announcements}} = _)
521 when is_list(announcements),
524 defp take_announcements(_), do: []
526 #### Unfollow-related helpers
528 def make_unfollow_data(follower, followed, follow_activity, activity_id) do
531 "actor" => follower.ap_id,
532 "to" => [followed.ap_id],
533 "object" => follow_activity.data
535 |> maybe_put("id", activity_id)
538 #### Block-related helpers
539 @spec fetch_latest_block(User.t(), User.t()) :: Activity.t() | nil
540 def fetch_latest_block(%User{ap_id: blocker_id}, %User{ap_id: blocked_id}) do
542 |> Activity.Queries.by_type()
543 |> where(actor: ^blocker_id)
544 # this is to use the index
545 |> Activity.Queries.by_object_id(blocked_id)
546 |> order_by([activity], fragment("? desc nulls last", activity.id))
551 def make_block_data(blocker, blocked, activity_id) do
554 "actor" => blocker.ap_id,
555 "to" => [blocked.ap_id],
556 "object" => blocked.ap_id
558 |> maybe_put("id", activity_id)
561 def make_unblock_data(blocker, blocked, block_activity, activity_id) do
564 "actor" => blocker.ap_id,
565 "to" => [blocked.ap_id],
566 "object" => block_activity.data
568 |> maybe_put("id", activity_id)
571 #### Create-related helpers
573 def make_create_data(params, additional) do
574 published = params.published || make_date()
578 "to" => params.to |> Enum.uniq(),
579 "actor" => params.actor.ap_id,
580 "object" => params.object,
581 "published" => published,
582 "context" => params.context
584 |> Map.merge(additional)
587 #### Listen-related helpers
588 def make_listen_data(params, additional) do
589 published = params.published || make_date()
593 "to" => params.to |> Enum.uniq(),
594 "actor" => params.actor.ap_id,
595 "object" => params.object,
596 "published" => published,
597 "context" => params.context
599 |> Map.merge(additional)
602 #### Flag-related helpers
603 @spec make_flag_data(map(), map()) :: map()
604 def make_flag_data(%{actor: actor, context: context, content: content} = params, additional) do
607 "actor" => actor.ap_id,
608 "content" => content,
609 "object" => build_flag_object(params),
610 "context" => context,
613 |> Map.merge(additional)
616 def make_flag_data(_, _), do: %{}
618 defp build_flag_object(%{account: account, statuses: statuses} = _) do
619 [account.ap_id] ++ build_flag_object(%{statuses: statuses})
622 defp build_flag_object(%{statuses: statuses}) do
623 Enum.map(statuses || [], &build_flag_object/1)
626 defp build_flag_object(act) when is_map(act) or is_binary(act) do
629 %Activity{} = act -> act.data["id"]
630 act when is_map(act) -> act["id"]
631 act when is_binary(act) -> act
634 activity = Activity.get_by_ap_id_with_object(id)
635 actor = User.get_by_ap_id(activity.object.data["actor"])
639 "id" => activity.data["id"],
640 "content" => activity.object.data["content"],
641 "published" => activity.object.data["published"],
642 "actor" => AccountView.render("show.json", %{user: actor})
646 defp build_flag_object(_), do: []
649 Fetches the OrderedCollection/OrderedCollectionPage from `from`, limiting the amount of pages fetched after
650 the first one to `pages_left` pages.
651 If the amount of pages is higher than the collection has, it returns whatever was there.
653 def fetch_ordered_collection(from, pages_left, acc \\ []) do
654 with {:ok, response} <- Tesla.get(from),
655 {:ok, collection} <- Jason.decode(response.body) do
656 case collection["type"] do
657 "OrderedCollection" ->
658 # If we've encountered the OrderedCollection and not the page,
659 # just call the same function on the page address
660 fetch_ordered_collection(collection["first"], pages_left)
662 "OrderedCollectionPage" ->
664 # There are still more pages
665 if Map.has_key?(collection, "next") do
666 # There are still more pages, go deeper saving what we have into the accumulator
667 fetch_ordered_collection(
670 acc ++ collection["orderedItems"]
673 # No more pages left, just return whatever we already have
674 acc ++ collection["orderedItems"]
677 # Got the amount of pages needed, add them all to the accumulator
678 acc ++ collection["orderedItems"]
682 {:error, "Not an OrderedCollection or OrderedCollectionPage"}
687 #### Report-related helpers
688 def get_reports(params, page, page_size) do
691 |> Map.put("type", "Flag")
692 |> Map.put("skip_preload", true)
693 |> Map.put("total", true)
694 |> Map.put("limit", page_size)
695 |> Map.put("offset", (page - 1) * page_size)
697 ActivityPub.fetch_activities([], params, :offset)
700 @spec get_reports_grouped_by_status(%{required(:activity) => String.t()}) :: %{
701 required(:groups) => [
703 required(:date) => String.t(),
704 required(:account) => %{},
705 required(:status) => %{},
706 required(:actors) => [%User{}],
707 required(:reports) => [%Activity{}]
710 required(:total) => integer
712 def get_reports_grouped_by_status(groups) do
715 |> Enum.map(fn entry ->
717 case Jason.decode(entry.activity) do
718 {:ok, activity} -> activity
719 _ -> build_flag_object(entry.activity)
722 parse_report_group(activity)
726 groups: parsed_groups
730 def parse_report_group(activity) do
731 reports = get_reports_by_status_id(activity["id"])
732 max_date = Enum.max_by(reports, &NaiveDateTime.from_iso8601!(&1.data["published"]))
733 actors = Enum.map(reports, & &1.user_actor)
736 date: max_date.data["published"],
737 account: activity["actor"],
740 content: activity["content"],
741 published: activity["published"]
743 actors: Enum.uniq(actors),
748 def get_reports_by_status_id(ap_id) do
750 where: fragment("(?)->>'type' = 'Flag'", a.data),
751 where: fragment("(?)->'object' @> ?", a.data, ^[%{id: ap_id}])
753 |> Activity.with_preloaded_user_actor()
757 @spec get_reported_activities() :: [
759 required(:activity) => String.t(),
760 required(:date) => String.t()
763 def get_reported_activities do
765 where: fragment("(?)->>'type' = 'Flag'", a.data),
767 date: fragment("max(?->>'published') date", a.data),
769 fragment("jsonb_array_elements_text((? #- '{object,0}')->'object') activity", a.data)
771 group_by: fragment("activity"),
772 order_by: fragment("date DESC")
777 def update_report_state(%Activity{} = activity, state)
778 when state in @strip_status_report_states do
779 {:ok, stripped_activity} = strip_report_status_data(activity)
783 |> Map.put("state", state)
784 |> Map.put("object", stripped_activity.data["object"])
787 |> Changeset.change(data: new_data)
791 def update_report_state(%Activity{} = activity, state) when state in @supported_report_states do
792 new_data = Map.put(activity.data, "state", state)
795 |> Changeset.change(data: new_data)
799 def update_report_state(activity_ids, state) when state in @supported_report_states do
800 activities_num = length(activity_ids)
802 from(a in Activity, where: a.id in ^activity_ids)
803 |> update(set: [data: fragment("jsonb_set(data, '{state}', ?)", ^state)])
804 |> Repo.update_all([])
806 {^activities_num, _} -> :ok
807 _ -> {:error, activity_ids}
811 def update_report_state(_, _), do: {:error, "Unsupported state"}
813 def strip_report_status_data(activity) do
814 [actor | reported_activities] = activity.data["object"]
815 stripped_activities = Enum.map(reported_activities, & &1["id"])
816 new_data = put_in(activity.data, ["object"], [actor | stripped_activities])
818 {:ok, %{activity | data: new_data}}
821 def update_activity_visibility(activity, visibility) when visibility in @valid_visibilities do
822 [to, cc, recipients] =
824 |> get_updated_targets(visibility)
825 |> Enum.map(&Enum.uniq/1)
834 |> Object.change(%{data: object_data})
835 |> Object.update_and_set_cache()
843 |> Map.put(:object, object)
844 |> Activity.change(%{data: activity_data, recipients: recipients})
848 def update_activity_visibility(_, _), do: {:error, "Unsupported visibility"}
850 defp get_updated_targets(
851 %Activity{data: %{"to" => to} = data, recipients: recipients},
854 cc = Map.get(data, "cc", [])
855 follower_address = User.get_cached_by_ap_id(data["actor"]).follower_address
856 public = Pleroma.Constants.as_public()
860 to = [public | List.delete(to, follower_address)]
861 cc = [follower_address | List.delete(cc, public)]
862 recipients = [public | recipients]
866 to = [follower_address | List.delete(to, public)]
867 cc = List.delete(cc, public)
868 recipients = List.delete(recipients, public)
872 to = [follower_address | List.delete(to, public)]
873 cc = [public | List.delete(cc, follower_address)]
874 recipients = recipients ++ [follower_address, public]
882 def get_existing_votes(actor, %{data: %{"id" => id}}) do
884 |> Activity.Queries.by_actor()
885 |> Activity.Queries.by_type("Create")
886 |> Activity.with_preloaded_object()
887 |> where([a, object: o], fragment("(?)->>'inReplyTo' = ?", o.data, ^to_string(id)))
888 |> where([a, object: o], fragment("(?)->>'type' = 'Answer'", o.data))
892 def maybe_put(map, _key, nil), do: map
893 def maybe_put(map, key, value), do: Map.put(map, key, value)