1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.Transmogrifier do
7 A module to handle coding from internal to wire ActivityPub and back.
10 alias Pleroma.EctoType.ActivityPub.ObjectValidators
13 alias Pleroma.Object.Containment
16 alias Pleroma.Web.ActivityPub.ActivityPub
17 alias Pleroma.Web.ActivityPub.Builder
18 alias Pleroma.Web.ActivityPub.ObjectValidator
19 alias Pleroma.Web.ActivityPub.Pipeline
20 alias Pleroma.Web.ActivityPub.Utils
21 alias Pleroma.Web.ActivityPub.Visibility
22 alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
23 alias Pleroma.Web.Federator
24 alias Pleroma.Workers.TransmogrifierWorker
29 require Pleroma.Constants
32 Modifies an incoming AP object (mastodon format) to our internal format.
34 def fix_object(object, options \\ []) do
36 |> strip_internal_fields()
41 |> fix_in_reply_to(options)
42 |> fix_quote_url(options)
50 def fix_summary(%{"summary" => nil} = object) do
51 Map.put(object, "summary", "")
54 def fix_summary(%{"summary" => _} = object) do
55 # summary is present, nothing to do
59 def fix_summary(object), do: Map.put(object, "summary", "")
61 def fix_addressing_list(map, field) do
66 Map.put(map, field, Enum.filter(addrs, &is_binary/1))
69 Map.put(map, field, [addrs])
72 Map.put(map, field, [])
76 # if directMessage flag is set to true, leave the addressing alone
77 def fix_explicit_addressing(%{"directMessage" => true} = object, _follower_collection),
80 def fix_explicit_addressing(%{"to" => to, "cc" => cc} = object, follower_collection) do
82 Utils.determine_explicit_mentions(object) ++
83 [Pleroma.Constants.as_public(), follower_collection]
85 explicit_to = Enum.filter(to, fn x -> x in explicit_mentions end)
86 explicit_cc = Enum.filter(to, fn x -> x not in explicit_mentions end)
91 |> Enum.reject(fn x -> String.ends_with?(x, "/followers") and x != follower_collection end)
95 |> Map.put("to", explicit_to)
96 |> Map.put("cc", final_cc)
99 def fix_addressing(object) do
100 {:ok, %User{follower_address: follower_collection}} =
102 |> Containment.get_actor()
103 |> User.get_or_fetch_by_ap_id()
106 |> fix_addressing_list("to")
107 |> fix_addressing_list("cc")
108 |> fix_addressing_list("bto")
109 |> fix_addressing_list("bcc")
110 |> fix_explicit_addressing(follower_collection)
111 |> CommonFixes.fix_implicit_addressing(follower_collection)
114 def fix_actor(%{"attributedTo" => actor} = object) do
115 actor = Containment.get_actor(%{"actor" => actor})
117 # TODO: Remove actor field for Objects
119 |> Map.put("actor", actor)
120 |> Map.put("attributedTo", actor)
123 def fix_in_reply_to(object, options \\ [])
125 def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object, options)
126 when not is_nil(in_reply_to) do
127 in_reply_to_id = prepare_in_reply_to(in_reply_to)
128 depth = (options[:depth] || 0) + 1
130 if Federator.allowed_thread_distance?(depth) do
131 with {:ok, replied_object} <- get_obj_helper(in_reply_to_id, options),
132 %Activity{} <- Activity.get_create_by_object_ap_id(replied_object.data["id"]) do
134 |> Map.put("inReplyTo", replied_object.data["id"])
135 |> Map.put("context", replied_object.data["context"] || object["conversation"])
136 |> Map.drop(["conversation", "inReplyToAtomUri"])
139 Logger.warn("Couldn't fetch #{inspect(in_reply_to_id)}, error: #{inspect(e)}")
147 def fix_in_reply_to(object, _options), do: object
149 def fix_quote_url(object, options \\ [])
151 def fix_quote_url(%{"quoteUri" => quote_url} = object, options)
152 when not is_nil(quote_url) do
153 depth = (options[:depth] || 0) + 1
155 if Federator.allowed_thread_distance?(depth) do
156 with {:ok, quoted_object} <- get_obj_helper(quote_url, options),
157 %Activity{} <- Activity.get_create_by_object_ap_id(quoted_object.data["id"]) do
159 |> Map.put("quoteUri", quoted_object.data["id"])
162 Logger.warn("Couldn't fetch #{inspect(quote_url)}, error: #{inspect(e)}")
171 def fix_quote_url(%{"quoteUrl" => quote_url} = object, options) do
173 |> Map.put("quoteUri", quote_url)
174 |> Map.delete("quoteUrl")
175 |> fix_quote_url(options)
179 # https://github.com/fedibird/mastodon/issues/9
180 def fix_quote_url(%{"quoteURL" => quote_url} = object, options) do
182 |> Map.put("quoteUri", quote_url)
183 |> Map.delete("quoteURL")
184 |> fix_quote_url(options)
187 def fix_quote_url(%{"_misskey_quote" => quote_url} = object, options) do
189 |> Map.put("quoteUri", quote_url)
190 |> Map.delete("_misskey_quote")
191 |> fix_quote_url(options)
194 def fix_quote_url(object, _), do: object
196 defp prepare_in_reply_to(in_reply_to) do
198 is_bitstring(in_reply_to) ->
201 is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) ->
204 is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) ->
205 Enum.at(in_reply_to, 0)
212 def fix_context(object) do
213 context = object["context"] || object["conversation"] || Utils.generate_context_id()
216 |> Map.put("context", context)
217 |> Map.drop(["conversation"])
220 def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
222 Enum.map(attachment, fn data ->
225 is_list(data["url"]) -> List.first(data["url"])
226 is_map(data["url"]) -> data["url"]
232 is_map(url) && MIME.extensions(url["mediaType"]) != [] ->
235 is_bitstring(data["mediaType"]) && MIME.extensions(data["mediaType"]) != [] ->
238 is_bitstring(data["mimeType"]) && MIME.extensions(data["mimeType"]) != [] ->
247 is_map(url) && is_binary(url["href"]) -> url["href"]
248 is_binary(data["url"]) -> data["url"]
249 is_binary(data["href"]) -> data["href"]
257 "type" => Map.get(url || %{}, "type", "Link")
259 |> Maps.put_if_present("mediaType", media_type)
260 |> Maps.put_if_present("width", (url || %{})["width"] || data["width"])
261 |> Maps.put_if_present("height", (url || %{})["height"] || data["height"])
264 "url" => [attachment_url],
265 "type" => data["type"] || "Document"
267 |> Maps.put_if_present("mediaType", media_type)
268 |> Maps.put_if_present("name", data["name"])
269 |> Maps.put_if_present("blurhash", data["blurhash"])
276 Map.put(object, "attachment", attachments)
279 def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
281 |> Map.put("attachment", [attachment])
285 def fix_attachments(object), do: object
287 def fix_url(%{"url" => url} = object) when is_map(url) do
288 Map.put(object, "url", url["href"])
291 def fix_url(%{"url" => url} = object) when is_list(url) do
292 first_element = Enum.at(url, 0)
296 is_bitstring(first_element) -> first_element
297 is_map(first_element) -> first_element["href"] || ""
301 Map.put(object, "url", url_string)
304 def fix_url(object), do: object
306 def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
309 |> Enum.filter(fn data -> is_map(data) and data["type"] == "Emoji" and data["icon"] end)
310 |> Enum.reduce(%{}, fn data, mapping ->
311 name = String.trim(data["name"], ":")
313 Map.put(mapping, name, data["icon"]["url"])
316 Map.put(object, "emoji", emoji)
319 def fix_emoji(%{"tag" => %{"type" => "Emoji"} = tag} = object) do
320 name = String.trim(tag["name"], ":")
321 emoji = %{name => tag["icon"]["url"]}
323 Map.put(object, "emoji", emoji)
326 def fix_emoji(object), do: object
328 def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
331 |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
333 %{"name" => "#" <> hashtag} -> String.downcase(hashtag)
334 %{"name" => hashtag} -> String.downcase(hashtag)
337 Map.put(object, "tag", tag ++ tags)
340 def fix_tag(%{"tag" => %{} = tag} = object) do
342 |> Map.put("tag", [tag])
346 def fix_tag(object), do: object
348 # content map usually only has one language so this will do for now.
349 def fix_content_map(%{"contentMap" => content_map} = object) do
350 content_groups = Map.to_list(content_map)
351 {_, content} = Enum.at(content_groups, 0)
353 Map.put(object, "content", content)
356 def fix_content_map(object), do: object
358 defp fix_type(%{"type" => "Note", "inReplyTo" => reply_id, "name" => _} = object, options)
359 when is_binary(reply_id) do
360 options = Keyword.put(options, :fetch, true)
362 with %Object{data: %{"type" => "Question"}} <- Object.normalize(reply_id, options) do
363 Map.put(object, "type", "Answer")
369 defp fix_type(object, _options), do: object
371 # Reduce the object list to find the reported user.
372 defp get_reported(objects) do
373 Enum.reduce_while(objects, nil, fn ap_id, _ ->
374 with %User{} = user <- User.get_cached_by_ap_id(ap_id) do
382 def handle_incoming(data, options \\ [])
384 # Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them
386 def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} = data, _options) do
387 with context <- data["context"] || Utils.generate_context_id(),
388 content <- data["content"] || "",
389 %User{} = actor <- User.get_cached_by_ap_id(actor),
390 # Reduce the object list to find the reported user.
391 %User{} = account <- get_reported(objects),
392 # Remove the reported user from the object list.
393 statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do
400 additional: %{"cc" => [account.ap_id]}
402 |> ActivityPub.flag()
406 # disallow objects with bogus IDs
407 def handle_incoming(%{"id" => nil}, _options), do: :error
408 def handle_incoming(%{"id" => ""}, _options), do: :error
409 # length of https:// = 8, should validate better, but good enough for now.
410 def handle_incoming(%{"id" => id}, _options) when is_binary(id) and byte_size(id) < 8,
413 @doc "Rewrite misskey likes into EmojiReacts"
417 "_misskey_reaction" => reaction,
423 |> Map.put("type", "EmojiReact")
424 |> Map.put("content", reaction)
425 |> handle_incoming(options)
431 "_misskey_reaction" => reaction
436 |> Map.put("type", "EmojiReact")
437 |> Map.put("content", reaction)
438 |> handle_incoming(options)
442 %{"type" => "Create", "object" => %{"type" => objtype, "id" => obj_id}} = data,
445 when objtype in ~w{Question Answer Audio Video Event Article Note Page} do
446 fetch_options = Keyword.put(options, :depth, (options[:depth] || 0) + 1)
450 |> strip_internal_fields()
451 |> fix_type(fetch_options)
452 |> fix_in_reply_to(fetch_options)
453 |> fix_quote_url(fetch_options)
455 # Only change the Create's context if the object's context has been modified.
457 if data["object"]["context"] != object["context"] do
459 |> Map.put("object", object)
460 |> Map.put("context", object["context"])
462 Map.put(data, "object", object)
465 options = Keyword.put(options, :local, false)
467 with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
468 nil <- Activity.get_create_by_object_ap_id(obj_id),
469 {:ok, activity, _} <- Pipeline.common_pipeline(data, options) do
472 %Activity{} = activity -> {:ok, activity}
477 def handle_incoming(%{"type" => type} = data, _options)
478 when type in ~w{Like EmojiReact Announce Add Remove} do
479 with :ok <- ObjectValidator.fetch_actor_and_object(data),
480 {:ok, activity, _meta} <- Pipeline.common_pipeline(data, local: false) do
489 %{"type" => type} = data,
492 when type in ~w{Update Block Follow Accept Reject} do
493 with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
494 {:ok, activity, _} <-
495 Pipeline.common_pipeline(data, local: false) do
501 %{"type" => "Delete"} = data,
504 with {:ok, activity, _} <-
505 Pipeline.common_pipeline(data, local: false) do
508 {:error, {:validate, _}} = e ->
509 # Check if we have a create activity for this
510 with {:ok, object_id} <- ObjectValidators.ObjectID.cast(data["object"]),
511 %Activity{data: %{"actor" => actor}} <-
512 Activity.create_by_object_ap_id(object_id) |> Repo.one(),
513 # We have one, insert a tombstone and retry
514 {:ok, tombstone_data, _} <- Builder.tombstone(actor, object_id),
515 {:ok, _tombstone} <- Object.create(tombstone_data) do
516 handle_incoming(data)
526 "object" => %{"type" => "Follow", "object" => followed},
532 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
533 {:ok, %User{} = follower} <- User.get_or_fetch_by_ap_id(follower),
534 {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
535 User.unfollow(follower, followed)
545 "object" => %{"type" => type}
549 when type in ["Like", "EmojiReact", "Announce", "Block"] do
550 with {:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
555 # For Undos that don't have the complete object attached, try to find it in our database.
563 when is_binary(object) do
564 with %Activity{data: data} <- Activity.get_by_ap_id(object) do
566 |> Map.put("object", data)
567 |> handle_incoming(options)
576 "actor" => origin_actor,
577 "object" => origin_actor,
578 "target" => target_actor
582 with %User{} = origin_user <- User.get_cached_by_ap_id(origin_actor),
583 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_actor),
584 true <- origin_actor in target_user.also_known_as do
585 ActivityPub.move(origin_user, target_user, false)
591 def handle_incoming(_, _), do: :error
593 @spec get_obj_helper(String.t(), Keyword.t()) :: {:ok, Object.t()} | nil
594 def get_obj_helper(id, options \\ []) do
595 options = Keyword.put(options, :fetch, true)
597 case Object.normalize(id, options) do
598 %Object{} = object -> {:ok, object}
603 @spec get_embedded_obj_helper(String.t() | Object.t(), User.t()) :: {:ok, Object.t()} | nil
604 def get_embedded_obj_helper(%{"attributedTo" => attributed_to, "id" => object_id} = data, %User{
607 when attributed_to == ap_id do
608 with {:ok, activity} <-
613 "actor" => attributed_to,
616 {:ok, Object.normalize(activity, fetch: false)}
618 _ -> get_obj_helper(object_id)
622 def get_embedded_obj_helper(object_id, _) do
623 get_obj_helper(object_id)
626 def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
627 with false <- String.starts_with?(in_reply_to, "http"),
628 {:ok, %{data: replied_to_object}} <- get_obj_helper(in_reply_to) do
629 Map.put(object, "inReplyTo", replied_to_object["external_url"] || in_reply_to)
635 def set_reply_to_uri(obj), do: obj
637 def set_quote_url(%{"quoteUri" => quote} = object) when is_binary(quote) do
638 Map.put(object, "quoteUrl", quote)
641 def set_quote_url(obj), do: obj
644 Serialized Mastodon-compatible `replies` collection containing _self-replies_.
645 Based on Mastodon's ActivityPub::NoteSerializer#replies.
647 def set_replies(obj_data) do
649 with limit when limit > 0 <-
650 Pleroma.Config.get([:activitypub, :note_replies_output_limit], 0),
651 %Object{} = object <- Object.get_cached_by_ap_id(obj_data["id"]) do
653 |> Object.self_replies()
654 |> select([o], fragment("?->>'id'", o.data))
661 set_replies(obj_data, replies_uris)
664 defp set_replies(obj, []) do
668 defp set_replies(obj, replies_uris) do
669 replies_collection = %{
670 "type" => "Collection",
671 "items" => replies_uris
674 Map.merge(obj, %{"replies" => replies_collection})
677 def replies(%{"replies" => %{"first" => %{"items" => items}}}) when not is_nil(items) do
681 def replies(%{"replies" => %{"items" => items}}) when not is_nil(items) do
685 def replies(_), do: []
687 # Prepares the object of an outgoing create activity.
688 def prepare_object(object) do
694 |> prepare_attachments
699 |> strip_internal_fields
700 |> strip_internal_tags
706 # internal -> Mastodon
709 def prepare_outgoing(%{"type" => activity_type, "object" => object_id} = data)
710 when activity_type in ["Create"] do
713 |> Object.normalize(fetch: false)
719 |> Map.put("object", object)
720 |> Map.merge(Utils.make_json_ld_header())
726 def prepare_outgoing(%{"type" => "Announce", "actor" => ap_id, "object" => object_id} = data) do
729 |> Object.normalize(fetch: false)
732 if Visibility.is_private?(object) && object.data["actor"] == ap_id do
733 data |> Map.put("object", object |> Map.get(:data) |> prepare_object)
735 data |> maybe_fix_object_url
740 |> strip_internal_fields
741 |> Map.merge(Utils.make_json_ld_header())
747 # Mastodon Accept/Reject requires a non-normalized object containing the actor URIs,
748 # because of course it does.
749 def prepare_outgoing(%{"type" => "Accept"} = data) do
750 with follow_activity <- Activity.normalize(data["object"]) do
752 "actor" => follow_activity.actor,
753 "object" => follow_activity.data["object"],
754 "id" => follow_activity.data["id"],
760 |> Map.put("object", object)
761 |> Map.merge(Utils.make_json_ld_header())
767 def prepare_outgoing(%{"type" => "Reject"} = data) do
768 with follow_activity <- Activity.normalize(data["object"]) do
770 "actor" => follow_activity.actor,
771 "object" => follow_activity.data["object"],
772 "id" => follow_activity.data["id"],
778 |> Map.put("object", object)
779 |> Map.merge(Utils.make_json_ld_header())
785 def prepare_outgoing(%{"type" => _type} = data) do
788 |> strip_internal_fields
789 |> maybe_fix_object_url
790 |> Map.merge(Utils.make_json_ld_header())
795 def maybe_fix_object_url(%{"object" => object} = data) when is_binary(object) do
796 with false <- String.starts_with?(object, "http"),
797 {:fetch, {:ok, relative_object}} <- {:fetch, get_obj_helper(object)},
798 %{data: %{"external_url" => external_url}} when not is_nil(external_url) <-
800 Map.put(data, "object", external_url)
803 Logger.error("Couldn't fetch #{object} #{inspect(e)}")
811 def maybe_fix_object_url(data), do: data
813 def add_hashtags(object) do
815 (object["tag"] || [])
817 # Expand internal representation tags into AS2 tags.
818 tag when is_binary(tag) ->
820 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
825 # Do not process tags which are already AS2 tag objects.
826 tag when is_map(tag) ->
830 Map.put(object, "tag", tags)
833 # TODO These should be added on our side on insertion, it doesn't make much
834 # sense to regenerate these all the time
835 def add_mention_tags(object) do
836 to = object["to"] || []
837 cc = object["cc"] || []
838 mentioned = User.get_users_from_set(to ++ cc, local_only: false)
840 mentions = Enum.map(mentioned, &build_mention_tag/1)
842 tags = object["tag"] || []
843 Map.put(object, "tag", tags ++ mentions)
846 defp build_mention_tag(%{ap_id: ap_id, nickname: nickname} = _) do
847 %{"type" => "Mention", "href" => ap_id, "name" => "@#{nickname}"}
850 def take_emoji_tags(%User{emoji: emoji}) do
853 |> Enum.map(&build_emoji_tag/1)
856 # TODO: we should probably send mtime instead of unix epoch time for updated
857 def add_emoji_tags(%{"emoji" => emoji} = object) do
858 tags = object["tag"] || []
860 out = Enum.map(emoji, &build_emoji_tag/1)
862 Map.put(object, "tag", tags ++ out)
865 def add_emoji_tags(object), do: object
867 defp build_emoji_tag({name, url}) do
869 "icon" => %{"url" => "#{URI.encode(url)}", "type" => "Image"},
870 "name" => ":" <> name <> ":",
872 "updated" => "1970-01-01T00:00:00Z",
877 def set_conversation(object) do
878 Map.put(object, "conversation", object["context"])
881 def set_type(%{"type" => "Answer"} = object) do
882 Map.put(object, "type", "Note")
885 def set_type(object), do: object
887 def add_attributed_to(object) do
888 attributed_to = object["attributedTo"] || object["actor"]
889 Map.put(object, "attributedTo", attributed_to)
892 def prepare_attachments(object) do
895 |> Map.get("attachment", [])
896 |> Enum.map(fn data ->
897 [%{"mediaType" => media_type, "href" => href} = url | _] = data["url"]
901 "mediaType" => media_type,
902 "name" => data["name"],
905 |> Maps.put_if_present("width", url["width"])
906 |> Maps.put_if_present("height", url["height"])
907 |> Maps.put_if_present("blurhash", data["blurhash"])
910 Map.put(object, "attachment", attachments)
913 def strip_internal_fields(object) do
914 Map.drop(object, Pleroma.Constants.object_internal_fields())
917 defp strip_internal_tags(%{"tag" => tags} = object) do
918 tags = Enum.filter(tags, fn x -> is_map(x) end)
920 Map.put(object, "tag", tags)
923 defp strip_internal_tags(object), do: object
925 def perform(:user_upgrade, user) do
926 # we pass a fake user so that the followers collection is stripped away
927 old_follower_address = User.ap_followers(%User{nickname: user.nickname})
931 where: ^old_follower_address in a.recipients,
936 "array_replace(?,?,?)",
938 ^old_follower_address,
939 ^user.follower_address
944 |> Repo.update_all([])
947 def upgrade_user_from_ap_id(ap_id) do
948 with %User{local: false} = user <- User.get_cached_by_ap_id(ap_id),
949 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id),
950 {:ok, user} <- update_user(user, data) do
951 {:ok, _pid} = Task.start(fn -> ActivityPub.pinned_fetch_task(user) end)
952 TransmogrifierWorker.enqueue("user_upgrade", %{"user_id" => user.id})
955 %User{} = user -> {:ok, user}
960 defp update_user(user, data) do
962 |> User.remote_user_changeset(data)
963 |> User.update_and_set_cache()
966 def maybe_fix_user_url(%{"url" => url} = data) when is_map(url) do
967 Map.put(data, "url", url["href"])
970 def maybe_fix_user_url(data), do: data
972 def maybe_fix_user_object(data), do: maybe_fix_user_url(data)