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.Federator
23 alias Pleroma.Workers.TransmogrifierWorker
28 require Pleroma.Constants
31 Modifies an incoming AP object (mastodon format) to our internal format.
33 def fix_object(object, options \\ []) do
35 |> strip_internal_fields()
40 |> fix_in_reply_to(options)
41 |> fix_quote_url(options)
49 def fix_summary(%{"summary" => nil} = object) do
50 Map.put(object, "summary", "")
53 def fix_summary(%{"summary" => _} = object) do
54 # summary is present, nothing to do
58 def fix_summary(object), do: Map.put(object, "summary", "")
60 def fix_addressing_list(map, field) do
65 Map.put(map, field, Enum.filter(addrs, &is_binary/1))
68 Map.put(map, field, [addrs])
71 Map.put(map, field, [])
75 # if directMessage flag is set to true, leave the addressing alone
76 def fix_explicit_addressing(%{"directMessage" => true} = object, _follower_collection),
79 def fix_explicit_addressing(%{"to" => to, "cc" => cc} = object, follower_collection) do
81 Utils.determine_explicit_mentions(object) ++
82 [Pleroma.Constants.as_public(), follower_collection]
84 explicit_to = Enum.filter(to, fn x -> x in explicit_mentions end)
85 explicit_cc = Enum.filter(to, fn x -> x not in explicit_mentions end)
90 |> Enum.reject(fn x -> String.ends_with?(x, "/followers") and x != follower_collection end)
94 |> Map.put("to", explicit_to)
95 |> Map.put("cc", final_cc)
98 # if as:Public is addressed, then make sure the followers collection is also addressed
99 # so that the activities will be delivered to local users.
100 def fix_implicit_addressing(%{"to" => to, "cc" => cc} = object, followers_collection) do
101 recipients = to ++ cc
103 if followers_collection not in recipients do
105 Pleroma.Constants.as_public() in cc ->
106 to = to ++ [followers_collection]
107 Map.put(object, "to", to)
109 Pleroma.Constants.as_public() in to ->
110 cc = cc ++ [followers_collection]
111 Map.put(object, "cc", cc)
121 def fix_addressing(object) do
122 {:ok, %User{follower_address: follower_collection}} =
124 |> Containment.get_actor()
125 |> User.get_or_fetch_by_ap_id()
128 |> fix_addressing_list("to")
129 |> fix_addressing_list("cc")
130 |> fix_addressing_list("bto")
131 |> fix_addressing_list("bcc")
132 |> fix_explicit_addressing(follower_collection)
133 |> fix_implicit_addressing(follower_collection)
136 def fix_actor(%{"attributedTo" => actor} = object) do
137 actor = Containment.get_actor(%{"actor" => actor})
139 # TODO: Remove actor field for Objects
141 |> Map.put("actor", actor)
142 |> Map.put("attributedTo", actor)
145 def fix_in_reply_to(object, options \\ [])
147 def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object, options)
148 when not is_nil(in_reply_to) do
149 in_reply_to_id = prepare_in_reply_to(in_reply_to)
150 depth = (options[:depth] || 0) + 1
152 if Federator.allowed_thread_distance?(depth) do
153 with {:ok, replied_object} <- get_obj_helper(in_reply_to_id, options),
154 %Activity{} <- Activity.get_create_by_object_ap_id(replied_object.data["id"]) do
156 |> Map.put("inReplyTo", replied_object.data["id"])
157 |> Map.put("context", replied_object.data["context"] || object["conversation"])
158 |> Map.drop(["conversation", "inReplyToAtomUri"])
161 Logger.warn("Couldn't fetch #{inspect(in_reply_to_id)}, error: #{inspect(e)}")
169 def fix_in_reply_to(object, _options), do: object
171 def fix_quote_url(object, options \\ [])
173 def fix_quote_url(%{"quoteUri" => quote_url} = object, options)
174 when not is_nil(quote_url) do
175 depth = (options[:depth] || 0) + 1
177 if Federator.allowed_thread_distance?(depth) do
178 with {:ok, quoted_object} <- get_obj_helper(quote_url, options),
179 %Activity{} <- Activity.get_create_by_object_ap_id(quoted_object.data["id"]) do
181 |> Map.put("quoteUri", quoted_object.data["id"])
184 Logger.warn("Couldn't fetch #{inspect(quote_url)}, error: #{inspect(e)}")
193 def fix_quote_url(%{"quoteUrl" => quote_url} = object, options) do
195 |> Map.put("quoteUri", quote_url)
196 |> Map.delete("quoteUrl")
197 |> fix_quote_url(options)
201 # https://github.com/fedibird/mastodon/issues/9
202 def fix_quote_url(%{"quoteURL" => quote_url} = object, options) do
204 |> Map.put("quoteUri", quote_url)
205 |> Map.delete("quoteURL")
206 |> fix_quote_url(options)
209 def fix_quote_url(%{"_misskey_quote" => quote_url} = object, options) do
211 |> Map.put("quoteUri", quote_url)
212 |> Map.delete("_misskey_quote")
213 |> fix_quote_url(options)
216 def fix_quote_url(object, _), do: object
218 defp prepare_in_reply_to(in_reply_to) do
220 is_bitstring(in_reply_to) ->
223 is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) ->
226 is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) ->
227 Enum.at(in_reply_to, 0)
234 def fix_context(object) do
235 context = object["context"] || object["conversation"] || Utils.generate_context_id()
238 |> Map.put("context", context)
239 |> Map.drop(["conversation"])
242 def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
244 Enum.map(attachment, fn data ->
247 is_list(data["url"]) -> List.first(data["url"])
248 is_map(data["url"]) -> data["url"]
254 is_map(url) && MIME.extensions(url["mediaType"]) != [] ->
257 is_bitstring(data["mediaType"]) && MIME.extensions(data["mediaType"]) != [] ->
260 is_bitstring(data["mimeType"]) && MIME.extensions(data["mimeType"]) != [] ->
269 is_map(url) && is_binary(url["href"]) -> url["href"]
270 is_binary(data["url"]) -> data["url"]
271 is_binary(data["href"]) -> data["href"]
279 "type" => Map.get(url || %{}, "type", "Link")
281 |> Maps.put_if_present("mediaType", media_type)
282 |> Maps.put_if_present("width", (url || %{})["width"] || data["width"])
283 |> Maps.put_if_present("height", (url || %{})["height"] || data["height"])
286 "url" => [attachment_url],
287 "type" => data["type"] || "Document"
289 |> Maps.put_if_present("mediaType", media_type)
290 |> Maps.put_if_present("name", data["name"])
291 |> Maps.put_if_present("blurhash", data["blurhash"])
298 Map.put(object, "attachment", attachments)
301 def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
303 |> Map.put("attachment", [attachment])
307 def fix_attachments(object), do: object
309 def fix_url(%{"url" => url} = object) when is_map(url) do
310 Map.put(object, "url", url["href"])
313 def fix_url(%{"url" => url} = object) when is_list(url) do
314 first_element = Enum.at(url, 0)
318 is_bitstring(first_element) -> first_element
319 is_map(first_element) -> first_element["href"] || ""
323 Map.put(object, "url", url_string)
326 def fix_url(object), do: object
328 def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
331 |> Enum.filter(fn data -> is_map(data) and data["type"] == "Emoji" and data["icon"] end)
332 |> Enum.reduce(%{}, fn data, mapping ->
333 name = String.trim(data["name"], ":")
335 Map.put(mapping, name, data["icon"]["url"])
338 Map.put(object, "emoji", emoji)
341 def fix_emoji(%{"tag" => %{"type" => "Emoji"} = tag} = object) do
342 name = String.trim(tag["name"], ":")
343 emoji = %{name => tag["icon"]["url"]}
345 Map.put(object, "emoji", emoji)
348 def fix_emoji(object), do: object
350 def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
353 |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
355 %{"name" => "#" <> hashtag} -> String.downcase(hashtag)
356 %{"name" => hashtag} -> String.downcase(hashtag)
359 Map.put(object, "tag", tag ++ tags)
362 def fix_tag(%{"tag" => %{} = tag} = object) do
364 |> Map.put("tag", [tag])
368 def fix_tag(object), do: object
370 # content map usually only has one language so this will do for now.
371 def fix_content_map(%{"contentMap" => content_map} = object) do
372 content_groups = Map.to_list(content_map)
373 {_, content} = Enum.at(content_groups, 0)
375 Map.put(object, "content", content)
378 def fix_content_map(object), do: object
380 defp fix_type(%{"type" => "Note", "inReplyTo" => reply_id, "name" => _} = object, options)
381 when is_binary(reply_id) do
382 options = Keyword.put(options, :fetch, true)
384 with %Object{data: %{"type" => "Question"}} <- Object.normalize(reply_id, options) do
385 Map.put(object, "type", "Answer")
391 defp fix_type(object, _options), do: object
393 # Reduce the object list to find the reported user.
394 defp get_reported(objects) do
395 Enum.reduce_while(objects, nil, fn ap_id, _ ->
396 with %User{} = user <- User.get_cached_by_ap_id(ap_id) do
404 def handle_incoming(data, options \\ [])
406 # Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them
408 def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} = data, _options) do
409 with context <- data["context"] || Utils.generate_context_id(),
410 content <- data["content"] || "",
411 %User{} = actor <- User.get_cached_by_ap_id(actor),
412 # Reduce the object list to find the reported user.
413 %User{} = account <- get_reported(objects),
414 # Remove the reported user from the object list.
415 statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do
422 additional: %{"cc" => [account.ap_id]}
424 |> ActivityPub.flag()
428 # disallow objects with bogus IDs
429 def handle_incoming(%{"id" => nil}, _options), do: :error
430 def handle_incoming(%{"id" => ""}, _options), do: :error
431 # length of https:// = 8, should validate better, but good enough for now.
432 def handle_incoming(%{"id" => id}, _options) when is_binary(id) and byte_size(id) < 8,
435 @doc "Rewrite misskey likes into EmojiReacts"
439 "_misskey_reaction" => reaction,
445 |> Map.put("type", "EmojiReact")
446 |> Map.put("content", reaction)
447 |> handle_incoming(options)
453 "_misskey_reaction" => reaction
458 |> Map.put("type", "EmojiReact")
459 |> Map.put("content", reaction)
460 |> handle_incoming(options)
464 %{"type" => "Create", "object" => %{"type" => objtype, "id" => obj_id}} = data,
467 when objtype in ~w{Question Answer Audio Video Event Article Note Page} do
468 fetch_options = Keyword.put(options, :depth, (options[:depth] || 0) + 1)
472 |> strip_internal_fields()
473 |> fix_type(fetch_options)
474 |> fix_in_reply_to(fetch_options)
475 |> fix_quote_url(fetch_options)
477 data = Map.put(data, "object", object)
478 options = Keyword.put(options, :local, false)
480 with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
481 nil <- Activity.get_create_by_object_ap_id(obj_id),
482 {:ok, activity, _} <- Pipeline.common_pipeline(data, options) do
485 %Activity{} = activity -> {:ok, activity}
490 def handle_incoming(%{"type" => type} = data, _options)
491 when type in ~w{Like EmojiReact Announce Add Remove} do
492 with :ok <- ObjectValidator.fetch_actor_and_object(data),
493 {:ok, activity, _meta} <- Pipeline.common_pipeline(data, local: false) do
502 %{"type" => type} = data,
505 when type in ~w{Update Block Follow Accept Reject} do
506 with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
507 {:ok, activity, _} <-
508 Pipeline.common_pipeline(data, local: false) do
514 %{"type" => "Delete"} = data,
517 with {:ok, activity, _} <-
518 Pipeline.common_pipeline(data, local: false) do
521 {:error, {:validate, _}} = e ->
522 # Check if we have a create activity for this
523 with {:ok, object_id} <- ObjectValidators.ObjectID.cast(data["object"]),
524 %Activity{data: %{"actor" => actor}} <-
525 Activity.create_by_object_ap_id(object_id) |> Repo.one(),
526 # We have one, insert a tombstone and retry
527 {:ok, tombstone_data, _} <- Builder.tombstone(actor, object_id),
528 {:ok, _tombstone} <- Object.create(tombstone_data) do
529 handle_incoming(data)
539 "object" => %{"type" => "Follow", "object" => followed},
545 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
546 {:ok, %User{} = follower} <- User.get_or_fetch_by_ap_id(follower),
547 {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
548 User.unfollow(follower, followed)
558 "object" => %{"type" => type}
562 when type in ["Like", "EmojiReact", "Announce", "Block"] do
563 with {:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
568 # For Undos that don't have the complete object attached, try to find it in our database.
576 when is_binary(object) do
577 with %Activity{data: data} <- Activity.get_by_ap_id(object) do
579 |> Map.put("object", data)
580 |> handle_incoming(options)
589 "actor" => origin_actor,
590 "object" => origin_actor,
591 "target" => target_actor
595 with %User{} = origin_user <- User.get_cached_by_ap_id(origin_actor),
596 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_actor),
597 true <- origin_actor in target_user.also_known_as do
598 ActivityPub.move(origin_user, target_user, false)
604 def handle_incoming(_, _), do: :error
606 @spec get_obj_helper(String.t(), Keyword.t()) :: {:ok, Object.t()} | nil
607 def get_obj_helper(id, options \\ []) do
608 options = Keyword.put(options, :fetch, true)
610 case Object.normalize(id, options) do
611 %Object{} = object -> {:ok, object}
616 @spec get_embedded_obj_helper(String.t() | Object.t(), User.t()) :: {:ok, Object.t()} | nil
617 def get_embedded_obj_helper(%{"attributedTo" => attributed_to, "id" => object_id} = data, %User{
620 when attributed_to == ap_id do
621 with {:ok, activity} <-
626 "actor" => attributed_to,
629 {:ok, Object.normalize(activity, fetch: false)}
631 _ -> get_obj_helper(object_id)
635 def get_embedded_obj_helper(object_id, _) do
636 get_obj_helper(object_id)
639 def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
640 with false <- String.starts_with?(in_reply_to, "http"),
641 {:ok, %{data: replied_to_object}} <- get_obj_helper(in_reply_to) do
642 Map.put(object, "inReplyTo", replied_to_object["external_url"] || in_reply_to)
648 def set_reply_to_uri(obj), do: obj
650 def set_quote_url(%{"quoteUri" => quote} = object) when is_binary(quote) do
651 Map.put(object, "quoteUrl", quote)
654 def set_quote_url(obj), do: obj
657 Serialized Mastodon-compatible `replies` collection containing _self-replies_.
658 Based on Mastodon's ActivityPub::NoteSerializer#replies.
660 def set_replies(obj_data) do
662 with limit when limit > 0 <-
663 Pleroma.Config.get([:activitypub, :note_replies_output_limit], 0),
664 %Object{} = object <- Object.get_cached_by_ap_id(obj_data["id"]) do
666 |> Object.self_replies()
667 |> select([o], fragment("?->>'id'", o.data))
674 set_replies(obj_data, replies_uris)
677 defp set_replies(obj, []) do
681 defp set_replies(obj, replies_uris) do
682 replies_collection = %{
683 "type" => "Collection",
684 "items" => replies_uris
687 Map.merge(obj, %{"replies" => replies_collection})
690 def replies(%{"replies" => %{"first" => %{"items" => items}}}) when not is_nil(items) do
694 def replies(%{"replies" => %{"items" => items}}) when not is_nil(items) do
698 def replies(_), do: []
700 # Prepares the object of an outgoing create activity.
701 def prepare_object(object) do
707 |> prepare_attachments
712 |> strip_internal_fields
713 |> strip_internal_tags
719 # internal -> Mastodon
722 def prepare_outgoing(%{"type" => activity_type, "object" => object_id} = data)
723 when activity_type in ["Create"] do
726 |> Object.normalize(fetch: false)
732 |> Map.put("object", object)
733 |> Map.merge(Utils.make_json_ld_header())
739 def prepare_outgoing(%{"type" => "Announce", "actor" => ap_id, "object" => object_id} = data) do
742 |> Object.normalize(fetch: false)
745 if Visibility.is_private?(object) && object.data["actor"] == ap_id do
746 data |> Map.put("object", object |> Map.get(:data) |> prepare_object)
748 data |> maybe_fix_object_url
753 |> strip_internal_fields
754 |> Map.merge(Utils.make_json_ld_header())
760 # Mastodon Accept/Reject requires a non-normalized object containing the actor URIs,
761 # because of course it does.
762 def prepare_outgoing(%{"type" => "Accept"} = data) do
763 with follow_activity <- Activity.normalize(data["object"]) do
765 "actor" => follow_activity.actor,
766 "object" => follow_activity.data["object"],
767 "id" => follow_activity.data["id"],
773 |> Map.put("object", object)
774 |> Map.merge(Utils.make_json_ld_header())
780 def prepare_outgoing(%{"type" => "Reject"} = data) do
781 with follow_activity <- Activity.normalize(data["object"]) do
783 "actor" => follow_activity.actor,
784 "object" => follow_activity.data["object"],
785 "id" => follow_activity.data["id"],
791 |> Map.put("object", object)
792 |> Map.merge(Utils.make_json_ld_header())
798 def prepare_outgoing(%{"type" => _type} = data) do
801 |> strip_internal_fields
802 |> maybe_fix_object_url
803 |> Map.merge(Utils.make_json_ld_header())
808 def maybe_fix_object_url(%{"object" => object} = data) when is_binary(object) do
809 with false <- String.starts_with?(object, "http"),
810 {:fetch, {:ok, relative_object}} <- {:fetch, get_obj_helper(object)},
811 %{data: %{"external_url" => external_url}} when not is_nil(external_url) <-
813 Map.put(data, "object", external_url)
816 Logger.error("Couldn't fetch #{object} #{inspect(e)}")
824 def maybe_fix_object_url(data), do: data
826 def add_hashtags(object) do
828 (object["tag"] || [])
830 # Expand internal representation tags into AS2 tags.
831 tag when is_binary(tag) ->
833 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
838 # Do not process tags which are already AS2 tag objects.
839 tag when is_map(tag) ->
843 Map.put(object, "tag", tags)
846 # TODO These should be added on our side on insertion, it doesn't make much
847 # sense to regenerate these all the time
848 def add_mention_tags(object) do
849 to = object["to"] || []
850 cc = object["cc"] || []
851 mentioned = User.get_users_from_set(to ++ cc, local_only: false)
853 mentions = Enum.map(mentioned, &build_mention_tag/1)
855 tags = object["tag"] || []
856 Map.put(object, "tag", tags ++ mentions)
859 defp build_mention_tag(%{ap_id: ap_id, nickname: nickname} = _) do
860 %{"type" => "Mention", "href" => ap_id, "name" => "@#{nickname}"}
863 def take_emoji_tags(%User{emoji: emoji}) do
866 |> Enum.map(&build_emoji_tag/1)
869 # TODO: we should probably send mtime instead of unix epoch time for updated
870 def add_emoji_tags(%{"emoji" => emoji} = object) do
871 tags = object["tag"] || []
873 out = Enum.map(emoji, &build_emoji_tag/1)
875 Map.put(object, "tag", tags ++ out)
878 def add_emoji_tags(object), do: object
880 defp build_emoji_tag({name, url}) do
882 "icon" => %{"url" => "#{URI.encode(url)}", "type" => "Image"},
883 "name" => ":" <> name <> ":",
885 "updated" => "1970-01-01T00:00:00Z",
890 def set_conversation(object) do
891 Map.put(object, "conversation", object["context"])
894 def set_type(%{"type" => "Answer"} = object) do
895 Map.put(object, "type", "Note")
898 def set_type(object), do: object
900 def add_attributed_to(object) do
901 attributed_to = object["attributedTo"] || object["actor"]
902 Map.put(object, "attributedTo", attributed_to)
905 def prepare_attachments(object) do
908 |> Map.get("attachment", [])
909 |> Enum.map(fn data ->
910 [%{"mediaType" => media_type, "href" => href} = url | _] = data["url"]
914 "mediaType" => media_type,
915 "name" => data["name"],
918 |> Maps.put_if_present("width", url["width"])
919 |> Maps.put_if_present("height", url["height"])
920 |> Maps.put_if_present("blurhash", data["blurhash"])
923 Map.put(object, "attachment", attachments)
926 def strip_internal_fields(object) do
927 Map.drop(object, Pleroma.Constants.object_internal_fields())
930 defp strip_internal_tags(%{"tag" => tags} = object) do
931 tags = Enum.filter(tags, fn x -> is_map(x) end)
933 Map.put(object, "tag", tags)
936 defp strip_internal_tags(object), do: object
938 def perform(:user_upgrade, user) do
939 # we pass a fake user so that the followers collection is stripped away
940 old_follower_address = User.ap_followers(%User{nickname: user.nickname})
944 where: ^old_follower_address in a.recipients,
949 "array_replace(?,?,?)",
951 ^old_follower_address,
952 ^user.follower_address
957 |> Repo.update_all([])
960 def upgrade_user_from_ap_id(ap_id) do
961 with %User{local: false} = user <- User.get_cached_by_ap_id(ap_id),
962 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id),
963 {:ok, user} <- update_user(user, data) do
964 {:ok, _pid} = Task.start(fn -> ActivityPub.pinned_fetch_task(user) end)
965 TransmogrifierWorker.enqueue("user_upgrade", %{"user_id" => user.id})
968 %User{} = user -> {:ok, user}
973 defp update_user(user, data) do
975 |> User.remote_user_changeset(data)
976 |> User.update_and_set_cache()
979 def maybe_fix_user_url(%{"url" => url} = data) when is_map(url) do
980 Map.put(data, "url", url["href"])
983 def maybe_fix_user_url(data), do: data
985 def maybe_fix_user_object(data), do: maybe_fix_user_url(data)