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)
48 def fix_summary(%{"summary" => nil} = object) do
49 Map.put(object, "summary", "")
52 def fix_summary(%{"summary" => _} = object) do
53 # summary is present, nothing to do
57 def fix_summary(object), do: Map.put(object, "summary", "")
59 def fix_addressing_list(map, field) do
64 Map.put(map, field, Enum.filter(addrs, &is_binary/1))
67 Map.put(map, field, [addrs])
70 Map.put(map, field, [])
74 # if directMessage flag is set to true, leave the addressing alone
75 def fix_explicit_addressing(%{"directMessage" => true} = object, _follower_collection),
78 def fix_explicit_addressing(%{"to" => to, "cc" => cc} = object, follower_collection) do
80 Utils.determine_explicit_mentions(object) ++
81 [Pleroma.Constants.as_public(), follower_collection]
83 explicit_to = Enum.filter(to, fn x -> x in explicit_mentions end)
84 explicit_cc = Enum.filter(to, fn x -> x not in explicit_mentions end)
89 |> Enum.reject(fn x -> String.ends_with?(x, "/followers") and x != follower_collection end)
93 |> Map.put("to", explicit_to)
94 |> Map.put("cc", final_cc)
97 # if as:Public is addressed, then make sure the followers collection is also addressed
98 # so that the activities will be delivered to local users.
99 def fix_implicit_addressing(%{"to" => to, "cc" => cc} = object, followers_collection) do
100 recipients = to ++ cc
102 if followers_collection not in recipients do
104 Pleroma.Constants.as_public() in cc ->
105 to = to ++ [followers_collection]
106 Map.put(object, "to", to)
108 Pleroma.Constants.as_public() in to ->
109 cc = cc ++ [followers_collection]
110 Map.put(object, "cc", cc)
120 def fix_addressing(object) do
121 {:ok, %User{follower_address: follower_collection}} =
123 |> Containment.get_actor()
124 |> User.get_or_fetch_by_ap_id()
127 |> fix_addressing_list("to")
128 |> fix_addressing_list("cc")
129 |> fix_addressing_list("bto")
130 |> fix_addressing_list("bcc")
131 |> fix_explicit_addressing(follower_collection)
132 |> fix_implicit_addressing(follower_collection)
135 def fix_actor(%{"attributedTo" => actor} = object) do
136 actor = Containment.get_actor(%{"actor" => actor})
138 # TODO: Remove actor field for Objects
140 |> Map.put("actor", actor)
141 |> Map.put("attributedTo", actor)
144 def fix_in_reply_to(object, options \\ [])
146 def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object, options)
147 when not is_nil(in_reply_to) do
148 in_reply_to_id = prepare_in_reply_to(in_reply_to)
149 depth = (options[:depth] || 0) + 1
151 if Federator.allowed_thread_distance?(depth) do
152 with {:ok, replied_object} <- get_obj_helper(in_reply_to_id, options),
153 %Activity{} <- Activity.get_create_by_object_ap_id(replied_object.data["id"]) do
155 |> Map.put("inReplyTo", replied_object.data["id"])
156 |> Map.put("context", replied_object.data["context"] || object["conversation"])
157 |> Map.drop(["conversation", "inReplyToAtomUri"])
160 Logger.warn("Couldn't fetch #{inspect(in_reply_to_id)}, error: #{inspect(e)}")
168 def fix_in_reply_to(object, _options), do: object
170 defp prepare_in_reply_to(in_reply_to) do
172 is_bitstring(in_reply_to) ->
175 is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) ->
178 is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) ->
179 Enum.at(in_reply_to, 0)
186 def fix_context(object) do
187 context = object["context"] || object["conversation"] || Utils.generate_context_id()
190 |> Map.put("context", context)
191 |> Map.drop(["conversation"])
194 def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
196 Enum.map(attachment, fn data ->
199 is_list(data["url"]) -> List.first(data["url"])
200 is_map(data["url"]) -> data["url"]
206 is_map(url) && MIME.extensions(url["mediaType"]) != [] ->
209 is_bitstring(data["mediaType"]) && MIME.extensions(data["mediaType"]) != [] ->
212 is_bitstring(data["mimeType"]) && MIME.extensions(data["mimeType"]) != [] ->
221 is_map(url) && is_binary(url["href"]) -> url["href"]
222 is_binary(data["url"]) -> data["url"]
223 is_binary(data["href"]) -> data["href"]
231 "type" => Map.get(url || %{}, "type", "Link")
233 |> Maps.put_if_present("mediaType", media_type)
234 |> Maps.put_if_present("width", (url || %{})["width"] || data["width"])
235 |> Maps.put_if_present("height", (url || %{})["height"] || data["height"])
238 "url" => [attachment_url],
239 "type" => data["type"] || "Document"
241 |> Maps.put_if_present("mediaType", media_type)
242 |> Maps.put_if_present("name", data["name"])
243 |> Maps.put_if_present("blurhash", data["blurhash"])
250 Map.put(object, "attachment", attachments)
253 def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
255 |> Map.put("attachment", [attachment])
259 def fix_attachments(object), do: object
261 def fix_url(%{"url" => url} = object) when is_map(url) do
262 Map.put(object, "url", url["href"])
265 def fix_url(%{"url" => url} = object) when is_list(url) do
266 first_element = Enum.at(url, 0)
270 is_bitstring(first_element) -> first_element
271 is_map(first_element) -> first_element["href"] || ""
275 Map.put(object, "url", url_string)
278 def fix_url(object), do: object
280 def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
283 |> Enum.filter(fn data -> is_map(data) and data["type"] == "Emoji" and data["icon"] end)
284 |> Enum.reduce(%{}, fn data, mapping ->
285 name = String.trim(data["name"], ":")
287 Map.put(mapping, name, data["icon"]["url"])
290 Map.put(object, "emoji", emoji)
293 def fix_emoji(%{"tag" => %{"type" => "Emoji"} = tag} = object) do
294 name = String.trim(tag["name"], ":")
295 emoji = %{name => tag["icon"]["url"]}
297 Map.put(object, "emoji", emoji)
300 def fix_emoji(object), do: object
302 def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
305 |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
307 %{"name" => "#" <> hashtag} -> String.downcase(hashtag)
308 %{"name" => hashtag} -> String.downcase(hashtag)
311 Map.put(object, "tag", tag ++ tags)
314 def fix_tag(%{"tag" => %{} = tag} = object) do
316 |> Map.put("tag", [tag])
320 def fix_tag(object), do: object
322 # content map usually only has one language so this will do for now.
323 def fix_content_map(%{"contentMap" => content_map} = object) do
324 content_groups = Map.to_list(content_map)
325 {_, content} = Enum.at(content_groups, 0)
327 Map.put(object, "content", content)
330 def fix_content_map(object), do: object
332 defp fix_type(%{"type" => "Note", "inReplyTo" => reply_id, "name" => _} = object, options)
333 when is_binary(reply_id) do
334 options = Keyword.put(options, :fetch, true)
336 with %Object{data: %{"type" => "Question"}} <- Object.normalize(reply_id, options) do
337 Map.put(object, "type", "Answer")
343 defp fix_type(object, _options), do: object
345 # Reduce the object list to find the reported user.
346 defp get_reported(objects) do
347 Enum.reduce_while(objects, nil, fn ap_id, _ ->
348 with %User{} = user <- User.get_cached_by_ap_id(ap_id) do
356 def handle_incoming(data, options \\ [])
358 # Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them
360 def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} = data, _options) do
361 with context <- data["context"] || Utils.generate_context_id(),
362 content <- data["content"] || "",
363 %User{} = actor <- User.get_cached_by_ap_id(actor),
364 # Reduce the object list to find the reported user.
365 %User{} = account <- get_reported(objects),
366 # Remove the reported user from the object list.
367 statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do
374 additional: %{"cc" => [account.ap_id]}
376 |> ActivityPub.flag()
380 # disallow objects with bogus IDs
381 def handle_incoming(%{"id" => nil}, _options), do: :error
382 def handle_incoming(%{"id" => ""}, _options), do: :error
383 # length of https:// = 8, should validate better, but good enough for now.
384 def handle_incoming(%{"id" => id}, _options) when is_binary(id) and byte_size(id) < 8,
388 %{"type" => "Listen", "object" => %{"type" => "Audio"} = object} = data,
391 actor = Containment.get_actor(data)
394 Map.put(data, "actor", actor)
397 with {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
398 reply_depth = (options[:depth] || 0) + 1
399 options = Keyword.put(options, :depth, reply_depth)
400 object = fix_object(object, options)
408 published: data["published"],
409 additional: Map.take(data, ["cc", "id"])
412 ActivityPub.listen(params)
418 @doc "Rewrite misskey likes into EmojiReacts"
422 "_misskey_reaction" => reaction,
429 |> Map.put("type", "EmojiReact")
430 |> Map.put("content", reaction)
431 |> handle_incoming(options)
437 "_misskey_reaction" => reaction,
442 |> Map.put("type", "EmojiReact")
443 |> Map.put("content", reaction)
444 |> handle_incoming(options)
448 %{"type" => "Create", "object" => %{"type" => objtype, "id" => obj_id}} = data,
451 when objtype in ~w{Question Answer ChatMessage Audio Video Event Article Note Page} do
452 fetch_options = Keyword.put(options, :depth, (options[:depth] || 0) + 1)
456 |> strip_internal_fields()
457 |> fix_type(fetch_options)
458 |> fix_in_reply_to(fetch_options)
460 data = Map.put(data, "object", object)
461 options = Keyword.put(options, :local, false)
463 with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
464 nil <- Activity.get_create_by_object_ap_id(obj_id),
465 {:ok, activity, _} <- Pipeline.common_pipeline(data, options) do
468 %Activity{} = activity -> {:ok, activity}
473 def handle_incoming(%{"type" => type} = data, _options)
474 when type in ~w{Like EmojiReact Announce Add Remove} do
475 with :ok <- ObjectValidator.fetch_actor_and_object(data),
476 {:ok, activity, _meta} <- Pipeline.common_pipeline(data, local: false) do
485 %{"type" => type} = data,
488 when type in ~w{Update Block Follow Accept Reject} do
489 with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
490 {:ok, activity, _} <-
491 Pipeline.common_pipeline(data, local: false) do
497 %{"type" => "Delete"} = data,
500 with {:ok, activity, _} <-
501 Pipeline.common_pipeline(data, local: false) do
504 {:error, {:validate, _}} = e ->
505 # Check if we have a create activity for this
506 with {:ok, object_id} <- ObjectValidators.ObjectID.cast(data["object"]),
507 %Activity{data: %{"actor" => actor}} <-
508 Activity.create_by_object_ap_id(object_id) |> Repo.one(),
509 # We have one, insert a tombstone and retry
510 {:ok, tombstone_data, _} <- Builder.tombstone(actor, object_id),
511 {:ok, _tombstone} <- Object.create(tombstone_data) do
512 handle_incoming(data)
522 "object" => %{"type" => "Follow", "object" => followed},
528 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
529 {:ok, %User{} = follower} <- User.get_or_fetch_by_ap_id(follower),
530 {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
531 User.unfollow(follower, followed)
541 "object" => %{"type" => type}
545 when type in ["Like", "EmojiReact", "Announce", "Block"] do
546 with {:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
551 # For Undos that don't have the complete object attached, try to find it in our database.
559 when is_binary(object) do
560 with %Activity{data: data} <- Activity.get_by_ap_id(object) do
562 |> Map.put("object", data)
563 |> handle_incoming(options)
572 "actor" => origin_actor,
573 "object" => origin_actor,
574 "target" => target_actor
578 with %User{} = origin_user <- User.get_cached_by_ap_id(origin_actor),
579 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_actor),
580 true <- origin_actor in target_user.also_known_as do
581 ActivityPub.move(origin_user, target_user, false)
587 def handle_incoming(_, _), do: :error
589 @spec get_obj_helper(String.t(), Keyword.t()) :: {:ok, Object.t()} | nil
590 def get_obj_helper(id, options \\ []) do
591 options = Keyword.put(options, :fetch, true)
593 case Object.normalize(id, options) do
594 %Object{} = object -> {:ok, object}
599 @spec get_embedded_obj_helper(String.t() | Object.t(), User.t()) :: {:ok, Object.t()} | nil
600 def get_embedded_obj_helper(%{"attributedTo" => attributed_to, "id" => object_id} = data, %User{
603 when attributed_to == ap_id do
604 with {:ok, activity} <-
609 "actor" => attributed_to,
612 {:ok, Object.normalize(activity, fetch: false)}
614 _ -> get_obj_helper(object_id)
618 def get_embedded_obj_helper(object_id, _) do
619 get_obj_helper(object_id)
622 def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
623 with false <- String.starts_with?(in_reply_to, "http"),
624 {:ok, %{data: replied_to_object}} <- get_obj_helper(in_reply_to) do
625 Map.put(object, "inReplyTo", replied_to_object["external_url"] || in_reply_to)
631 def set_reply_to_uri(obj), do: obj
634 Serialized Mastodon-compatible `replies` collection containing _self-replies_.
635 Based on Mastodon's ActivityPub::NoteSerializer#replies.
637 def set_replies(obj_data) do
639 with limit when limit > 0 <-
640 Pleroma.Config.get([:activitypub, :note_replies_output_limit], 0),
641 %Object{} = object <- Object.get_cached_by_ap_id(obj_data["id"]) do
643 |> Object.self_replies()
644 |> select([o], fragment("?->>'id'", o.data))
651 set_replies(obj_data, replies_uris)
654 defp set_replies(obj, []) do
658 defp set_replies(obj, replies_uris) do
659 replies_collection = %{
660 "type" => "Collection",
661 "items" => replies_uris
664 Map.merge(obj, %{"replies" => replies_collection})
667 def replies(%{"replies" => %{"first" => %{"items" => items}}}) when not is_nil(items) do
671 def replies(%{"replies" => %{"items" => items}}) when not is_nil(items) do
675 def replies(_), do: []
677 # Prepares the object of an outgoing create activity.
678 def prepare_object(object) do
684 |> prepare_attachments
688 |> strip_internal_fields
689 |> strip_internal_tags
695 # internal -> Mastodon
698 def prepare_outgoing(%{"type" => activity_type, "object" => object_id} = data)
699 when activity_type in ["Create", "Listen"] do
702 |> Object.normalize(fetch: false)
708 |> Map.put("object", object)
709 |> Map.merge(Utils.make_json_ld_header())
715 def prepare_outgoing(%{"type" => "Announce", "actor" => ap_id, "object" => object_id} = data) do
718 |> Object.normalize(fetch: false)
721 if Visibility.is_private?(object) && object.data["actor"] == ap_id do
722 data |> Map.put("object", object |> Map.get(:data) |> prepare_object)
724 data |> maybe_fix_object_url
729 |> strip_internal_fields
730 |> Map.merge(Utils.make_json_ld_header())
736 # Mastodon Accept/Reject requires a non-normalized object containing the actor URIs,
737 # because of course it does.
738 def prepare_outgoing(%{"type" => "Accept"} = data) do
739 with follow_activity <- Activity.normalize(data["object"]) do
741 "actor" => follow_activity.actor,
742 "object" => follow_activity.data["object"],
743 "id" => follow_activity.data["id"],
749 |> Map.put("object", object)
750 |> Map.merge(Utils.make_json_ld_header())
756 def prepare_outgoing(%{"type" => "Reject"} = data) do
757 with follow_activity <- Activity.normalize(data["object"]) do
759 "actor" => follow_activity.actor,
760 "object" => follow_activity.data["object"],
761 "id" => follow_activity.data["id"],
767 |> Map.put("object", object)
768 |> Map.merge(Utils.make_json_ld_header())
774 def prepare_outgoing(%{"type" => _type} = data) do
777 |> strip_internal_fields
778 |> maybe_fix_object_url
779 |> Map.merge(Utils.make_json_ld_header())
784 def maybe_fix_object_url(%{"object" => object} = data) when is_binary(object) do
785 with false <- String.starts_with?(object, "http"),
786 {:fetch, {:ok, relative_object}} <- {:fetch, get_obj_helper(object)},
787 %{data: %{"external_url" => external_url}} when not is_nil(external_url) <-
789 Map.put(data, "object", external_url)
792 Logger.error("Couldn't fetch #{object} #{inspect(e)}")
800 def maybe_fix_object_url(data), do: data
802 def add_hashtags(object) do
804 (object["tag"] || [])
806 # Expand internal representation tags into AS2 tags.
807 tag when is_binary(tag) ->
809 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
814 # Do not process tags which are already AS2 tag objects.
815 tag when is_map(tag) ->
819 Map.put(object, "tag", tags)
822 # TODO These should be added on our side on insertion, it doesn't make much
823 # sense to regenerate these all the time
824 def add_mention_tags(object) do
825 to = object["to"] || []
826 cc = object["cc"] || []
827 mentioned = User.get_users_from_set(to ++ cc, local_only: false)
829 mentions = Enum.map(mentioned, &build_mention_tag/1)
831 tags = object["tag"] || []
832 Map.put(object, "tag", tags ++ mentions)
835 defp build_mention_tag(%{ap_id: ap_id, nickname: nickname} = _) do
836 %{"type" => "Mention", "href" => ap_id, "name" => "@#{nickname}"}
839 def take_emoji_tags(%User{emoji: emoji}) do
842 |> Enum.map(&build_emoji_tag/1)
845 # TODO: we should probably send mtime instead of unix epoch time for updated
846 def add_emoji_tags(%{"emoji" => emoji} = object) do
847 tags = object["tag"] || []
849 out = Enum.map(emoji, &build_emoji_tag/1)
851 Map.put(object, "tag", tags ++ out)
854 def add_emoji_tags(object), do: object
856 defp build_emoji_tag({name, url}) do
858 "icon" => %{"url" => "#{URI.encode(url)}", "type" => "Image"},
859 "name" => ":" <> name <> ":",
861 "updated" => "1970-01-01T00:00:00Z",
866 def set_conversation(object) do
867 Map.put(object, "conversation", object["context"])
870 def set_type(%{"type" => "Answer"} = object) do
871 Map.put(object, "type", "Note")
874 def set_type(object), do: object
876 def add_attributed_to(object) do
877 attributed_to = object["attributedTo"] || object["actor"]
878 Map.put(object, "attributedTo", attributed_to)
882 def prepare_attachments(%{"type" => "ChatMessage"} = object), do: object
884 def prepare_attachments(object) do
887 |> Map.get("attachment", [])
888 |> Enum.map(fn data ->
889 [%{"mediaType" => media_type, "href" => href} = url | _] = data["url"]
893 "mediaType" => media_type,
894 "name" => data["name"],
897 |> Maps.put_if_present("width", url["width"])
898 |> Maps.put_if_present("height", url["height"])
899 |> Maps.put_if_present("blurhash", data["blurhash"])
902 Map.put(object, "attachment", attachments)
905 def strip_internal_fields(object) do
906 Map.drop(object, Pleroma.Constants.object_internal_fields())
909 defp strip_internal_tags(%{"tag" => tags} = object) do
910 tags = Enum.filter(tags, fn x -> is_map(x) end)
912 Map.put(object, "tag", tags)
915 defp strip_internal_tags(object), do: object
917 def perform(:user_upgrade, user) do
918 # we pass a fake user so that the followers collection is stripped away
919 old_follower_address = User.ap_followers(%User{nickname: user.nickname})
923 where: ^old_follower_address in a.recipients,
928 "array_replace(?,?,?)",
930 ^old_follower_address,
931 ^user.follower_address
936 |> Repo.update_all([])
939 def upgrade_user_from_ap_id(ap_id) do
940 with %User{local: false} = user <- User.get_cached_by_ap_id(ap_id),
941 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id),
942 {:ok, user} <- update_user(user, data) do
943 {:ok, _pid} = Task.start(fn -> ActivityPub.pinned_fetch_task(user) end)
944 TransmogrifierWorker.enqueue("user_upgrade", %{"user_id" => user.id})
947 %User{} = user -> {:ok, user}
952 defp update_user(user, data) do
954 |> User.remote_user_changeset(data)
955 |> User.update_and_set_cache()
958 def maybe_fix_user_url(%{"url" => url} = data) when is_map(url) do
959 Map.put(data, "url", url["href"])
962 def maybe_fix_user_url(data), do: data
964 def maybe_fix_user_object(data), do: maybe_fix_user_url(data)