1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 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.
11 alias Pleroma.Object.Containment
14 alias Pleroma.Web.ActivityPub.ActivityPub
15 alias Pleroma.Web.ActivityPub.Utils
16 alias Pleroma.Web.ActivityPub.Visibility
17 alias Pleroma.Web.Federator
18 alias Pleroma.Workers.TransmogrifierWorker
23 require Pleroma.Constants
26 Modifies an incoming AP object (mastodon format) to our internal format.
28 def fix_object(object, options \\ []) do
30 |> strip_internal_fields
35 |> fix_in_reply_to(options)
44 def fix_summary(%{"summary" => nil} = object) do
45 Map.put(object, "summary", "")
48 def fix_summary(%{"summary" => _} = object) do
49 # summary is present, nothing to do
53 def fix_summary(object), do: Map.put(object, "summary", "")
55 def fix_addressing_list(map, field) do
57 is_binary(map[field]) ->
58 Map.put(map, field, [map[field]])
61 Map.put(map, field, [])
68 def fix_explicit_addressing(
69 %{"to" => to, "cc" => cc} = object,
73 explicit_to = Enum.filter(to, fn x -> x in explicit_mentions end)
75 explicit_cc = Enum.filter(to, fn x -> x not in explicit_mentions end)
79 |> Enum.reject(fn x -> String.ends_with?(x, "/followers") and x != follower_collection end)
83 |> Map.put("to", explicit_to)
84 |> Map.put("cc", final_cc)
87 def fix_explicit_addressing(object, _explicit_mentions, _followers_collection), do: object
89 # if directMessage flag is set to true, leave the addressing alone
90 def fix_explicit_addressing(%{"directMessage" => true} = object), do: object
92 def fix_explicit_addressing(object) do
93 explicit_mentions = Utils.determine_explicit_mentions(object)
95 %User{follower_address: follower_collection} =
97 |> Containment.get_actor()
98 |> User.get_cached_by_ap_id()
103 Pleroma.Constants.as_public(),
107 fix_explicit_addressing(object, explicit_mentions, follower_collection)
110 # if as:Public is addressed, then make sure the followers collection is also addressed
111 # so that the activities will be delivered to local users.
112 def fix_implicit_addressing(%{"to" => to, "cc" => cc} = object, followers_collection) do
113 recipients = to ++ cc
115 if followers_collection not in recipients do
117 Pleroma.Constants.as_public() in cc ->
118 to = to ++ [followers_collection]
119 Map.put(object, "to", to)
121 Pleroma.Constants.as_public() in to ->
122 cc = cc ++ [followers_collection]
123 Map.put(object, "cc", cc)
133 def fix_implicit_addressing(object, _), do: object
135 def fix_addressing(object) do
136 {:ok, %User{} = user} = User.get_or_fetch_by_ap_id(object["actor"])
137 followers_collection = User.ap_followers(user)
140 |> fix_addressing_list("to")
141 |> fix_addressing_list("cc")
142 |> fix_addressing_list("bto")
143 |> fix_addressing_list("bcc")
144 |> fix_explicit_addressing()
145 |> fix_implicit_addressing(followers_collection)
148 def fix_actor(%{"attributedTo" => actor} = object) do
149 Map.put(object, "actor", Containment.get_actor(%{"actor" => actor}))
152 def fix_in_reply_to(object, options \\ [])
154 def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object, options)
155 when not is_nil(in_reply_to) do
156 in_reply_to_id = prepare_in_reply_to(in_reply_to)
157 object = Map.put(object, "inReplyToAtomUri", in_reply_to_id)
159 if Federator.allowed_incoming_reply_depth?(options[:depth]) do
160 with {:ok, replied_object} <- get_obj_helper(in_reply_to_id, options),
161 %Activity{} = _ <- Activity.get_create_by_object_ap_id(replied_object.data["id"]) do
163 |> Map.put("inReplyTo", replied_object.data["id"])
164 |> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
165 |> Map.put("conversation", replied_object.data["context"] || object["conversation"])
166 |> Map.put("context", replied_object.data["context"] || object["conversation"])
169 Logger.error("Couldn't fetch #{inspect(in_reply_to_id)}, error: #{inspect(e)}")
177 def fix_in_reply_to(object, _options), do: object
179 defp prepare_in_reply_to(in_reply_to) do
181 is_bitstring(in_reply_to) ->
184 is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) ->
187 is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) ->
188 Enum.at(in_reply_to, 0)
195 def fix_context(object) do
196 context = object["context"] || object["conversation"] || Utils.generate_context_id()
199 |> Map.put("context", context)
200 |> Map.put("conversation", context)
203 def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
205 Enum.map(attachment, fn data ->
206 media_type = data["mediaType"] || data["mimeType"]
207 href = data["url"] || data["href"]
208 url = [%{"type" => "Link", "mediaType" => media_type, "href" => href}]
211 |> Map.put("mediaType", media_type)
212 |> Map.put("url", url)
215 Map.put(object, "attachment", attachments)
218 def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
220 |> Map.put("attachment", [attachment])
224 def fix_attachments(object), do: object
226 def fix_url(%{"url" => url} = object) when is_map(url) do
227 Map.put(object, "url", url["href"])
230 def fix_url(%{"type" => "Video", "url" => url} = object) when is_list(url) do
231 first_element = Enum.at(url, 0)
233 link_element = Enum.find(url, fn x -> is_map(x) and x["mimeType"] == "text/html" end)
236 |> Map.put("attachment", [first_element])
237 |> Map.put("url", link_element["href"])
240 def fix_url(%{"type" => object_type, "url" => url} = object)
241 when object_type != "Video" and is_list(url) do
242 first_element = Enum.at(url, 0)
246 is_bitstring(first_element) -> first_element
247 is_map(first_element) -> first_element["href"] || ""
251 Map.put(object, "url", url_string)
254 def fix_url(object), do: object
256 def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
259 |> Enum.filter(fn data -> data["type"] == "Emoji" and data["icon"] end)
260 |> Enum.reduce(%{}, fn data, mapping ->
261 name = String.trim(data["name"], ":")
263 Map.put(mapping, name, data["icon"]["url"])
266 # we merge mastodon and pleroma emoji into a single mapping, to allow for both wire formats
267 emoji = Map.merge(object["emoji"] || %{}, emoji)
269 Map.put(object, "emoji", emoji)
272 def fix_emoji(%{"tag" => %{"type" => "Emoji"} = tag} = object) do
273 name = String.trim(tag["name"], ":")
274 emoji = %{name => tag["icon"]["url"]}
276 Map.put(object, "emoji", emoji)
279 def fix_emoji(object), do: object
281 def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
284 |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
285 |> Enum.map(fn data -> String.slice(data["name"], 1..-1) end)
287 Map.put(object, "tag", tag ++ tags)
290 def fix_tag(%{"tag" => %{"type" => "Hashtag", "name" => hashtag} = tag} = object) do
291 combined = [tag, String.slice(hashtag, 1..-1)]
293 Map.put(object, "tag", combined)
296 def fix_tag(%{"tag" => %{} = tag} = object), do: Map.put(object, "tag", [tag])
298 def fix_tag(object), do: object
300 # content map usually only has one language so this will do for now.
301 def fix_content_map(%{"contentMap" => content_map} = object) do
302 content_groups = Map.to_list(content_map)
303 {_, content} = Enum.at(content_groups, 0)
305 Map.put(object, "content", content)
308 def fix_content_map(object), do: object
310 def fix_type(object, options \\ [])
312 def fix_type(%{"inReplyTo" => reply_id, "name" => _} = object, options)
313 when is_binary(reply_id) do
314 with true <- Federator.allowed_incoming_reply_depth?(options[:depth]),
315 {:ok, %{data: %{"type" => "Question"} = _} = _} <- get_obj_helper(reply_id, options) do
316 Map.put(object, "type", "Answer")
322 def fix_type(object, _), do: object
324 defp mastodon_follow_hack(%{"id" => id, "actor" => follower_id}, followed) do
325 with true <- id =~ "follows",
326 %User{local: true} = follower <- User.get_cached_by_ap_id(follower_id),
327 %Activity{} = activity <- Utils.fetch_latest_follow(follower, followed) do
334 defp mastodon_follow_hack(_, _), do: {:error, nil}
336 defp get_follow_activity(follow_object, followed) do
337 with object_id when not is_nil(object_id) <- Utils.get_ap_id(follow_object),
338 {_, %Activity{} = activity} <- {:activity, Activity.get_by_ap_id(object_id)} do
341 # Can't find the activity. This might a Mastodon 2.3 "Accept"
343 mastodon_follow_hack(follow_object, followed)
350 # Reduce the object list to find the reported user.
351 defp get_reported(objects) do
352 Enum.reduce_while(objects, nil, fn ap_id, _ ->
353 with %User{} = user <- User.get_cached_by_ap_id(ap_id) do
361 def handle_incoming(data, options \\ [])
363 # Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them
365 def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} = data, _options) do
366 with context <- data["context"] || Utils.generate_context_id(),
367 content <- data["content"] || "",
368 %User{} = actor <- User.get_cached_by_ap_id(actor),
369 # Reduce the object list to find the reported user.
370 %User{} = account <- get_reported(objects),
371 # Remove the reported user from the object list.
372 statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do
379 additional: %{"cc" => [account.ap_id]}
381 |> ActivityPub.flag()
385 # disallow objects with bogus IDs
386 def handle_incoming(%{"id" => nil}, _options), do: :error
387 def handle_incoming(%{"id" => ""}, _options), do: :error
388 # length of https:// = 8, should validate better, but good enough for now.
389 def handle_incoming(%{"id" => id}, _options) when not (is_binary(id) and length(id) > 8),
392 # TODO: validate those with a Ecto scheme
396 %{"type" => "Create", "object" => %{"type" => objtype} = object} = data,
399 when objtype in ["Article", "Note", "Video", "Page", "Question", "Answer"] do
400 actor = Containment.get_actor(data)
403 Map.put(data, "actor", actor)
406 with nil <- Activity.get_create_by_object_ap_id(object["id"]),
407 {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
408 options = Keyword.put(options, :depth, (options[:depth] || 0) + 1)
409 object = fix_object(data["object"], options)
415 context: object["conversation"],
417 published: data["published"],
426 ActivityPub.create(params)
428 %Activity{} = activity -> {:ok, activity}
434 %{"type" => "Listen", "object" => %{"type" => "Audio"} = object} = data,
437 actor = Containment.get_actor(data)
440 Map.put(data, "actor", actor)
443 with {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
444 options = Keyword.put(options, :depth, (options[:depth] || 0) + 1)
445 object = fix_object(object, options)
453 published: data["published"],
454 additional: Map.take(data, ["cc", "id"])
457 ActivityPub.listen(params)
464 %{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data,
467 with %User{local: true} = followed <-
468 User.get_cached_by_ap_id(Containment.get_actor(%{"actor" => followed})),
469 {:ok, %User{} = follower} <-
470 User.get_or_fetch_by_ap_id(Containment.get_actor(%{"actor" => follower})),
471 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
472 with deny_follow_blocked <- Pleroma.Config.get([:user, :deny_follow_blocked]),
473 {_, false} <- {:user_blocked, User.blocks?(followed, follower) && deny_follow_blocked},
474 {_, false} <- {:user_locked, User.locked?(followed)},
475 {_, {:ok, follower}} <- {:follow, User.follow(follower, followed)},
477 {:follow_state_update, Utils.update_follow_state_for_all(activity, "accept")} do
478 ActivityPub.accept(%{
479 to: [follower.ap_id],
485 {:user_blocked, true} ->
486 {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
488 ActivityPub.reject(%{
489 to: [follower.ap_id],
495 {:follow, {:error, _}} ->
496 {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
498 ActivityPub.reject(%{
499 to: [follower.ap_id],
505 {:user_locked, true} ->
517 %{"type" => "Accept", "object" => follow_object, "actor" => _actor, "id" => id} = data,
520 with actor <- Containment.get_actor(data),
521 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
522 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
523 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "accept"),
524 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
525 {:ok, _follower} = User.follow(follower, followed) do
526 ActivityPub.accept(%{
527 to: follow_activity.data["to"],
530 object: follow_activity.data["id"],
540 %{"type" => "Reject", "object" => follow_object, "actor" => _actor, "id" => id} = data,
543 with actor <- Containment.get_actor(data),
544 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
545 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
546 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "reject"),
547 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
549 ActivityPub.reject(%{
550 to: follow_activity.data["to"],
553 object: follow_activity.data["id"],
557 User.unfollow(follower, followed)
566 %{"type" => "Like", "object" => object_id, "actor" => _actor, "id" => id} = data,
569 with actor <- Containment.get_actor(data),
570 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
571 {:ok, object} <- get_obj_helper(object_id),
572 {:ok, activity, _object} <- ActivityPub.like(actor, object, id, false) do
580 %{"type" => "Announce", "object" => object_id, "actor" => _actor, "id" => id} = data,
583 with actor <- Containment.get_actor(data),
584 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
585 {:ok, object} <- get_embedded_obj_helper(object_id, actor),
586 public <- Visibility.is_public?(data),
587 {:ok, activity, _object} <- ActivityPub.announce(actor, object, id, false, public) do
595 %{"type" => "Update", "object" => %{"type" => object_type} = object, "actor" => actor_id} =
599 when object_type in [
605 with %User{ap_id: ^actor_id} = actor <- User.get_cached_by_ap_id(object["id"]) do
606 {:ok, new_user_data} = ActivityPub.user_data_from_user_object(object)
608 locked = new_user_data[:locked] || false
609 attachment = get_in(new_user_data, [:source_data, "attachment"]) || []
610 invisible = new_user_data[:invisible] || false
614 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
615 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
619 |> Map.take([:avatar, :banner, :bio, :name, :also_known_as])
620 |> Map.put(:fields, fields)
621 |> Map.put(:locked, locked)
622 |> Map.put(:invisible, invisible)
625 |> User.upgrade_changeset(update_data, true)
626 |> User.update_and_set_cache()
628 ActivityPub.update(%{
630 to: data["to"] || [],
631 cc: data["cc"] || [],
634 activity_id: data["id"]
643 # TODO: We presently assume that any actor on the same origin domain as the object being
644 # deleted has the rights to delete that object. A better way to validate whether or not
645 # the object should be deleted is to refetch the object URI, which should return either
646 # an error or a tombstone. This would allow us to verify that a deletion actually took
649 %{"type" => "Delete", "object" => object_id, "actor" => actor, "id" => id} = data,
652 object_id = Utils.get_ap_id(object_id)
654 with actor <- Containment.get_actor(data),
655 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
656 {:ok, object} <- get_obj_helper(object_id),
657 :ok <- Containment.contain_origin(actor.ap_id, object.data),
659 ActivityPub.delete(object, local: false, activity_id: id, actor: actor.ap_id) do
663 case User.get_cached_by_ap_id(object_id) do
664 %User{ap_id: ^actor} = user ->
679 "object" => %{"type" => "Announce", "object" => object_id},
685 with actor <- Containment.get_actor(data),
686 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
687 {:ok, object} <- get_obj_helper(object_id),
688 {:ok, activity, _} <- ActivityPub.unannounce(actor, object, id, false) do
698 "object" => %{"type" => "Follow", "object" => followed},
704 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
705 {:ok, %User{} = follower} <- User.get_or_fetch_by_ap_id(follower),
706 {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
707 User.unfollow(follower, followed)
717 "object" => %{"type" => "Block", "object" => blocked},
723 with %User{local: true} = blocked <- User.get_cached_by_ap_id(blocked),
724 {:ok, %User{} = blocker} <- User.get_or_fetch_by_ap_id(blocker),
725 {:ok, activity} <- ActivityPub.unblock(blocker, blocked, id, false) do
726 User.unblock(blocker, blocked)
734 %{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = _data,
737 with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked),
738 {:ok, %User{} = blocker} = User.get_or_fetch_by_ap_id(blocker),
739 {:ok, activity} <- ActivityPub.block(blocker, blocked, id, false) do
740 User.unfollow(blocker, blocked)
741 User.block(blocker, blocked)
751 "object" => %{"type" => "Like", "object" => object_id},
757 with actor <- Containment.get_actor(data),
758 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
759 {:ok, object} <- get_obj_helper(object_id),
760 {:ok, activity, _, _} <- ActivityPub.unlike(actor, object, id, false) do
767 # For Undos that don't have the complete object attached, try to find it in our database.
775 when is_binary(object) do
776 with %Activity{data: data} <- Activity.get_by_ap_id(object) do
778 |> Map.put("object", data)
779 |> handle_incoming(options)
785 def handle_incoming(_, _), do: :error
787 @spec get_obj_helper(String.t(), Keyword.t()) :: {:ok, Object.t()} | nil
788 def get_obj_helper(id, options \\ []) do
789 case Object.normalize(id, true, options) do
790 %Object{} = object -> {:ok, object}
795 @spec get_embedded_obj_helper(String.t() | Object.t(), User.t()) :: {:ok, Object.t()} | nil
796 def get_embedded_obj_helper(%{"attributedTo" => attributed_to, "id" => object_id} = data, %User{
799 when attributed_to == ap_id do
800 with {:ok, activity} <-
805 "actor" => attributed_to,
808 {:ok, Object.normalize(activity)}
810 _ -> get_obj_helper(object_id)
814 def get_embedded_obj_helper(object_id, _) do
815 get_obj_helper(object_id)
818 def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
819 with false <- String.starts_with?(in_reply_to, "http"),
820 {:ok, %{data: replied_to_object}} <- get_obj_helper(in_reply_to) do
821 Map.put(object, "inReplyTo", replied_to_object["external_url"] || in_reply_to)
827 def set_reply_to_uri(obj), do: obj
829 # Prepares the object of an outgoing create activity.
830 def prepare_object(object) do
837 |> prepare_attachments
840 |> strip_internal_fields
841 |> strip_internal_tags
847 # internal -> Mastodon
850 def prepare_outgoing(%{"type" => activity_type, "object" => object_id} = data)
851 when activity_type in ["Create", "Listen"] do
854 |> Object.normalize()
860 |> Map.put("object", object)
861 |> Map.merge(Utils.make_json_ld_header())
867 def prepare_outgoing(%{"type" => "Announce", "actor" => ap_id, "object" => object_id} = data) do
870 |> Object.normalize()
873 if Visibility.is_private?(object) && object.data["actor"] == ap_id do
874 data |> Map.put("object", object |> Map.get(:data) |> prepare_object)
876 data |> maybe_fix_object_url
881 |> strip_internal_fields
882 |> Map.merge(Utils.make_json_ld_header())
888 # Mastodon Accept/Reject requires a non-normalized object containing the actor URIs,
889 # because of course it does.
890 def prepare_outgoing(%{"type" => "Accept"} = data) do
891 with follow_activity <- Activity.normalize(data["object"]) do
893 "actor" => follow_activity.actor,
894 "object" => follow_activity.data["object"],
895 "id" => follow_activity.data["id"],
901 |> Map.put("object", object)
902 |> Map.merge(Utils.make_json_ld_header())
908 def prepare_outgoing(%{"type" => "Reject"} = data) do
909 with follow_activity <- Activity.normalize(data["object"]) do
911 "actor" => follow_activity.actor,
912 "object" => follow_activity.data["object"],
913 "id" => follow_activity.data["id"],
919 |> Map.put("object", object)
920 |> Map.merge(Utils.make_json_ld_header())
926 def prepare_outgoing(%{"type" => _type} = data) do
929 |> strip_internal_fields
930 |> maybe_fix_object_url
931 |> Map.merge(Utils.make_json_ld_header())
936 def maybe_fix_object_url(%{"object" => object} = data) when is_binary(object) do
937 with false <- String.starts_with?(object, "http"),
938 {:fetch, {:ok, relative_object}} <- {:fetch, get_obj_helper(object)},
939 %{data: %{"external_url" => external_url}} when not is_nil(external_url) <-
941 Map.put(data, "object", external_url)
944 Logger.error("Couldn't fetch #{object} #{inspect(e)}")
952 def maybe_fix_object_url(data), do: data
954 def add_hashtags(object) do
956 (object["tag"] || [])
958 # Expand internal representation tags into AS2 tags.
959 tag when is_binary(tag) ->
961 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
966 # Do not process tags which are already AS2 tag objects.
967 tag when is_map(tag) ->
971 Map.put(object, "tag", tags)
974 def add_mention_tags(object) do
977 |> Utils.get_notified_from_object()
978 |> Enum.map(&build_mention_tag/1)
980 tags = object["tag"] || []
982 Map.put(object, "tag", tags ++ mentions)
985 defp build_mention_tag(%{ap_id: ap_id, nickname: nickname} = _) do
986 %{"type" => "Mention", "href" => ap_id, "name" => "@#{nickname}"}
989 def take_emoji_tags(%User{emoji: emoji}) do
991 |> Enum.flat_map(&Map.to_list/1)
992 |> Enum.map(&build_emoji_tag/1)
995 # TODO: we should probably send mtime instead of unix epoch time for updated
996 def add_emoji_tags(%{"emoji" => emoji} = object) do
997 tags = object["tag"] || []
999 out = Enum.map(emoji, &build_emoji_tag/1)
1001 Map.put(object, "tag", tags ++ out)
1004 def add_emoji_tags(object), do: object
1006 defp build_emoji_tag({name, url}) do
1008 "icon" => %{"url" => url, "type" => "Image"},
1009 "name" => ":" <> name <> ":",
1011 "updated" => "1970-01-01T00:00:00Z",
1016 def set_conversation(object) do
1017 Map.put(object, "conversation", object["context"])
1020 def set_sensitive(object) do
1021 tags = object["tag"] || []
1022 Map.put(object, "sensitive", "nsfw" in tags)
1025 def set_type(%{"type" => "Answer"} = object) do
1026 Map.put(object, "type", "Note")
1029 def set_type(object), do: object
1031 def add_attributed_to(object) do
1032 attributed_to = object["attributedTo"] || object["actor"]
1033 Map.put(object, "attributedTo", attributed_to)
1036 def prepare_attachments(object) do
1038 (object["attachment"] || [])
1039 |> Enum.map(fn data ->
1040 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
1041 %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
1044 Map.put(object, "attachment", attachments)
1047 defp strip_internal_fields(object) do
1049 |> Map.drop(Pleroma.Constants.object_internal_fields())
1052 defp strip_internal_tags(%{"tag" => tags} = object) do
1053 tags = Enum.filter(tags, fn x -> is_map(x) end)
1055 Map.put(object, "tag", tags)
1058 defp strip_internal_tags(object), do: object
1060 def perform(:user_upgrade, user) do
1061 # we pass a fake user so that the followers collection is stripped away
1062 old_follower_address = User.ap_followers(%User{nickname: user.nickname})
1067 where: ^old_follower_address in u.following,
1072 "array_replace(?,?,?)",
1074 ^old_follower_address,
1075 ^user.follower_address
1081 Repo.update_all(q, [])
1086 where: ^old_follower_address in a.recipients,
1091 "array_replace(?,?,?)",
1093 ^old_follower_address,
1094 ^user.follower_address
1100 Repo.update_all(q, [])
1103 def upgrade_user_from_ap_id(ap_id) do
1104 with %User{local: false} = user <- User.get_cached_by_ap_id(ap_id),
1105 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id),
1106 already_ap <- User.ap_enabled?(user),
1107 {:ok, user} <- upgrade_user(user, data) do
1108 if not already_ap do
1109 TransmogrifierWorker.enqueue("user_upgrade", %{"user_id" => user.id})
1114 %User{} = user -> {:ok, user}
1119 defp upgrade_user(user, data) do
1121 |> User.upgrade_changeset(data, true)
1122 |> User.update_and_set_cache()
1125 def maybe_fix_user_url(%{"url" => url} = data) when is_map(url) do
1126 Map.put(data, "url", url["href"])
1129 def maybe_fix_user_url(data), do: data
1131 def maybe_fix_user_object(data), do: maybe_fix_user_url(data)