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 banner = new_user_data[:info][:banner]
609 locked = new_user_data[:info][:locked] || false
610 attachment = get_in(new_user_data, [:info, :source_data, "attachment"]) || []
611 invisible = new_user_data[:info][:invisible] || false
615 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
616 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
620 |> Map.take([:name, :bio, :avatar])
621 |> Map.put(:info, %{banner: banner, locked: locked, fields: fields, invisible: invisible})
624 |> User.upgrade_changeset(update_data, true)
625 |> User.update_and_set_cache()
627 ActivityPub.update(%{
629 to: data["to"] || [],
630 cc: data["cc"] || [],
633 activity_id: data["id"]
642 # TODO: We presently assume that any actor on the same origin domain as the object being
643 # deleted has the rights to delete that object. A better way to validate whether or not
644 # the object should be deleted is to refetch the object URI, which should return either
645 # an error or a tombstone. This would allow us to verify that a deletion actually took
648 %{"type" => "Delete", "object" => object_id, "actor" => actor, "id" => id} = data,
651 object_id = Utils.get_ap_id(object_id)
653 with actor <- Containment.get_actor(data),
654 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
655 {:ok, object} <- get_obj_helper(object_id),
656 :ok <- Containment.contain_origin(actor.ap_id, object.data),
658 ActivityPub.delete(object, local: false, activity_id: id, actor: actor.ap_id) do
662 case User.get_cached_by_ap_id(object_id) do
663 %User{ap_id: ^actor} = user ->
678 "object" => %{"type" => "Announce", "object" => object_id},
684 with actor <- Containment.get_actor(data),
685 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
686 {:ok, object} <- get_obj_helper(object_id),
687 {:ok, activity, _} <- ActivityPub.unannounce(actor, object, id, false) do
697 "object" => %{"type" => "Follow", "object" => followed},
703 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
704 {:ok, %User{} = follower} <- User.get_or_fetch_by_ap_id(follower),
705 {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
706 User.unfollow(follower, followed)
716 "object" => %{"type" => "Block", "object" => blocked},
722 with %User{local: true} = blocked <- User.get_cached_by_ap_id(blocked),
723 {:ok, %User{} = blocker} <- User.get_or_fetch_by_ap_id(blocker),
724 {:ok, activity} <- ActivityPub.unblock(blocker, blocked, id, false) do
725 User.unblock(blocker, blocked)
733 %{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = _data,
736 with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked),
737 {:ok, %User{} = blocker} = User.get_or_fetch_by_ap_id(blocker),
738 {:ok, activity} <- ActivityPub.block(blocker, blocked, id, false) do
739 User.unfollow(blocker, blocked)
740 User.block(blocker, blocked)
750 "object" => %{"type" => "Like", "object" => object_id},
756 with actor <- Containment.get_actor(data),
757 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
758 {:ok, object} <- get_obj_helper(object_id),
759 {:ok, activity, _, _} <- ActivityPub.unlike(actor, object, id, false) do
766 # For Undos that don't have the complete object attached, try to find it in our database.
774 when is_binary(object) do
775 with %Activity{data: data} <- Activity.get_by_ap_id(object) do
777 |> Map.put("object", data)
778 |> handle_incoming(options)
784 def handle_incoming(_, _), do: :error
786 @spec get_obj_helper(String.t(), Keyword.t()) :: {:ok, Object.t()} | nil
787 def get_obj_helper(id, options \\ []) do
788 case Object.normalize(id, true, options) do
789 %Object{} = object -> {:ok, object}
794 @spec get_embedded_obj_helper(String.t() | Object.t(), User.t()) :: {:ok, Object.t()} | nil
795 def get_embedded_obj_helper(%{"attributedTo" => attributed_to, "id" => object_id} = data, %User{
798 when attributed_to == ap_id do
799 with {:ok, activity} <-
804 "actor" => attributed_to,
807 {:ok, Object.normalize(activity)}
809 _ -> get_obj_helper(object_id)
813 def get_embedded_obj_helper(object_id, _) do
814 get_obj_helper(object_id)
817 def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
818 with false <- String.starts_with?(in_reply_to, "http"),
819 {:ok, %{data: replied_to_object}} <- get_obj_helper(in_reply_to) do
820 Map.put(object, "inReplyTo", replied_to_object["external_url"] || in_reply_to)
826 def set_reply_to_uri(obj), do: obj
828 # Prepares the object of an outgoing create activity.
829 def prepare_object(object) do
836 |> prepare_attachments
839 |> strip_internal_fields
840 |> strip_internal_tags
846 # internal -> Mastodon
849 def prepare_outgoing(%{"type" => activity_type, "object" => object_id} = data)
850 when activity_type in ["Create", "Listen"] do
853 |> Object.normalize()
859 |> Map.put("object", object)
860 |> Map.merge(Utils.make_json_ld_header())
866 def prepare_outgoing(%{"type" => "Announce", "actor" => ap_id, "object" => object_id} = data) do
869 |> Object.normalize()
872 if Visibility.is_private?(object) && object.data["actor"] == ap_id do
873 data |> Map.put("object", object |> Map.get(:data) |> prepare_object)
875 data |> maybe_fix_object_url
880 |> strip_internal_fields
881 |> Map.merge(Utils.make_json_ld_header())
887 # Mastodon Accept/Reject requires a non-normalized object containing the actor URIs,
888 # because of course it does.
889 def prepare_outgoing(%{"type" => "Accept"} = data) do
890 with follow_activity <- Activity.normalize(data["object"]) do
892 "actor" => follow_activity.actor,
893 "object" => follow_activity.data["object"],
894 "id" => follow_activity.data["id"],
900 |> Map.put("object", object)
901 |> Map.merge(Utils.make_json_ld_header())
907 def prepare_outgoing(%{"type" => "Reject"} = data) do
908 with follow_activity <- Activity.normalize(data["object"]) do
910 "actor" => follow_activity.actor,
911 "object" => follow_activity.data["object"],
912 "id" => follow_activity.data["id"],
918 |> Map.put("object", object)
919 |> Map.merge(Utils.make_json_ld_header())
925 def prepare_outgoing(%{"type" => _type} = data) do
928 |> strip_internal_fields
929 |> maybe_fix_object_url
930 |> Map.merge(Utils.make_json_ld_header())
935 def maybe_fix_object_url(%{"object" => object} = data) when is_binary(object) do
936 with false <- String.starts_with?(object, "http"),
937 {:fetch, {:ok, relative_object}} <- {:fetch, get_obj_helper(object)},
938 %{data: %{"external_url" => external_url}} when not is_nil(external_url) <-
940 Map.put(data, "object", external_url)
943 Logger.error("Couldn't fetch #{object} #{inspect(e)}")
951 def maybe_fix_object_url(data), do: data
953 def add_hashtags(object) do
955 (object["tag"] || [])
957 # Expand internal representation tags into AS2 tags.
958 tag when is_binary(tag) ->
960 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
965 # Do not process tags which are already AS2 tag objects.
966 tag when is_map(tag) ->
970 Map.put(object, "tag", tags)
973 def add_mention_tags(object) do
976 |> Utils.get_notified_from_object()
977 |> Enum.map(&build_mention_tag/1)
979 tags = object["tag"] || []
981 Map.put(object, "tag", tags ++ mentions)
984 defp build_mention_tag(%{ap_id: ap_id, nickname: nickname} = _) do
985 %{"type" => "Mention", "href" => ap_id, "name" => "@#{nickname}"}
988 def take_emoji_tags(%User{info: %{emoji: emoji} = _user_info} = _user) do
990 |> Enum.flat_map(&Map.to_list/1)
991 |> Enum.map(&build_emoji_tag/1)
994 # TODO: we should probably send mtime instead of unix epoch time for updated
995 def add_emoji_tags(%{"emoji" => emoji} = object) do
996 tags = object["tag"] || []
998 out = Enum.map(emoji, &build_emoji_tag/1)
1000 Map.put(object, "tag", tags ++ out)
1003 def add_emoji_tags(object), do: object
1005 defp build_emoji_tag({name, url}) do
1007 "icon" => %{"url" => url, "type" => "Image"},
1008 "name" => ":" <> name <> ":",
1010 "updated" => "1970-01-01T00:00:00Z",
1015 def set_conversation(object) do
1016 Map.put(object, "conversation", object["context"])
1019 def set_sensitive(object) do
1020 tags = object["tag"] || []
1021 Map.put(object, "sensitive", "nsfw" in tags)
1024 def set_type(%{"type" => "Answer"} = object) do
1025 Map.put(object, "type", "Note")
1028 def set_type(object), do: object
1030 def add_attributed_to(object) do
1031 attributed_to = object["attributedTo"] || object["actor"]
1032 Map.put(object, "attributedTo", attributed_to)
1035 def prepare_attachments(object) do
1037 (object["attachment"] || [])
1038 |> Enum.map(fn data ->
1039 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
1040 %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
1043 Map.put(object, "attachment", attachments)
1046 defp strip_internal_fields(object) do
1048 |> Map.drop(Pleroma.Constants.object_internal_fields())
1051 defp strip_internal_tags(%{"tag" => tags} = object) do
1052 tags = Enum.filter(tags, fn x -> is_map(x) end)
1054 Map.put(object, "tag", tags)
1057 defp strip_internal_tags(object), do: object
1059 def perform(:user_upgrade, user) do
1060 # we pass a fake user so that the followers collection is stripped away
1061 old_follower_address = User.ap_followers(%User{nickname: user.nickname})
1066 where: ^old_follower_address in u.following,
1071 "array_replace(?,?,?)",
1073 ^old_follower_address,
1074 ^user.follower_address
1080 Repo.update_all(q, [])
1085 where: ^old_follower_address in a.recipients,
1090 "array_replace(?,?,?)",
1092 ^old_follower_address,
1093 ^user.follower_address
1099 Repo.update_all(q, [])
1102 def upgrade_user_from_ap_id(ap_id) do
1103 with %User{local: false} = user <- User.get_cached_by_ap_id(ap_id),
1104 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id),
1105 already_ap <- User.ap_enabled?(user),
1106 {:ok, user} <- upgrade_user(user, data) do
1107 if not already_ap do
1108 TransmogrifierWorker.enqueue("user_upgrade", %{"user_id" => user.id})
1113 %User{} = user -> {:ok, user}
1118 defp upgrade_user(user, data) do
1120 |> User.upgrade_changeset(data, true)
1121 |> User.update_and_set_cache()
1124 def maybe_fix_user_url(%{"url" => url} = data) when is_map(url) do
1125 Map.put(data, "url", url["href"])
1128 def maybe_fix_user_url(data), do: data
1130 def maybe_fix_user_object(data), do: maybe_fix_user_url(data)