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.Visibility
15 alias Pleroma.Web.Endpoint
16 alias Pleroma.Web.Router.Helpers
21 require Pleroma.Constants
23 @supported_object_types ["Article", "Note", "Video", "Page", "Question", "Answer"]
24 @supported_report_states ~w(open closed resolved)
25 @valid_visibilities ~w(public unlisted private direct)
27 # Some implementations send the actor URI as the actor field, others send the entire actor object,
28 # so figure out what the actor's URI is based on what we have.
29 def get_ap_id(%{"id" => id} = _), do: id
30 def get_ap_id(id), do: id
32 def normalize_params(params) do
33 Map.put(params, "actor", get_ap_id(params["actor"]))
36 @spec determine_explicit_mentions(map()) :: map()
37 def determine_explicit_mentions(%{"tag" => tag} = _) when is_list(tag) do
39 %{"type" => "Mention", "href" => href} -> [href]
44 def determine_explicit_mentions(%{"tag" => tag} = object) when is_map(tag) do
46 |> Map.put("tag", [tag])
47 |> determine_explicit_mentions()
50 def determine_explicit_mentions(_), do: []
52 @spec recipient_in_collection(any(), any()) :: boolean()
53 defp recipient_in_collection(ap_id, coll) when is_binary(coll), do: ap_id == coll
54 defp recipient_in_collection(ap_id, coll) when is_list(coll), do: ap_id in coll
55 defp recipient_in_collection(_, _), do: false
57 @spec recipient_in_message(User.t(), User.t(), map()) :: boolean()
58 def recipient_in_message(%User{ap_id: ap_id} = recipient, %User{} = actor, params) do
59 addresses = [params["to"], params["cc"], params["bto"], params["bcc"]]
62 Enum.any?(addresses, &recipient_in_collection(ap_id, &1)) -> true
63 # if the message is unaddressed at all, then assume it is directly addressed
65 Enum.all?(addresses, &is_nil(&1)) -> true
66 # if the message is sent from somebody the user is following, then assume it
67 # is addressed to the recipient
68 User.following?(recipient, actor) -> true
73 defp extract_list(target) when is_binary(target), do: [target]
74 defp extract_list(lst) when is_list(lst), do: lst
75 defp extract_list(_), do: []
77 def maybe_splice_recipient(ap_id, params) do
79 !recipient_in_collection(ap_id, params["to"]) &&
80 !recipient_in_collection(ap_id, params["cc"])
83 cc_list = extract_list(params["cc"])
84 Map.put(params, "cc", [ap_id | cc_list])
90 def make_json_ld_header do
93 "https://www.w3.org/ns/activitystreams",
94 "#{Web.base_url()}/schemas/litepub-0.1.jsonld",
103 DateTime.utc_now() |> DateTime.to_iso8601()
106 def generate_activity_id do
107 generate_id("activities")
110 def generate_context_id do
111 generate_id("contexts")
114 def generate_object_id do
115 Helpers.o_status_url(Endpoint, :object, UUID.generate())
118 def generate_id(type) do
119 "#{Web.base_url()}/#{type}/#{UUID.generate()}"
122 def get_notified_from_object(%{"type" => type} = object) when type in @supported_object_types do
123 fake_create_activity = %{
124 "to" => object["to"],
125 "cc" => object["cc"],
130 get_notified_from_object(fake_create_activity)
133 def get_notified_from_object(object) do
134 Notification.get_notified_from_activity(%Activity{data: object}, false)
137 def create_context(context) do
138 context = context || generate_id("contexts")
140 # Ecto has problems accessing the constraint inside the jsonb,
141 # so we explicitly check for the existed object before insert
142 object = Object.get_cached_by_ap_id(context)
144 with true <- is_nil(object),
145 changeset <- Object.context_mapping(context),
146 {:ok, inserted_object} <- Repo.insert(changeset) do
155 Enqueues an activity for federation if it's local
157 @spec maybe_federate(any()) :: :ok
158 def maybe_federate(%Activity{local: true} = activity) do
159 if Pleroma.Config.get!([:instance, :federating]) do
160 Pleroma.Web.Federator.publish(activity)
166 def maybe_federate(_), do: :ok
169 Adds an id and a published data if they aren't there,
170 also adds it to an included object
172 @spec lazy_put_activity_defaults(map(), boolean) :: map()
173 def lazy_put_activity_defaults(map, fake? \\ false)
175 def lazy_put_activity_defaults(map, true) do
177 |> Map.put_new("id", "pleroma:fakeid")
178 |> Map.put_new_lazy("published", &make_date/0)
179 |> Map.put_new("context", "pleroma:fakecontext")
180 |> Map.put_new("context_id", -1)
181 |> lazy_put_object_defaults(true)
184 def lazy_put_activity_defaults(map, _fake?) do
185 %{data: %{"id" => context}, id: context_id} = create_context(map["context"])
188 |> Map.put_new_lazy("id", &generate_activity_id/0)
189 |> Map.put_new_lazy("published", &make_date/0)
190 |> Map.put_new("context", context)
191 |> Map.put_new("context_id", context_id)
192 |> lazy_put_object_defaults(false)
195 # Adds an id and published date if they aren't there.
197 @spec lazy_put_object_defaults(map(), boolean()) :: map()
198 defp lazy_put_object_defaults(%{"object" => map} = activity, true)
202 |> Map.put_new("id", "pleroma:fake_object_id")
203 |> Map.put_new_lazy("published", &make_date/0)
204 |> Map.put_new("context", activity["context"])
205 |> Map.put_new("context_id", activity["context_id"])
206 |> Map.put_new("fake", true)
208 %{activity | "object" => object}
211 defp lazy_put_object_defaults(%{"object" => map} = activity, _)
215 |> Map.put_new_lazy("id", &generate_object_id/0)
216 |> Map.put_new_lazy("published", &make_date/0)
217 |> Map.put_new("context", activity["context"])
218 |> Map.put_new("context_id", activity["context_id"])
220 %{activity | "object" => object}
223 defp lazy_put_object_defaults(activity, _), do: activity
226 Inserts a full object if it is contained in an activity.
228 def insert_full_object(%{"object" => %{"type" => type} = object_data} = map)
229 when is_map(object_data) and type in @supported_object_types do
230 with {:ok, object} <- Object.create(object_data) do
231 map = Map.put(map, "object", object.data["id"])
237 def insert_full_object(map), do: {:ok, map, nil}
239 #### Like-related helpers
242 Returns an existing like if a user already liked an object
244 @spec get_existing_like(String.t(), map()) :: Activity.t() | nil
245 def get_existing_like(actor, %{data: %{"id" => id}}) do
247 |> Activity.Queries.by_actor()
248 |> Activity.Queries.by_object_id(id)
249 |> Activity.Queries.by_type("Like")
255 Returns like activities targeting an object
257 def get_object_likes(%{data: %{"id" => id}}) do
259 |> Activity.Queries.by_object_id()
260 |> Activity.Queries.by_type("Like")
264 @spec make_like_data(User.t(), map(), String.t()) :: map()
266 %User{ap_id: ap_id} = actor,
267 %{data: %{"actor" => object_actor_id, "id" => id}} = object,
270 object_actor = User.get_cached_by_ap_id(object_actor_id)
273 if Visibility.is_public?(object) do
274 [actor.follower_address, object.data["actor"]]
276 [object.data["actor"]]
280 (object.data["to"] ++ (object.data["cc"] || []))
281 |> List.delete(actor.ap_id)
282 |> List.delete(object_actor.follower_address)
290 "context" => object.data["context"]
292 |> maybe_put("id", activity_id)
295 @spec update_element_in_object(String.t(), list(any), Object.t()) ::
296 {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
297 def update_element_in_object(property, element, object) do
301 %{"#{property}_count" => length(element), "#{property}s" => element}
305 |> Changeset.change(data: data)
306 |> Object.update_and_set_cache()
309 @spec add_like_to_object(Activity.t(), Object.t()) ::
310 {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
311 def add_like_to_object(%Activity{data: %{"actor" => actor}}, object) do
312 [actor | fetch_likes(object)]
314 |> update_likes_in_object(object)
317 @spec remove_like_from_object(Activity.t(), Object.t()) ::
318 {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
319 def remove_like_from_object(%Activity{data: %{"actor" => actor}}, object) do
322 |> List.delete(actor)
323 |> update_likes_in_object(object)
326 defp update_likes_in_object(likes, object) do
327 update_element_in_object("like", likes, object)
330 defp fetch_likes(object) do
331 if is_list(object.data["likes"]) do
338 #### Follow-related helpers
341 Updates a follow activity's state (for locked accounts).
343 @spec update_follow_state_for_all(Activity.t(), String.t()) :: {:ok, Activity} | {:error, any()}
344 def update_follow_state_for_all(
345 %Activity{data: %{"actor" => actor, "object" => object}} = activity,
349 |> Activity.Queries.by_type()
350 |> Activity.Queries.by_actor(actor)
351 |> Activity.Queries.by_object_id(object)
352 |> where(fragment("data->>'state' = 'pending'"))
353 |> update(set: [data: fragment("jsonb_set(data, '{state}', ?)", ^state)])
354 |> Repo.update_all([])
356 User.set_follow_state_cache(actor, object, state)
358 activity = Activity.get_by_id(activity.id)
363 def update_follow_state(
364 %Activity{data: %{"actor" => actor, "object" => object}} = activity,
367 new_data = Map.put(activity.data, "state", state)
368 changeset = Changeset.change(activity, data: new_data)
370 with {:ok, activity} <- Repo.update(changeset) do
371 User.set_follow_state_cache(actor, object, state)
377 Makes a follow activity data for the given follower and followed
379 def make_follow_data(
380 %User{ap_id: follower_id},
381 %User{ap_id: followed_id} = _followed,
386 "actor" => follower_id,
387 "to" => [followed_id],
388 "cc" => [Pleroma.Constants.as_public()],
389 "object" => followed_id,
392 |> maybe_put("id", activity_id)
395 def fetch_latest_follow(%User{ap_id: follower_id}, %User{ap_id: followed_id}) do
397 |> Activity.Queries.by_type()
398 |> where(actor: ^follower_id)
399 # this is to use the index
400 |> Activity.Queries.by_object_id(followed_id)
401 |> order_by([activity], fragment("? desc nulls last", activity.id))
406 #### Announce-related helpers
409 Retruns an existing announce activity if the notice has already been announced
411 @spec get_existing_announce(String.t(), map()) :: Activity.t() | nil
412 def get_existing_announce(actor, %{data: %{"id" => ap_id}}) do
414 |> Activity.Queries.by_type()
415 |> where(actor: ^actor)
416 # this is to use the index
417 |> Activity.Queries.by_object_id(ap_id)
422 Make announce activity data for the given actor and object
424 # for relayed messages, we only want to send to subscribers
425 def make_announce_data(
426 %User{ap_id: ap_id} = user,
427 %Object{data: %{"id" => id}} = object,
432 "type" => "Announce",
435 "to" => [user.follower_address],
437 "context" => object.data["context"]
439 |> maybe_put("id", activity_id)
442 def make_announce_data(
443 %User{ap_id: ap_id} = user,
444 %Object{data: %{"id" => id}} = object,
449 "type" => "Announce",
452 "to" => [user.follower_address, object.data["actor"]],
453 "cc" => [Pleroma.Constants.as_public()],
454 "context" => object.data["context"]
456 |> maybe_put("id", activity_id)
460 Make unannounce activity data for the given actor and object
462 def make_unannounce_data(
463 %User{ap_id: ap_id} = user,
464 %Activity{data: %{"context" => context}} = activity,
470 "object" => activity.data,
471 "to" => [user.follower_address, activity.data["actor"]],
472 "cc" => [Pleroma.Constants.as_public()],
475 |> maybe_put("id", activity_id)
478 def make_unlike_data(
479 %User{ap_id: ap_id} = user,
480 %Activity{data: %{"context" => context}} = activity,
486 "object" => activity.data,
487 "to" => [user.follower_address, activity.data["actor"]],
488 "cc" => [Pleroma.Constants.as_public()],
491 |> maybe_put("id", activity_id)
494 @spec add_announce_to_object(Activity.t(), Object.t()) ::
495 {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
496 def add_announce_to_object(
497 %Activity{data: %{"actor" => actor, "cc" => [Pleroma.Constants.as_public()]}},
500 announcements = take_announcements(object)
502 with announcements <- Enum.uniq([actor | announcements]) do
503 update_element_in_object("announcement", announcements, object)
507 def add_announce_to_object(_, object), do: {:ok, object}
509 @spec remove_announce_from_object(Activity.t(), Object.t()) ::
510 {:ok, Object.t()} | {:error, Ecto.Changeset.t()}
511 def remove_announce_from_object(%Activity{data: %{"actor" => actor}}, object) do
512 with announcements <- List.delete(take_announcements(object), actor) do
513 update_element_in_object("announcement", announcements, object)
517 defp take_announcements(%{data: %{"announcements" => announcements}} = _)
518 when is_list(announcements),
521 defp take_announcements(_), do: []
523 #### Unfollow-related helpers
525 def make_unfollow_data(follower, followed, follow_activity, activity_id) do
528 "actor" => follower.ap_id,
529 "to" => [followed.ap_id],
530 "object" => follow_activity.data
532 |> maybe_put("id", activity_id)
535 #### Block-related helpers
536 @spec fetch_latest_block(User.t(), User.t()) :: Activity.t() | nil
537 def fetch_latest_block(%User{ap_id: blocker_id}, %User{ap_id: blocked_id}) do
539 |> Activity.Queries.by_type()
540 |> where(actor: ^blocker_id)
541 # this is to use the index
542 |> Activity.Queries.by_object_id(blocked_id)
543 |> order_by([activity], fragment("? desc nulls last", activity.id))
548 def make_block_data(blocker, blocked, activity_id) do
551 "actor" => blocker.ap_id,
552 "to" => [blocked.ap_id],
553 "object" => blocked.ap_id
555 |> maybe_put("id", activity_id)
558 def make_unblock_data(blocker, blocked, block_activity, activity_id) do
561 "actor" => blocker.ap_id,
562 "to" => [blocked.ap_id],
563 "object" => block_activity.data
565 |> maybe_put("id", activity_id)
568 #### Create-related helpers
570 def make_create_data(params, additional) do
571 published = params.published || make_date()
575 "to" => params.to |> Enum.uniq(),
576 "actor" => params.actor.ap_id,
577 "object" => params.object,
578 "published" => published,
579 "context" => params.context
581 |> Map.merge(additional)
584 #### Flag-related helpers
585 @spec make_flag_data(map(), map()) :: map()
586 def make_flag_data(%{actor: actor, context: context, content: content} = params, additional) do
589 "actor" => actor.ap_id,
590 "content" => content,
591 "object" => build_flag_object(params),
592 "context" => context,
595 |> Map.merge(additional)
598 def make_flag_data(_, _), do: %{}
600 defp build_flag_object(%{account: account, statuses: statuses} = _) do
602 Enum.map(statuses || [], fn
603 %Activity{} = act -> act.data["id"]
604 act when is_map(act) -> act["id"]
605 act when is_binary(act) -> act
609 defp build_flag_object(_), do: []
612 Fetches the OrderedCollection/OrderedCollectionPage from `from`, limiting the amount of pages fetched after
613 the first one to `pages_left` pages.
614 If the amount of pages is higher than the collection has, it returns whatever was there.
616 def fetch_ordered_collection(from, pages_left, acc \\ []) do
617 with {:ok, response} <- Tesla.get(from),
618 {:ok, collection} <- Jason.decode(response.body) do
619 case collection["type"] do
620 "OrderedCollection" ->
621 # If we've encountered the OrderedCollection and not the page,
622 # just call the same function on the page address
623 fetch_ordered_collection(collection["first"], pages_left)
625 "OrderedCollectionPage" ->
627 # There are still more pages
628 if Map.has_key?(collection, "next") do
629 # There are still more pages, go deeper saving what we have into the accumulator
630 fetch_ordered_collection(
633 acc ++ collection["orderedItems"]
636 # No more pages left, just return whatever we already have
637 acc ++ collection["orderedItems"]
640 # Got the amount of pages needed, add them all to the accumulator
641 acc ++ collection["orderedItems"]
645 {:error, "Not an OrderedCollection or OrderedCollectionPage"}
650 #### Report-related helpers
652 def update_report_state(%Activity{} = activity, state) when state in @supported_report_states do
653 new_data = Map.put(activity.data, "state", state)
656 |> Changeset.change(data: new_data)
660 def update_report_state(_, _), do: {:error, "Unsupported state"}
662 def update_activity_visibility(activity, visibility) when visibility in @valid_visibilities do
663 [to, cc, recipients] =
665 |> get_updated_targets(visibility)
666 |> Enum.map(&Enum.uniq/1)
675 |> Object.change(%{data: object_data})
676 |> Object.update_and_set_cache()
684 |> Map.put(:object, object)
685 |> Activity.change(%{data: activity_data, recipients: recipients})
689 def update_activity_visibility(_, _), do: {:error, "Unsupported visibility"}
691 defp get_updated_targets(
692 %Activity{data: %{"to" => to} = data, recipients: recipients},
695 cc = Map.get(data, "cc", [])
696 follower_address = User.get_cached_by_ap_id(data["actor"]).follower_address
697 public = Pleroma.Constants.as_public()
701 to = [public | List.delete(to, follower_address)]
702 cc = [follower_address | List.delete(cc, public)]
703 recipients = [public | recipients]
707 to = [follower_address | List.delete(to, public)]
708 cc = List.delete(cc, public)
709 recipients = List.delete(recipients, public)
713 to = [follower_address | List.delete(to, public)]
714 cc = [public | List.delete(cc, follower_address)]
715 recipients = recipients ++ [follower_address, public]
723 def get_existing_votes(actor, %{data: %{"id" => id}}) do
725 |> Activity.Queries.by_actor()
726 |> Activity.Queries.by_type("Create")
727 |> Activity.with_preloaded_object()
728 |> where([a, object: o], fragment("(?)->>'inReplyTo' = ?", o.data, ^to_string(id)))
729 |> where([a, object: o], fragment("(?)->>'type' = 'Answer'", o.data))
733 defp maybe_put(map, _key, nil), do: map
734 defp maybe_put(map, key, value), do: Map.put(map, key, value)