1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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.EarmarkRenderer
11 alias Pleroma.EctoType.ActivityPub.ObjectValidators
12 alias Pleroma.FollowingRelationship
14 alias Pleroma.Notification
16 alias Pleroma.Object.Containment
19 alias Pleroma.Web.ActivityPub.ActivityPub
20 alias Pleroma.Web.ActivityPub.Builder
21 alias Pleroma.Web.ActivityPub.ObjectValidator
22 alias Pleroma.Web.ActivityPub.Pipeline
23 alias Pleroma.Web.ActivityPub.Utils
24 alias Pleroma.Web.ActivityPub.Visibility
25 alias Pleroma.Web.Federator
26 alias Pleroma.Workers.TransmogrifierWorker
31 require Pleroma.Constants
34 Modifies an incoming AP object (mastodon format) to our internal format.
36 def fix_object(object, options \\ []) do
38 |> strip_internal_fields
43 |> fix_in_reply_to(options)
53 def fix_summary(%{"summary" => nil} = object) do
54 Map.put(object, "summary", "")
57 def fix_summary(%{"summary" => _} = object) do
58 # summary is present, nothing to do
62 def fix_summary(object), do: Map.put(object, "summary", "")
64 def fix_addressing_list(map, field) do
69 Map.put(map, field, Enum.filter(addrs, &is_binary/1))
72 Map.put(map, field, [addrs])
75 Map.put(map, field, [])
79 def fix_explicit_addressing(
80 %{"to" => to, "cc" => cc} = object,
84 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)
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 def fix_explicit_addressing(object, _explicit_mentions, _followers_collection), do: object
100 # if directMessage flag is set to true, leave the addressing alone
101 def fix_explicit_addressing(%{"directMessage" => true} = object), do: object
103 def fix_explicit_addressing(object) do
104 explicit_mentions = Utils.determine_explicit_mentions(object)
106 %User{follower_address: follower_collection} =
108 |> Containment.get_actor()
109 |> User.get_cached_by_ap_id()
114 Pleroma.Constants.as_public(),
118 fix_explicit_addressing(object, explicit_mentions, follower_collection)
121 # if as:Public is addressed, then make sure the followers collection is also addressed
122 # so that the activities will be delivered to local users.
123 def fix_implicit_addressing(%{"to" => to, "cc" => cc} = object, followers_collection) do
124 recipients = to ++ cc
126 if followers_collection not in recipients do
128 Pleroma.Constants.as_public() in cc ->
129 to = to ++ [followers_collection]
130 Map.put(object, "to", to)
132 Pleroma.Constants.as_public() in to ->
133 cc = cc ++ [followers_collection]
134 Map.put(object, "cc", cc)
144 def fix_implicit_addressing(object, _), do: object
146 def fix_addressing(object) do
147 {:ok, %User{} = user} = User.get_or_fetch_by_ap_id(object["actor"])
148 followers_collection = User.ap_followers(user)
151 |> fix_addressing_list("to")
152 |> fix_addressing_list("cc")
153 |> fix_addressing_list("bto")
154 |> fix_addressing_list("bcc")
155 |> fix_explicit_addressing()
156 |> fix_implicit_addressing(followers_collection)
159 def fix_actor(%{"attributedTo" => actor} = object) do
160 actor = Containment.get_actor(%{"actor" => actor})
162 # TODO: Remove actor field for Objects
164 |> Map.put("actor", actor)
165 |> Map.put("attributedTo", actor)
168 def fix_in_reply_to(object, options \\ [])
170 def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object, options)
171 when not is_nil(in_reply_to) do
172 in_reply_to_id = prepare_in_reply_to(in_reply_to)
173 object = Map.put(object, "inReplyToAtomUri", in_reply_to_id)
174 depth = (options[:depth] || 0) + 1
176 if Federator.allowed_thread_distance?(depth) do
177 with {:ok, replied_object} <- get_obj_helper(in_reply_to_id, options),
178 %Activity{} <- Activity.get_create_by_object_ap_id(replied_object.data["id"]) do
180 |> Map.put("inReplyTo", replied_object.data["id"])
181 |> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
182 |> Map.put("context", replied_object.data["context"] || object["conversation"])
183 |> Map.drop(["conversation"])
186 Logger.warn("Couldn't fetch #{inspect(in_reply_to_id)}, error: #{inspect(e)}")
194 def fix_in_reply_to(object, _options), 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.valid?(url["mediaType"]) -> url["mediaType"]
233 MIME.valid?(data["mediaType"]) -> data["mediaType"]
234 MIME.valid?(data["mimeType"]) -> data["mimeType"]
240 is_map(url) && is_binary(url["href"]) -> url["href"]
241 is_binary(data["url"]) -> data["url"]
242 is_binary(data["href"]) -> data["href"]
250 "type" => Map.get(url || %{}, "type", "Link")
252 |> Maps.put_if_present("mediaType", media_type)
255 "url" => [attachment_url],
256 "type" => data["type"] || "Document"
258 |> Maps.put_if_present("mediaType", media_type)
259 |> Maps.put_if_present("name", data["name"])
266 Map.put(object, "attachment", attachments)
269 def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
271 |> Map.put("attachment", [attachment])
275 def fix_attachments(object), do: object
277 def fix_url(%{"url" => url} = object) when is_map(url) do
278 Map.put(object, "url", url["href"])
281 def fix_url(%{"type" => object_type, "url" => url} = object)
282 when object_type in ["Video", "Audio"] and is_list(url) do
284 Enum.find(url, fn x ->
285 media_type = x["mediaType"] || x["mimeType"] || ""
287 is_map(x) and String.starts_with?(media_type, ["audio/", "video/"])
291 Enum.find(url, fn x -> is_map(x) and (x["mediaType"] || x["mimeType"]) == "text/html" end)
294 |> Map.put("attachment", [attachment])
295 |> Map.put("url", link_element["href"])
298 def fix_url(%{"type" => object_type, "url" => url} = object)
299 when object_type != "Video" and is_list(url) do
300 first_element = Enum.at(url, 0)
304 is_bitstring(first_element) -> first_element
305 is_map(first_element) -> first_element["href"] || ""
309 Map.put(object, "url", url_string)
312 def fix_url(object), do: object
314 def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
317 |> Enum.filter(fn data -> data["type"] == "Emoji" and data["icon"] end)
318 |> Enum.reduce(%{}, fn data, mapping ->
319 name = String.trim(data["name"], ":")
321 Map.put(mapping, name, data["icon"]["url"])
324 # we merge mastodon and pleroma emoji into a single mapping, to allow for both wire formats
325 emoji = Map.merge(object["emoji"] || %{}, emoji)
327 Map.put(object, "emoji", emoji)
330 def fix_emoji(%{"tag" => %{"type" => "Emoji"} = tag} = object) do
331 name = String.trim(tag["name"], ":")
332 emoji = %{name => tag["icon"]["url"]}
334 Map.put(object, "emoji", emoji)
337 def fix_emoji(object), do: object
339 def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
342 |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
343 |> Enum.map(fn data -> String.slice(data["name"], 1..-1) end)
345 Map.put(object, "tag", tag ++ tags)
348 def fix_tag(%{"tag" => %{"type" => "Hashtag", "name" => hashtag} = tag} = object) do
349 combined = [tag, String.slice(hashtag, 1..-1)]
351 Map.put(object, "tag", combined)
354 def fix_tag(%{"tag" => %{} = tag} = object), do: Map.put(object, "tag", [tag])
356 def fix_tag(object), do: object
358 # content map usually only has one language so this will do for now.
359 def fix_content_map(%{"contentMap" => content_map} = object) do
360 content_groups = Map.to_list(content_map)
361 {_, content} = Enum.at(content_groups, 0)
363 Map.put(object, "content", content)
366 def fix_content_map(object), do: object
368 def fix_type(object, options \\ [])
370 def fix_type(%{"inReplyTo" => reply_id, "name" => _} = object, options)
371 when is_binary(reply_id) do
372 with true <- Federator.allowed_thread_distance?(options[:depth]),
373 {:ok, %{data: %{"type" => "Question"} = _} = _} <- get_obj_helper(reply_id, options) do
374 Map.put(object, "type", "Answer")
380 def fix_type(object, _), do: object
382 defp fix_content(%{"mediaType" => "text/markdown", "content" => content} = object)
383 when is_binary(content) do
386 |> Earmark.as_html!(%Earmark.Options{renderer: EarmarkRenderer})
387 |> Pleroma.HTML.filter_tags()
389 Map.merge(object, %{"content" => html_content, "mediaType" => "text/html"})
392 defp fix_content(object), do: object
394 defp mastodon_follow_hack(%{"id" => id, "actor" => follower_id}, followed) do
395 with true <- id =~ "follows",
396 %User{local: true} = follower <- User.get_cached_by_ap_id(follower_id),
397 %Activity{} = activity <- Utils.fetch_latest_follow(follower, followed) do
404 defp mastodon_follow_hack(_, _), do: {:error, nil}
406 defp get_follow_activity(follow_object, followed) do
407 with object_id when not is_nil(object_id) <- Utils.get_ap_id(follow_object),
408 {_, %Activity{} = activity} <- {:activity, Activity.get_by_ap_id(object_id)} do
411 # Can't find the activity. This might a Mastodon 2.3 "Accept"
413 mastodon_follow_hack(follow_object, followed)
420 # Reduce the object list to find the reported user.
421 defp get_reported(objects) do
422 Enum.reduce_while(objects, nil, fn ap_id, _ ->
423 with %User{} = user <- User.get_cached_by_ap_id(ap_id) do
431 # Compatibility wrapper for Mastodon votes
432 defp handle_create(%{"object" => %{"type" => "Answer"}} = data, _user) do
433 handle_incoming(data)
436 defp handle_create(%{"object" => object} = data, user) do
441 context: object["context"],
443 published: data["published"],
451 |> ActivityPub.create()
454 def handle_incoming(data, options \\ [])
456 # Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them
458 def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} = data, _options) do
459 with context <- data["context"] || Utils.generate_context_id(),
460 content <- data["content"] || "",
461 %User{} = actor <- User.get_cached_by_ap_id(actor),
462 # Reduce the object list to find the reported user.
463 %User{} = account <- get_reported(objects),
464 # Remove the reported user from the object list.
465 statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do
472 additional: %{"cc" => [account.ap_id]}
474 |> ActivityPub.flag()
478 # disallow objects with bogus IDs
479 def handle_incoming(%{"id" => nil}, _options), do: :error
480 def handle_incoming(%{"id" => ""}, _options), do: :error
481 # length of https:// = 8, should validate better, but good enough for now.
482 def handle_incoming(%{"id" => id}, _options) when is_binary(id) and byte_size(id) < 8,
485 # TODO: validate those with a Ecto scheme
489 %{"type" => "Create", "object" => %{"type" => objtype} = object} = data,
492 when objtype in ["Article", "Event", "Note", "Video", "Page", "Audio"] do
493 actor = Containment.get_actor(data)
495 with nil <- Activity.get_create_by_object_ap_id(object["id"]),
496 {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(actor) do
499 |> Map.put("object", fix_object(object, options))
500 |> Map.put("actor", actor)
503 with {:ok, created_activity} <- handle_create(data, user) do
504 reply_depth = (options[:depth] || 0) + 1
506 if Federator.allowed_thread_distance?(reply_depth) do
507 for reply_id <- replies(object) do
508 Pleroma.Workers.RemoteFetcherWorker.enqueue("fetch_remote", %{
510 "depth" => reply_depth
515 {:ok, created_activity}
518 %Activity{} = activity -> {:ok, activity}
524 %{"type" => "Listen", "object" => %{"type" => "Audio"} = object} = data,
527 actor = Containment.get_actor(data)
530 Map.put(data, "actor", actor)
533 with {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
534 reply_depth = (options[:depth] || 0) + 1
535 options = Keyword.put(options, :depth, reply_depth)
536 object = fix_object(object, options)
544 published: data["published"],
545 additional: Map.take(data, ["cc", "id"])
548 ActivityPub.listen(params)
555 %{"type" => "Accept", "object" => follow_object, "actor" => _actor, "id" => id} = data,
558 with actor <- Containment.get_actor(data),
559 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
560 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
561 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "accept"),
562 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
563 {:ok, _relationship} <- FollowingRelationship.update(follower, followed, :follow_accept) do
564 User.update_follower_count(followed)
565 User.update_following_count(follower)
567 Notification.update_notification_type(followed, follow_activity)
569 ActivityPub.accept(%{
570 to: follow_activity.data["to"],
573 object: follow_activity.data["id"],
584 %{"type" => "Reject", "object" => follow_object, "actor" => _actor, "id" => id} = data,
587 with actor <- Containment.get_actor(data),
588 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
589 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
590 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "reject"),
591 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
592 {:ok, _relationship} <- FollowingRelationship.update(follower, followed, :follow_reject),
594 ActivityPub.reject(%{
595 to: follow_activity.data["to"],
598 object: follow_activity.data["id"],
608 @misskey_reactions %{
622 @doc "Rewrite misskey likes into EmojiReacts"
626 "_misskey_reaction" => reaction
631 |> Map.put("type", "EmojiReact")
632 |> Map.put("content", @misskey_reactions[reaction] || reaction)
633 |> handle_incoming(options)
637 %{"type" => "Create", "object" => %{"type" => objtype}} = data,
640 when objtype in ["Question", "Answer", "ChatMessage"] do
641 with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
642 {:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
647 def handle_incoming(%{"type" => type} = data, _options)
648 when type in ~w{Like EmojiReact Announce} do
649 with :ok <- ObjectValidator.fetch_actor_and_object(data),
650 {:ok, activity, _meta} <-
651 Pipeline.common_pipeline(data, local: false) do
659 %{"type" => type} = data,
662 when type in ~w{Update Block Follow} do
663 with {:ok, %User{}} <- ObjectValidator.fetch_actor(data),
664 {:ok, activity, _} <-
665 Pipeline.common_pipeline(data, local: false) do
671 %{"type" => "Delete"} = data,
674 with {:ok, activity, _} <-
675 Pipeline.common_pipeline(data, local: false) do
678 {:error, {:validate_object, _}} = e ->
679 # Check if we have a create activity for this
680 with {:ok, object_id} <- ObjectValidators.ObjectID.cast(data["object"]),
681 %Activity{data: %{"actor" => actor}} <-
682 Activity.create_by_object_ap_id(object_id) |> Repo.one(),
683 # We have one, insert a tombstone and retry
684 {:ok, tombstone_data, _} <- Builder.tombstone(actor, object_id),
685 {:ok, _tombstone} <- Object.create(tombstone_data) do
686 handle_incoming(data)
696 "object" => %{"type" => "Follow", "object" => followed},
702 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
703 {:ok, %User{} = follower} <- User.get_or_fetch_by_ap_id(follower),
704 {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
705 User.unfollow(follower, followed)
715 "object" => %{"type" => type}
719 when type in ["Like", "EmojiReact", "Announce", "Block"] do
720 with {:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
725 # For Undos that don't have the complete object attached, try to find it in our database.
733 when is_binary(object) do
734 with %Activity{data: data} <- Activity.get_by_ap_id(object) do
736 |> Map.put("object", data)
737 |> handle_incoming(options)
746 "actor" => origin_actor,
747 "object" => origin_actor,
748 "target" => target_actor
752 with %User{} = origin_user <- User.get_cached_by_ap_id(origin_actor),
753 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_actor),
754 true <- origin_actor in target_user.also_known_as do
755 ActivityPub.move(origin_user, target_user, false)
761 def handle_incoming(_, _), do: :error
763 @spec get_obj_helper(String.t(), Keyword.t()) :: {:ok, Object.t()} | nil
764 def get_obj_helper(id, options \\ []) do
765 case Object.normalize(id, true, options) do
766 %Object{} = object -> {:ok, object}
771 @spec get_embedded_obj_helper(String.t() | Object.t(), User.t()) :: {:ok, Object.t()} | nil
772 def get_embedded_obj_helper(%{"attributedTo" => attributed_to, "id" => object_id} = data, %User{
775 when attributed_to == ap_id do
776 with {:ok, activity} <-
781 "actor" => attributed_to,
784 {:ok, Object.normalize(activity)}
786 _ -> get_obj_helper(object_id)
790 def get_embedded_obj_helper(object_id, _) do
791 get_obj_helper(object_id)
794 def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
795 with false <- String.starts_with?(in_reply_to, "http"),
796 {:ok, %{data: replied_to_object}} <- get_obj_helper(in_reply_to) do
797 Map.put(object, "inReplyTo", replied_to_object["external_url"] || in_reply_to)
803 def set_reply_to_uri(obj), do: obj
806 Serialized Mastodon-compatible `replies` collection containing _self-replies_.
807 Based on Mastodon's ActivityPub::NoteSerializer#replies.
809 def set_replies(obj_data) do
811 with limit when limit > 0 <-
812 Pleroma.Config.get([:activitypub, :note_replies_output_limit], 0),
813 %Object{} = object <- Object.get_cached_by_ap_id(obj_data["id"]) do
815 |> Object.self_replies()
816 |> select([o], fragment("?->>'id'", o.data))
823 set_replies(obj_data, replies_uris)
826 defp set_replies(obj, []) do
830 defp set_replies(obj, replies_uris) do
831 replies_collection = %{
832 "type" => "Collection",
833 "items" => replies_uris
836 Map.merge(obj, %{"replies" => replies_collection})
839 def replies(%{"replies" => %{"first" => %{"items" => items}}}) when not is_nil(items) do
843 def replies(%{"replies" => %{"items" => items}}) when not is_nil(items) do
847 def replies(_), do: []
849 # Prepares the object of an outgoing create activity.
850 def prepare_object(object) do
857 |> prepare_attachments
861 |> strip_internal_fields
862 |> strip_internal_tags
868 # internal -> Mastodon
871 def prepare_outgoing(%{"type" => activity_type, "object" => object_id} = data)
872 when activity_type in ["Create", "Listen"] do
875 |> Object.normalize()
881 |> Map.put("object", object)
882 |> Map.merge(Utils.make_json_ld_header())
888 def prepare_outgoing(%{"type" => "Announce", "actor" => ap_id, "object" => object_id} = data) do
891 |> Object.normalize()
894 if Visibility.is_private?(object) && object.data["actor"] == ap_id do
895 data |> Map.put("object", object |> Map.get(:data) |> prepare_object)
897 data |> maybe_fix_object_url
902 |> strip_internal_fields
903 |> Map.merge(Utils.make_json_ld_header())
909 # Mastodon Accept/Reject requires a non-normalized object containing the actor URIs,
910 # because of course it does.
911 def prepare_outgoing(%{"type" => "Accept"} = data) do
912 with follow_activity <- Activity.normalize(data["object"]) do
914 "actor" => follow_activity.actor,
915 "object" => follow_activity.data["object"],
916 "id" => follow_activity.data["id"],
922 |> Map.put("object", object)
923 |> Map.merge(Utils.make_json_ld_header())
929 def prepare_outgoing(%{"type" => "Reject"} = data) do
930 with follow_activity <- Activity.normalize(data["object"]) do
932 "actor" => follow_activity.actor,
933 "object" => follow_activity.data["object"],
934 "id" => follow_activity.data["id"],
940 |> Map.put("object", object)
941 |> Map.merge(Utils.make_json_ld_header())
947 def prepare_outgoing(%{"type" => _type} = data) do
950 |> strip_internal_fields
951 |> maybe_fix_object_url
952 |> Map.merge(Utils.make_json_ld_header())
957 def maybe_fix_object_url(%{"object" => object} = data) when is_binary(object) do
958 with false <- String.starts_with?(object, "http"),
959 {:fetch, {:ok, relative_object}} <- {:fetch, get_obj_helper(object)},
960 %{data: %{"external_url" => external_url}} when not is_nil(external_url) <-
962 Map.put(data, "object", external_url)
965 Logger.error("Couldn't fetch #{object} #{inspect(e)}")
973 def maybe_fix_object_url(data), do: data
975 def add_hashtags(object) do
977 (object["tag"] || [])
979 # Expand internal representation tags into AS2 tags.
980 tag when is_binary(tag) ->
982 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
987 # Do not process tags which are already AS2 tag objects.
988 tag when is_map(tag) ->
992 Map.put(object, "tag", tags)
995 # TODO These should be added on our side on insertion, it doesn't make much
996 # sense to regenerate these all the time
997 def add_mention_tags(object) do
998 to = object["to"] || []
999 cc = object["cc"] || []
1000 mentioned = User.get_users_from_set(to ++ cc, local_only: false)
1002 mentions = Enum.map(mentioned, &build_mention_tag/1)
1004 tags = object["tag"] || []
1005 Map.put(object, "tag", tags ++ mentions)
1008 defp build_mention_tag(%{ap_id: ap_id, nickname: nickname} = _) do
1009 %{"type" => "Mention", "href" => ap_id, "name" => "@#{nickname}"}
1012 def take_emoji_tags(%User{emoji: emoji}) do
1015 |> Enum.map(&build_emoji_tag/1)
1018 # TODO: we should probably send mtime instead of unix epoch time for updated
1019 def add_emoji_tags(%{"emoji" => emoji} = object) do
1020 tags = object["tag"] || []
1022 out = Enum.map(emoji, &build_emoji_tag/1)
1024 Map.put(object, "tag", tags ++ out)
1027 def add_emoji_tags(object), do: object
1029 defp build_emoji_tag({name, url}) do
1031 "icon" => %{"url" => url, "type" => "Image"},
1032 "name" => ":" <> name <> ":",
1034 "updated" => "1970-01-01T00:00:00Z",
1039 def set_conversation(object) do
1040 Map.put(object, "conversation", object["context"])
1043 def set_sensitive(%{"sensitive" => true} = object) do
1047 def set_sensitive(object) do
1048 tags = object["tag"] || []
1049 Map.put(object, "sensitive", "nsfw" in tags)
1052 def set_type(%{"type" => "Answer"} = object) do
1053 Map.put(object, "type", "Note")
1056 def set_type(object), do: object
1058 def add_attributed_to(object) do
1059 attributed_to = object["attributedTo"] || object["actor"]
1060 Map.put(object, "attributedTo", attributed_to)
1063 # TODO: Revisit this
1064 def prepare_attachments(%{"type" => "ChatMessage"} = object), do: object
1066 def prepare_attachments(object) do
1069 |> Map.get("attachment", [])
1070 |> Enum.map(fn data ->
1071 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
1075 "mediaType" => media_type,
1076 "name" => data["name"],
1077 "type" => "Document"
1081 Map.put(object, "attachment", attachments)
1084 def strip_internal_fields(object) do
1085 Map.drop(object, Pleroma.Constants.object_internal_fields())
1088 defp strip_internal_tags(%{"tag" => tags} = object) do
1089 tags = Enum.filter(tags, fn x -> is_map(x) end)
1091 Map.put(object, "tag", tags)
1094 defp strip_internal_tags(object), do: object
1096 def perform(:user_upgrade, user) do
1097 # we pass a fake user so that the followers collection is stripped away
1098 old_follower_address = User.ap_followers(%User{nickname: user.nickname})
1102 where: ^old_follower_address in a.recipients,
1107 "array_replace(?,?,?)",
1109 ^old_follower_address,
1110 ^user.follower_address
1115 |> Repo.update_all([])
1118 def upgrade_user_from_ap_id(ap_id) do
1119 with %User{local: false} = user <- User.get_cached_by_ap_id(ap_id),
1120 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id),
1121 {:ok, user} <- update_user(user, data) do
1122 TransmogrifierWorker.enqueue("user_upgrade", %{"user_id" => user.id})
1125 %User{} = user -> {:ok, user}
1130 defp update_user(user, data) do
1132 |> User.remote_user_changeset(data)
1133 |> User.update_and_set_cache()
1136 def maybe_fix_user_url(%{"url" => url} = data) when is_map(url) do
1137 Map.put(data, "url", url["href"])
1140 def maybe_fix_user_url(data), do: data
1142 def maybe_fix_user_object(data), do: maybe_fix_user_url(data)