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)
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 defp prepare_in_reply_to(in_reply_to) do
173 is_bitstring(in_reply_to) ->
176 is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) ->
179 is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) ->
180 Enum.at(in_reply_to, 0)
187 def fix_context(object) do
188 context = object["context"] || object["conversation"] || Utils.generate_context_id()
191 |> Map.put("context", context)
192 |> Map.drop(["conversation"])
195 def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
197 Enum.map(attachment, fn data ->
200 is_list(data["url"]) -> List.first(data["url"])
201 is_map(data["url"]) -> data["url"]
207 is_map(url) && MIME.valid?(url["mediaType"]) -> url["mediaType"]
208 MIME.valid?(data["mediaType"]) -> data["mediaType"]
209 MIME.valid?(data["mimeType"]) -> data["mimeType"]
215 is_map(url) && is_binary(url["href"]) -> url["href"]
216 is_binary(data["url"]) -> data["url"]
217 is_binary(data["href"]) -> data["href"]
225 "type" => Map.get(url || %{}, "type", "Link")
227 |> Maps.put_if_present("mediaType", media_type)
230 "url" => [attachment_url],
231 "type" => data["type"] || "Document"
233 |> Maps.put_if_present("mediaType", media_type)
234 |> Maps.put_if_present("name", data["name"])
235 |> Maps.put_if_present("blurhash", data["blurhash"])
242 Map.put(object, "attachment", attachments)
245 def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
247 |> Map.put("attachment", [attachment])
251 def fix_attachments(object), do: object
253 def fix_url(%{"url" => url} = object) when is_map(url) do
254 Map.put(object, "url", url["href"])
257 def fix_url(%{"url" => url} = object) when is_list(url) do
258 first_element = Enum.at(url, 0)
262 is_bitstring(first_element) -> first_element
263 is_map(first_element) -> first_element["href"] || ""
267 Map.put(object, "url", url_string)
270 def fix_url(object), do: object
272 def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
275 |> Enum.filter(fn data -> is_map(data) and data["type"] == "Emoji" and data["icon"] end)
276 |> Enum.reduce(%{}, fn data, mapping ->
277 name = String.trim(data["name"], ":")
279 Map.put(mapping, name, data["icon"]["url"])
282 Map.put(object, "emoji", emoji)
285 def fix_emoji(%{"tag" => %{"type" => "Emoji"} = tag} = object) do
286 name = String.trim(tag["name"], ":")
287 emoji = %{name => tag["icon"]["url"]}
289 Map.put(object, "emoji", emoji)
292 def fix_emoji(object), do: object
294 def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
297 |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
299 %{"name" => "#" <> hashtag} -> String.downcase(hashtag)
300 %{"name" => hashtag} -> String.downcase(hashtag)
303 Map.put(object, "tag", tag ++ tags)
306 def fix_tag(%{"tag" => %{} = tag} = object) do
308 |> Map.put("tag", [tag])
312 def fix_tag(object), do: object
314 # content map usually only has one language so this will do for now.
315 def fix_content_map(%{"contentMap" => content_map} = object) do
316 content_groups = Map.to_list(content_map)
317 {_, content} = Enum.at(content_groups, 0)
319 Map.put(object, "content", content)
322 def fix_content_map(object), do: object
324 def fix_type(object, options \\ [])
326 def fix_type(%{"inReplyTo" => reply_id, "name" => _} = object, options)
327 when is_binary(reply_id) do
328 with true <- Federator.allowed_thread_distance?(options[:depth]),
329 {:ok, %{data: %{"type" => "Question"} = _} = _} <- get_obj_helper(reply_id, options) do
330 Map.put(object, "type", "Answer")
336 def fix_type(object, _), do: object
338 # Reduce the object list to find the reported user.
339 defp get_reported(objects) do
340 Enum.reduce_while(objects, nil, fn ap_id, _ ->
341 with %User{} = user <- User.get_cached_by_ap_id(ap_id) do
349 # Compatibility wrapper for Mastodon votes
350 defp handle_create(%{"object" => %{"type" => "Answer"}} = data, _user) do
351 handle_incoming(data)
354 defp handle_create(%{"object" => object} = data, user) do
359 context: object["context"],
361 published: data["published"],
369 |> ActivityPub.create()
372 def handle_incoming(data, options \\ [])
374 # Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them
376 def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} = data, _options) do
377 with context <- data["context"] || Utils.generate_context_id(),
378 content <- data["content"] || "",
379 %User{} = actor <- User.get_cached_by_ap_id(actor),
380 # Reduce the object list to find the reported user.
381 %User{} = account <- get_reported(objects),
382 # Remove the reported user from the object list.
383 statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do
390 additional: %{"cc" => [account.ap_id]}
392 |> ActivityPub.flag()
396 # disallow objects with bogus IDs
397 def handle_incoming(%{"id" => nil}, _options), do: :error
398 def handle_incoming(%{"id" => ""}, _options), do: :error
399 # length of https:// = 8, should validate better, but good enough for now.
400 def handle_incoming(%{"id" => id}, _options) when is_binary(id) and byte_size(id) < 8,
403 # TODO: validate those with a Ecto scheme
407 %{"type" => "Create", "object" => %{"type" => "Page"} = object} = data,
410 actor = Containment.get_actor(data)
412 with nil <- Activity.get_create_by_object_ap_id(object["id"]),
413 {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(actor) do
416 |> Map.put("object", fix_object(object, options))
417 |> Map.put("actor", actor)
420 with {:ok, created_activity} <- handle_create(data, user) do
421 reply_depth = (options[:depth] || 0) + 1
423 if Federator.allowed_thread_distance?(reply_depth) do
424 for reply_id <- replies(object) do
425 Pleroma.Workers.RemoteFetcherWorker.enqueue("fetch_remote", %{
427 "depth" => reply_depth
432 {:ok, created_activity}
435 %Activity{} = activity -> {:ok, activity}
441 %{"type" => "Listen", "object" => %{"type" => "Audio"} = object} = data,
444 actor = Containment.get_actor(data)
447 Map.put(data, "actor", actor)
450 with {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
451 reply_depth = (options[:depth] || 0) + 1
452 options = Keyword.put(options, :depth, reply_depth)
453 object = fix_object(object, options)
461 published: data["published"],
462 additional: Map.take(data, ["cc", "id"])
465 ActivityPub.listen(params)
471 @misskey_reactions %{
485 @doc "Rewrite misskey likes into EmojiReacts"
489 "_misskey_reaction" => reaction
494 |> Map.put("type", "EmojiReact")
495 |> Map.put("content", @misskey_reactions[reaction] || reaction)
496 |> handle_incoming(options)
500 %{"type" => "Create", "object" => %{"type" => objtype, "id" => obj_id}} = data,
503 when objtype in ~w{Question Answer ChatMessage Audio Video Event Article Note} do
504 data = Map.put(data, "object", strip_internal_fields(data["object"]))
505 options = Keyword.put(options, :local, false)
507 with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
508 nil <- Activity.get_create_by_object_ap_id(obj_id),
509 {:ok, activity, _} <- Pipeline.common_pipeline(data, options) do
512 %Activity{} = activity -> {:ok, activity}
517 def handle_incoming(%{"type" => type} = data, _options)
518 when type in ~w{Like EmojiReact Announce} do
519 with :ok <- ObjectValidator.fetch_actor_and_object(data),
520 {:ok, activity, _meta} <-
521 Pipeline.common_pipeline(data, local: false) do
529 %{"type" => type} = data,
532 when type in ~w{Update Block Follow Accept Reject} do
533 with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
534 {:ok, activity, _} <-
535 Pipeline.common_pipeline(data, local: false) do
541 %{"type" => "Delete"} = data,
544 with {:ok, activity, _} <-
545 Pipeline.common_pipeline(data, local: false) do
548 {:error, {:validate, _}} = e ->
549 # Check if we have a create activity for this
550 with {:ok, object_id} <- ObjectValidators.ObjectID.cast(data["object"]),
551 %Activity{data: %{"actor" => actor}} <-
552 Activity.create_by_object_ap_id(object_id) |> Repo.one(),
553 # We have one, insert a tombstone and retry
554 {:ok, tombstone_data, _} <- Builder.tombstone(actor, object_id),
555 {:ok, _tombstone} <- Object.create(tombstone_data) do
556 handle_incoming(data)
566 "object" => %{"type" => "Follow", "object" => followed},
572 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
573 {:ok, %User{} = follower} <- User.get_or_fetch_by_ap_id(follower),
574 {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
575 User.unfollow(follower, followed)
585 "object" => %{"type" => type}
589 when type in ["Like", "EmojiReact", "Announce", "Block"] do
590 with {:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
595 # For Undos that don't have the complete object attached, try to find it in our database.
603 when is_binary(object) do
604 with %Activity{data: data} <- Activity.get_by_ap_id(object) do
606 |> Map.put("object", data)
607 |> handle_incoming(options)
616 "actor" => origin_actor,
617 "object" => origin_actor,
618 "target" => target_actor
622 with %User{} = origin_user <- User.get_cached_by_ap_id(origin_actor),
623 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_actor),
624 true <- origin_actor in target_user.also_known_as do
625 ActivityPub.move(origin_user, target_user, false)
631 def handle_incoming(_, _), do: :error
633 @spec get_obj_helper(String.t(), Keyword.t()) :: {:ok, Object.t()} | nil
634 def get_obj_helper(id, options \\ []) do
635 options = Keyword.put(options, :fetch, true)
637 case Object.normalize(id, options) do
638 %Object{} = object -> {:ok, object}
643 @spec get_embedded_obj_helper(String.t() | Object.t(), User.t()) :: {:ok, Object.t()} | nil
644 def get_embedded_obj_helper(%{"attributedTo" => attributed_to, "id" => object_id} = data, %User{
647 when attributed_to == ap_id do
648 with {:ok, activity} <-
653 "actor" => attributed_to,
656 {:ok, Object.normalize(activity, fetch: false)}
658 _ -> get_obj_helper(object_id)
662 def get_embedded_obj_helper(object_id, _) do
663 get_obj_helper(object_id)
666 def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
667 with false <- String.starts_with?(in_reply_to, "http"),
668 {:ok, %{data: replied_to_object}} <- get_obj_helper(in_reply_to) do
669 Map.put(object, "inReplyTo", replied_to_object["external_url"] || in_reply_to)
675 def set_reply_to_uri(obj), do: obj
678 Serialized Mastodon-compatible `replies` collection containing _self-replies_.
679 Based on Mastodon's ActivityPub::NoteSerializer#replies.
681 def set_replies(obj_data) do
683 with limit when limit > 0 <-
684 Pleroma.Config.get([:activitypub, :note_replies_output_limit], 0),
685 %Object{} = object <- Object.get_cached_by_ap_id(obj_data["id"]) do
687 |> Object.self_replies()
688 |> select([o], fragment("?->>'id'", o.data))
695 set_replies(obj_data, replies_uris)
698 defp set_replies(obj, []) do
702 defp set_replies(obj, replies_uris) do
703 replies_collection = %{
704 "type" => "Collection",
705 "items" => replies_uris
708 Map.merge(obj, %{"replies" => replies_collection})
711 def replies(%{"replies" => %{"first" => %{"items" => items}}}) when not is_nil(items) do
715 def replies(%{"replies" => %{"items" => items}}) when not is_nil(items) do
719 def replies(_), do: []
721 # Prepares the object of an outgoing create activity.
722 def prepare_object(object) do
728 |> prepare_attachments
732 |> strip_internal_fields
733 |> strip_internal_tags
739 # internal -> Mastodon
742 def prepare_outgoing(%{"type" => activity_type, "object" => object_id} = data)
743 when activity_type in ["Create", "Listen"] do
746 |> Object.normalize(fetch: false)
752 |> Map.put("object", object)
753 |> Map.merge(Utils.make_json_ld_header())
759 def prepare_outgoing(%{"type" => "Announce", "actor" => ap_id, "object" => object_id} = data) do
762 |> Object.normalize(fetch: false)
765 if Visibility.is_private?(object) && object.data["actor"] == ap_id do
766 data |> Map.put("object", object |> Map.get(:data) |> prepare_object)
768 data |> maybe_fix_object_url
773 |> strip_internal_fields
774 |> Map.merge(Utils.make_json_ld_header())
780 # Mastodon Accept/Reject requires a non-normalized object containing the actor URIs,
781 # because of course it does.
782 def prepare_outgoing(%{"type" => "Accept"} = data) do
783 with follow_activity <- Activity.normalize(data["object"]) do
785 "actor" => follow_activity.actor,
786 "object" => follow_activity.data["object"],
787 "id" => follow_activity.data["id"],
793 |> Map.put("object", object)
794 |> Map.merge(Utils.make_json_ld_header())
800 def prepare_outgoing(%{"type" => "Reject"} = data) do
801 with follow_activity <- Activity.normalize(data["object"]) do
803 "actor" => follow_activity.actor,
804 "object" => follow_activity.data["object"],
805 "id" => follow_activity.data["id"],
811 |> Map.put("object", object)
812 |> Map.merge(Utils.make_json_ld_header())
818 def prepare_outgoing(%{"type" => _type} = data) do
821 |> strip_internal_fields
822 |> maybe_fix_object_url
823 |> Map.merge(Utils.make_json_ld_header())
828 def maybe_fix_object_url(%{"object" => object} = data) when is_binary(object) do
829 with false <- String.starts_with?(object, "http"),
830 {:fetch, {:ok, relative_object}} <- {:fetch, get_obj_helper(object)},
831 %{data: %{"external_url" => external_url}} when not is_nil(external_url) <-
833 Map.put(data, "object", external_url)
836 Logger.error("Couldn't fetch #{object} #{inspect(e)}")
844 def maybe_fix_object_url(data), do: data
846 def add_hashtags(object) do
848 (object["tag"] || [])
850 # Expand internal representation tags into AS2 tags.
851 tag when is_binary(tag) ->
853 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
858 # Do not process tags which are already AS2 tag objects.
859 tag when is_map(tag) ->
863 Map.put(object, "tag", tags)
866 # TODO These should be added on our side on insertion, it doesn't make much
867 # sense to regenerate these all the time
868 def add_mention_tags(object) do
869 to = object["to"] || []
870 cc = object["cc"] || []
871 mentioned = User.get_users_from_set(to ++ cc, local_only: false)
873 mentions = Enum.map(mentioned, &build_mention_tag/1)
875 tags = object["tag"] || []
876 Map.put(object, "tag", tags ++ mentions)
879 defp build_mention_tag(%{ap_id: ap_id, nickname: nickname} = _) do
880 %{"type" => "Mention", "href" => ap_id, "name" => "@#{nickname}"}
883 def take_emoji_tags(%User{emoji: emoji}) do
886 |> Enum.map(&build_emoji_tag/1)
889 # TODO: we should probably send mtime instead of unix epoch time for updated
890 def add_emoji_tags(%{"emoji" => emoji} = object) do
891 tags = object["tag"] || []
893 out = Enum.map(emoji, &build_emoji_tag/1)
895 Map.put(object, "tag", tags ++ out)
898 def add_emoji_tags(object), do: object
900 defp build_emoji_tag({name, url}) do
902 "icon" => %{"url" => "#{URI.encode(url)}", "type" => "Image"},
903 "name" => ":" <> name <> ":",
905 "updated" => "1970-01-01T00:00:00Z",
910 def set_conversation(object) do
911 Map.put(object, "conversation", object["context"])
914 def set_type(%{"type" => "Answer"} = object) do
915 Map.put(object, "type", "Note")
918 def set_type(object), do: object
920 def add_attributed_to(object) do
921 attributed_to = object["attributedTo"] || object["actor"]
922 Map.put(object, "attributedTo", attributed_to)
926 def prepare_attachments(%{"type" => "ChatMessage"} = object), do: object
928 def prepare_attachments(object) do
931 |> Map.get("attachment", [])
932 |> Enum.map(fn data ->
933 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
937 "mediaType" => media_type,
938 "name" => data["name"],
943 Map.put(object, "attachment", attachments)
946 def strip_internal_fields(object) do
947 Map.drop(object, Pleroma.Constants.object_internal_fields())
950 defp strip_internal_tags(%{"tag" => tags} = object) do
951 tags = Enum.filter(tags, fn x -> is_map(x) end)
953 Map.put(object, "tag", tags)
956 defp strip_internal_tags(object), do: object
958 def perform(:user_upgrade, user) do
959 # we pass a fake user so that the followers collection is stripped away
960 old_follower_address = User.ap_followers(%User{nickname: user.nickname})
964 where: ^old_follower_address in a.recipients,
969 "array_replace(?,?,?)",
971 ^old_follower_address,
972 ^user.follower_address
977 |> Repo.update_all([])
980 def upgrade_user_from_ap_id(ap_id) do
981 with %User{local: false} = user <- User.get_cached_by_ap_id(ap_id),
982 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id),
983 {:ok, user} <- update_user(user, data) do
984 TransmogrifierWorker.enqueue("user_upgrade", %{"user_id" => user.id})
987 %User{} = user -> {:ok, user}
992 defp update_user(user, data) do
994 |> User.remote_user_changeset(data)
995 |> User.update_and_set_cache()
998 def maybe_fix_user_url(%{"url" => url} = data) when is_map(url) do
999 Map.put(data, "url", url["href"])
1002 def maybe_fix_user_url(data), do: data
1004 def maybe_fix_user_object(data), do: maybe_fix_user_url(data)