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.FollowingRelationship
12 alias Pleroma.Object.Containment
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.ActivityPub.ObjectValidator
17 alias Pleroma.Web.ActivityPub.ObjectValidators.LikeValidator
18 alias Pleroma.Web.ActivityPub.Pipeline
19 alias Pleroma.Web.ActivityPub.Transmogrifier.ChatMessageHandling
20 alias Pleroma.Web.ActivityPub.Utils
21 alias Pleroma.Web.ActivityPub.Visibility
22 alias Pleroma.Web.Federator
23 alias Pleroma.Workers.TransmogrifierWorker
28 require Pleroma.Constants
31 Modifies an incoming AP object (mastodon format) to our internal format.
33 def fix_object(object, options \\ []) do
35 |> strip_internal_fields
40 |> fix_in_reply_to(options)
49 def fix_summary(%{"summary" => nil} = object) do
50 Map.put(object, "summary", "")
53 def fix_summary(%{"summary" => _} = object) do
54 # summary is present, nothing to do
58 def fix_summary(object), do: Map.put(object, "summary", "")
60 def fix_addressing_list(map, field) do
62 is_binary(map[field]) ->
63 Map.put(map, field, [map[field]])
66 Map.put(map, field, [])
73 def fix_explicit_addressing(
74 %{"to" => to, "cc" => cc} = object,
78 explicit_to = Enum.filter(to, fn x -> x in explicit_mentions end)
80 explicit_cc = Enum.filter(to, fn x -> x not in explicit_mentions end)
84 |> Enum.reject(fn x -> String.ends_with?(x, "/followers") and x != follower_collection end)
88 |> Map.put("to", explicit_to)
89 |> Map.put("cc", final_cc)
92 def fix_explicit_addressing(object, _explicit_mentions, _followers_collection), do: object
94 # if directMessage flag is set to true, leave the addressing alone
95 def fix_explicit_addressing(%{"directMessage" => true} = object), do: object
97 def fix_explicit_addressing(object) do
98 explicit_mentions = Utils.determine_explicit_mentions(object)
100 %User{follower_address: follower_collection} =
102 |> Containment.get_actor()
103 |> User.get_cached_by_ap_id()
108 Pleroma.Constants.as_public(),
112 fix_explicit_addressing(object, explicit_mentions, follower_collection)
115 # if as:Public is addressed, then make sure the followers collection is also addressed
116 # so that the activities will be delivered to local users.
117 def fix_implicit_addressing(%{"to" => to, "cc" => cc} = object, followers_collection) do
118 recipients = to ++ cc
120 if followers_collection not in recipients do
122 Pleroma.Constants.as_public() in cc ->
123 to = to ++ [followers_collection]
124 Map.put(object, "to", to)
126 Pleroma.Constants.as_public() in to ->
127 cc = cc ++ [followers_collection]
128 Map.put(object, "cc", cc)
138 def fix_implicit_addressing(object, _), do: object
140 def fix_addressing(object) do
141 {:ok, %User{} = user} = User.get_or_fetch_by_ap_id(object["actor"])
142 followers_collection = User.ap_followers(user)
145 |> fix_addressing_list("to")
146 |> fix_addressing_list("cc")
147 |> fix_addressing_list("bto")
148 |> fix_addressing_list("bcc")
149 |> fix_explicit_addressing()
150 |> fix_implicit_addressing(followers_collection)
153 def fix_actor(%{"attributedTo" => actor} = object) do
154 Map.put(object, "actor", Containment.get_actor(%{"actor" => actor}))
157 def fix_in_reply_to(object, options \\ [])
159 def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object, options)
160 when not is_nil(in_reply_to) do
161 in_reply_to_id = prepare_in_reply_to(in_reply_to)
162 object = Map.put(object, "inReplyToAtomUri", in_reply_to_id)
163 depth = (options[:depth] || 0) + 1
165 if Federator.allowed_thread_distance?(depth) do
166 with {:ok, replied_object} <- get_obj_helper(in_reply_to_id, options),
167 %Activity{} <- Activity.get_create_by_object_ap_id(replied_object.data["id"]) do
169 |> Map.put("inReplyTo", replied_object.data["id"])
170 |> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
171 |> Map.put("conversation", replied_object.data["context"] || object["conversation"])
172 |> Map.put("context", replied_object.data["context"] || object["conversation"])
175 Logger.error("Couldn't fetch #{inspect(in_reply_to_id)}, error: #{inspect(e)}")
183 def fix_in_reply_to(object, _options), do: object
185 defp prepare_in_reply_to(in_reply_to) do
187 is_bitstring(in_reply_to) ->
190 is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) ->
193 is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) ->
194 Enum.at(in_reply_to, 0)
201 def fix_context(object) do
202 context = object["context"] || object["conversation"] || Utils.generate_context_id()
205 |> Map.put("context", context)
206 |> Map.put("conversation", context)
209 def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
211 Enum.map(attachment, fn data ->
212 media_type = data["mediaType"] || data["mimeType"]
213 href = data["url"] || data["href"]
214 url = [%{"type" => "Link", "mediaType" => media_type, "href" => href}]
217 |> Map.put("mediaType", media_type)
218 |> Map.put("url", url)
221 Map.put(object, "attachment", attachments)
224 def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
226 |> Map.put("attachment", [attachment])
230 def fix_attachments(object), do: object
232 def fix_url(%{"url" => url} = object) when is_map(url) do
233 Map.put(object, "url", url["href"])
236 def fix_url(%{"type" => object_type, "url" => url} = object)
237 when object_type in ["Video", "Audio"] and is_list(url) do
238 first_element = Enum.at(url, 0)
240 link_element = Enum.find(url, fn x -> is_map(x) and x["mimeType"] == "text/html" end)
243 |> Map.put("attachment", [first_element])
244 |> Map.put("url", link_element["href"])
247 def fix_url(%{"type" => object_type, "url" => url} = object)
248 when object_type != "Video" and is_list(url) do
249 first_element = Enum.at(url, 0)
253 is_bitstring(first_element) -> first_element
254 is_map(first_element) -> first_element["href"] || ""
258 Map.put(object, "url", url_string)
261 def fix_url(object), do: object
263 def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
266 |> Enum.filter(fn data -> data["type"] == "Emoji" and data["icon"] end)
267 |> Enum.reduce(%{}, fn data, mapping ->
268 name = String.trim(data["name"], ":")
270 Map.put(mapping, name, data["icon"]["url"])
273 # we merge mastodon and pleroma emoji into a single mapping, to allow for both wire formats
274 emoji = Map.merge(object["emoji"] || %{}, emoji)
276 Map.put(object, "emoji", emoji)
279 def fix_emoji(%{"tag" => %{"type" => "Emoji"} = tag} = object) do
280 name = String.trim(tag["name"], ":")
281 emoji = %{name => tag["icon"]["url"]}
283 Map.put(object, "emoji", emoji)
286 def fix_emoji(object), do: object
288 def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
291 |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
292 |> Enum.map(fn data -> String.slice(data["name"], 1..-1) end)
294 Map.put(object, "tag", tag ++ tags)
297 def fix_tag(%{"tag" => %{"type" => "Hashtag", "name" => hashtag} = tag} = object) do
298 combined = [tag, String.slice(hashtag, 1..-1)]
300 Map.put(object, "tag", combined)
303 def fix_tag(%{"tag" => %{} = tag} = object), do: Map.put(object, "tag", [tag])
305 def fix_tag(object), do: object
307 # content map usually only has one language so this will do for now.
308 def fix_content_map(%{"contentMap" => content_map} = object) do
309 content_groups = Map.to_list(content_map)
310 {_, content} = Enum.at(content_groups, 0)
312 Map.put(object, "content", content)
315 def fix_content_map(object), do: object
317 def fix_type(object, options \\ [])
319 def fix_type(%{"inReplyTo" => reply_id, "name" => _} = object, options)
320 when is_binary(reply_id) do
321 with true <- Federator.allowed_thread_distance?(options[:depth]),
322 {:ok, %{data: %{"type" => "Question"} = _} = _} <- get_obj_helper(reply_id, options) do
323 Map.put(object, "type", "Answer")
329 def fix_type(object, _), do: object
331 defp mastodon_follow_hack(%{"id" => id, "actor" => follower_id}, followed) do
332 with true <- id =~ "follows",
333 %User{local: true} = follower <- User.get_cached_by_ap_id(follower_id),
334 %Activity{} = activity <- Utils.fetch_latest_follow(follower, followed) do
341 defp mastodon_follow_hack(_, _), do: {:error, nil}
343 defp get_follow_activity(follow_object, followed) do
344 with object_id when not is_nil(object_id) <- Utils.get_ap_id(follow_object),
345 {_, %Activity{} = activity} <- {:activity, Activity.get_by_ap_id(object_id)} do
348 # Can't find the activity. This might a Mastodon 2.3 "Accept"
350 mastodon_follow_hack(follow_object, followed)
357 # Reduce the object list to find the reported user.
358 defp get_reported(objects) do
359 Enum.reduce_while(objects, nil, fn ap_id, _ ->
360 with %User{} = user <- User.get_cached_by_ap_id(ap_id) do
368 def handle_incoming(data, options \\ [])
370 # Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them
372 def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} = data, _options) do
373 with context <- data["context"] || Utils.generate_context_id(),
374 content <- data["content"] || "",
375 %User{} = actor <- User.get_cached_by_ap_id(actor),
376 # Reduce the object list to find the reported user.
377 %User{} = account <- get_reported(objects),
378 # Remove the reported user from the object list.
379 statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do
386 additional: %{"cc" => [account.ap_id]}
388 |> ActivityPub.flag()
392 # disallow objects with bogus IDs
393 def handle_incoming(%{"id" => nil}, _options), do: :error
394 def handle_incoming(%{"id" => ""}, _options), do: :error
395 # length of https:// = 8, should validate better, but good enough for now.
396 def handle_incoming(%{"id" => id}, _options) when is_binary(id) and byte_size(id) < 8,
399 # TODO: validate those with a Ecto scheme
403 %{"type" => "Create", "object" => %{"type" => objtype} = object} = data,
406 when objtype in ["Article", "Event", "Note", "Video", "Page", "Question", "Answer", "Audio"] do
407 actor = Containment.get_actor(data)
410 Map.put(data, "actor", actor)
413 with nil <- Activity.get_create_by_object_ap_id(object["id"]),
414 {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
415 object = fix_object(object, options)
421 context: object["conversation"],
423 published: data["published"],
432 with {:ok, created_activity} <- ActivityPub.create(params) do
433 reply_depth = (options[:depth] || 0) + 1
435 if Federator.allowed_thread_distance?(reply_depth) do
436 for reply_id <- replies(object) do
437 Pleroma.Workers.RemoteFetcherWorker.enqueue("fetch_remote", %{
439 "depth" => reply_depth
444 {:ok, created_activity}
447 %Activity{} = activity -> {:ok, activity}
453 %{"type" => "Listen", "object" => %{"type" => "Audio"} = object} = data,
456 actor = Containment.get_actor(data)
459 Map.put(data, "actor", actor)
462 with {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
463 reply_depth = (options[:depth] || 0) + 1
464 options = Keyword.put(options, :depth, reply_depth)
465 object = fix_object(object, options)
473 published: data["published"],
474 additional: Map.take(data, ["cc", "id"])
477 ActivityPub.listen(params)
484 %{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data,
487 with %User{local: true} = followed <-
488 User.get_cached_by_ap_id(Containment.get_actor(%{"actor" => followed})),
489 {:ok, %User{} = follower} <-
490 User.get_or_fetch_by_ap_id(Containment.get_actor(%{"actor" => follower})),
491 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
492 with deny_follow_blocked <- Pleroma.Config.get([:user, :deny_follow_blocked]),
493 {_, false} <- {:user_blocked, User.blocks?(followed, follower) && deny_follow_blocked},
494 {_, false} <- {:user_locked, User.locked?(followed)},
495 {_, {:ok, follower}} <- {:follow, User.follow(follower, followed)},
497 {:follow_state_update, Utils.update_follow_state_for_all(activity, "accept")},
498 {:ok, _relationship} <- FollowingRelationship.update(follower, followed, "accept") do
499 ActivityPub.accept(%{
500 to: [follower.ap_id],
506 {:user_blocked, true} ->
507 {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
508 {:ok, _relationship} = FollowingRelationship.update(follower, followed, "reject")
510 ActivityPub.reject(%{
511 to: [follower.ap_id],
517 {:follow, {:error, _}} ->
518 {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
519 {:ok, _relationship} = FollowingRelationship.update(follower, followed, "reject")
521 ActivityPub.reject(%{
522 to: [follower.ap_id],
528 {:user_locked, true} ->
529 {:ok, _relationship} = FollowingRelationship.update(follower, followed, "pending")
541 %{"type" => "Accept", "object" => follow_object, "actor" => _actor, "id" => id} = data,
544 with actor <- Containment.get_actor(data),
545 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
546 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
547 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "accept"),
548 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
549 {:ok, _relationship} <- FollowingRelationship.update(follower, followed, "accept") do
550 ActivityPub.accept(%{
551 to: follow_activity.data["to"],
554 object: follow_activity.data["id"],
564 %{"type" => "Reject", "object" => follow_object, "actor" => _actor, "id" => id} = data,
567 with actor <- Containment.get_actor(data),
568 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
569 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
570 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "reject"),
571 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
572 {:ok, _relationship} <- FollowingRelationship.update(follower, followed, "reject"),
574 ActivityPub.reject(%{
575 to: follow_activity.data["to"],
578 object: follow_activity.data["id"],
588 @misskey_reactions %{
602 @doc "Rewrite misskey likes into EmojiReacts"
606 "_misskey_reaction" => reaction
611 |> Map.put("type", "EmojiReact")
612 |> Map.put("content", @misskey_reactions[reaction] || reaction)
613 |> handle_incoming(options)
617 %{"type" => "Create", "object" => %{"type" => "ChatMessage"}} = data,
620 do: ChatMessageHandling.handle_incoming(data, options)
622 def handle_incoming(%{"type" => "Like"} = data, _options) do
623 with {_, {:ok, cast_data_sym}} <-
625 data |> LikeValidator.cast_data() |> Ecto.Changeset.apply_action(:insert)},
626 cast_data = ObjectValidator.stringify_keys(Map.from_struct(cast_data_sym)),
627 :ok <- ObjectValidator.fetch_actor_and_object(cast_data),
628 {_, {:ok, cast_data}} <- {:ensure_context_presence, ensure_context_presence(cast_data)},
629 {_, {:ok, cast_data}} <-
630 {:ensure_recipients_presence, ensure_recipients_presence(cast_data)},
631 {_, {:ok, activity, _meta}} <-
632 {:common_pipeline, Pipeline.common_pipeline(cast_data, local: false)} do
641 "type" => "EmojiReact",
642 "object" => object_id,
649 with actor <- Containment.get_actor(data),
650 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
651 {:ok, object} <- get_obj_helper(object_id),
652 {:ok, activity, _object} <-
653 ActivityPub.react_with_emoji(actor, object, emoji, activity_id: id, local: false) do
661 %{"type" => "Announce", "object" => object_id, "actor" => _actor, "id" => id} = data,
664 with actor <- Containment.get_actor(data),
665 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
666 {:ok, object} <- get_embedded_obj_helper(object_id, actor),
667 public <- Visibility.is_public?(data),
668 {:ok, activity, _object} <- ActivityPub.announce(actor, object, id, false, public) do
676 %{"type" => "Update", "object" => %{"type" => object_type} = object, "actor" => actor_id} =
680 when object_type in [
686 with %User{ap_id: ^actor_id} = actor <- User.get_cached_by_ap_id(object["id"]) do
687 {:ok, new_user_data} = ActivityPub.user_data_from_user_object(object)
690 |> User.upgrade_changeset(new_user_data, true)
691 |> User.update_and_set_cache()
693 ActivityPub.update(%{
695 to: data["to"] || [],
696 cc: data["cc"] || [],
699 activity_id: data["id"]
708 # TODO: We presently assume that any actor on the same origin domain as the object being
709 # deleted has the rights to delete that object. A better way to validate whether or not
710 # the object should be deleted is to refetch the object URI, which should return either
711 # an error or a tombstone. This would allow us to verify that a deletion actually took
714 %{"type" => "Delete", "object" => object_id, "actor" => actor, "id" => id} = data,
717 object_id = Utils.get_ap_id(object_id)
719 with actor <- Containment.get_actor(data),
720 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
721 {:ok, object} <- get_obj_helper(object_id),
722 :ok <- Containment.contain_origin(actor.ap_id, object.data),
724 ActivityPub.delete(object, local: false, activity_id: id, actor: actor.ap_id) do
728 case User.get_cached_by_ap_id(object_id) do
729 %User{ap_id: ^actor} = user ->
744 "object" => %{"type" => "Announce", "object" => object_id},
750 with actor <- Containment.get_actor(data),
751 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
752 {:ok, object} <- get_obj_helper(object_id),
753 {:ok, activity, _} <- ActivityPub.unannounce(actor, object, id, false) do
763 "object" => %{"type" => "Follow", "object" => followed},
769 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
770 {:ok, %User{} = follower} <- User.get_or_fetch_by_ap_id(follower),
771 {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
772 User.unfollow(follower, followed)
782 "object" => %{"type" => "EmojiReact", "id" => reaction_activity_id},
788 with actor <- Containment.get_actor(data),
789 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
790 {:ok, activity, _} <-
791 ActivityPub.unreact_with_emoji(actor, reaction_activity_id,
804 "object" => %{"type" => "Block", "object" => blocked},
810 with %User{local: true} = blocked <- User.get_cached_by_ap_id(blocked),
811 {:ok, %User{} = blocker} <- User.get_or_fetch_by_ap_id(blocker),
812 {:ok, activity} <- ActivityPub.unblock(blocker, blocked, id, false) do
813 User.unblock(blocker, blocked)
821 %{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = _data,
824 with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked),
825 {:ok, %User{} = blocker} = User.get_or_fetch_by_ap_id(blocker),
826 {:ok, activity} <- ActivityPub.block(blocker, blocked, id, false) do
827 User.unfollow(blocker, blocked)
828 User.block(blocker, blocked)
838 "object" => %{"type" => "Like", "object" => object_id},
844 with actor <- Containment.get_actor(data),
845 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
846 {:ok, object} <- get_obj_helper(object_id),
847 {:ok, activity, _, _} <- ActivityPub.unlike(actor, object, id, false) do
854 # For Undos that don't have the complete object attached, try to find it in our database.
862 when is_binary(object) do
863 with %Activity{data: data} <- Activity.get_by_ap_id(object) do
865 |> Map.put("object", data)
866 |> handle_incoming(options)
875 "actor" => origin_actor,
876 "object" => origin_actor,
877 "target" => target_actor
881 with %User{} = origin_user <- User.get_cached_by_ap_id(origin_actor),
882 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_actor),
883 true <- origin_actor in target_user.also_known_as do
884 ActivityPub.move(origin_user, target_user, false)
890 def handle_incoming(_, _), do: :error
892 @spec get_obj_helper(String.t(), Keyword.t()) :: {:ok, Object.t()} | nil
893 def get_obj_helper(id, options \\ []) do
894 case Object.normalize(id, true, options) do
895 %Object{} = object -> {:ok, object}
900 @spec get_embedded_obj_helper(String.t() | Object.t(), User.t()) :: {:ok, Object.t()} | nil
901 def get_embedded_obj_helper(%{"attributedTo" => attributed_to, "id" => object_id} = data, %User{
904 when attributed_to == ap_id do
905 with {:ok, activity} <-
910 "actor" => attributed_to,
913 {:ok, Object.normalize(activity)}
915 _ -> get_obj_helper(object_id)
919 def get_embedded_obj_helper(object_id, _) do
920 get_obj_helper(object_id)
923 def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
924 with false <- String.starts_with?(in_reply_to, "http"),
925 {:ok, %{data: replied_to_object}} <- get_obj_helper(in_reply_to) do
926 Map.put(object, "inReplyTo", replied_to_object["external_url"] || in_reply_to)
932 def set_reply_to_uri(obj), do: obj
935 Serialized Mastodon-compatible `replies` collection containing _self-replies_.
936 Based on Mastodon's ActivityPub::NoteSerializer#replies.
938 def set_replies(obj_data) do
940 with limit when limit > 0 <-
941 Pleroma.Config.get([:activitypub, :note_replies_output_limit], 0),
942 %Object{} = object <- Object.get_cached_by_ap_id(obj_data["id"]) do
944 |> Object.self_replies()
945 |> select([o], fragment("?->>'id'", o.data))
952 set_replies(obj_data, replies_uris)
955 defp set_replies(obj, []) do
959 defp set_replies(obj, replies_uris) do
960 replies_collection = %{
961 "type" => "Collection",
962 "items" => replies_uris
965 Map.merge(obj, %{"replies" => replies_collection})
968 def replies(%{"replies" => %{"first" => %{"items" => items}}}) when not is_nil(items) do
972 def replies(%{"replies" => %{"items" => items}}) when not is_nil(items) do
976 def replies(_), do: []
978 # Prepares the object of an outgoing create activity.
979 def prepare_object(object) do
986 |> prepare_attachments
990 |> strip_internal_fields
991 |> strip_internal_tags
997 # internal -> Mastodon
1000 def prepare_outgoing(%{"type" => activity_type, "object" => object_id} = data)
1001 when activity_type in ["Create", "Listen"] do
1004 |> Object.normalize()
1010 |> Map.put("object", object)
1011 |> Map.merge(Utils.make_json_ld_header())
1012 |> Map.delete("bcc")
1017 def prepare_outgoing(%{"type" => "Announce", "actor" => ap_id, "object" => object_id} = data) do
1020 |> Object.normalize()
1023 if Visibility.is_private?(object) && object.data["actor"] == ap_id do
1024 data |> Map.put("object", object |> Map.get(:data) |> prepare_object)
1026 data |> maybe_fix_object_url
1031 |> strip_internal_fields
1032 |> Map.merge(Utils.make_json_ld_header())
1033 |> Map.delete("bcc")
1038 # Mastodon Accept/Reject requires a non-normalized object containing the actor URIs,
1039 # because of course it does.
1040 def prepare_outgoing(%{"type" => "Accept"} = data) do
1041 with follow_activity <- Activity.normalize(data["object"]) do
1043 "actor" => follow_activity.actor,
1044 "object" => follow_activity.data["object"],
1045 "id" => follow_activity.data["id"],
1051 |> Map.put("object", object)
1052 |> Map.merge(Utils.make_json_ld_header())
1058 def prepare_outgoing(%{"type" => "Reject"} = data) do
1059 with follow_activity <- Activity.normalize(data["object"]) do
1061 "actor" => follow_activity.actor,
1062 "object" => follow_activity.data["object"],
1063 "id" => follow_activity.data["id"],
1069 |> Map.put("object", object)
1070 |> Map.merge(Utils.make_json_ld_header())
1076 def prepare_outgoing(%{"type" => _type} = data) do
1079 |> strip_internal_fields
1080 |> maybe_fix_object_url
1081 |> Map.merge(Utils.make_json_ld_header())
1086 def maybe_fix_object_url(%{"object" => object} = data) when is_binary(object) do
1087 with false <- String.starts_with?(object, "http"),
1088 {:fetch, {:ok, relative_object}} <- {:fetch, get_obj_helper(object)},
1089 %{data: %{"external_url" => external_url}} when not is_nil(external_url) <-
1091 Map.put(data, "object", external_url)
1094 Logger.error("Couldn't fetch #{object} #{inspect(e)}")
1102 def maybe_fix_object_url(data), do: data
1104 def add_hashtags(object) do
1106 (object["tag"] || [])
1108 # Expand internal representation tags into AS2 tags.
1109 tag when is_binary(tag) ->
1111 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
1112 "name" => "##{tag}",
1116 # Do not process tags which are already AS2 tag objects.
1117 tag when is_map(tag) ->
1121 Map.put(object, "tag", tags)
1124 def add_mention_tags(object) do
1125 {enabled_receivers, disabled_receivers} = Utils.get_notified_from_object(object)
1126 potential_receivers = enabled_receivers ++ disabled_receivers
1127 mentions = Enum.map(potential_receivers, &build_mention_tag/1)
1129 tags = object["tag"] || []
1130 Map.put(object, "tag", tags ++ mentions)
1133 defp build_mention_tag(%{ap_id: ap_id, nickname: nickname} = _) do
1134 %{"type" => "Mention", "href" => ap_id, "name" => "@#{nickname}"}
1137 def take_emoji_tags(%User{emoji: emoji}) do
1139 |> Enum.flat_map(&Map.to_list/1)
1140 |> Enum.map(&build_emoji_tag/1)
1143 # TODO: we should probably send mtime instead of unix epoch time for updated
1144 def add_emoji_tags(%{"emoji" => emoji} = object) do
1145 tags = object["tag"] || []
1147 out = Enum.map(emoji, &build_emoji_tag/1)
1149 Map.put(object, "tag", tags ++ out)
1152 def add_emoji_tags(object), do: object
1154 defp build_emoji_tag({name, url}) do
1156 "icon" => %{"url" => url, "type" => "Image"},
1157 "name" => ":" <> name <> ":",
1159 "updated" => "1970-01-01T00:00:00Z",
1164 def set_conversation(object) do
1165 Map.put(object, "conversation", object["context"])
1168 def set_sensitive(object) do
1169 tags = object["tag"] || []
1170 Map.put(object, "sensitive", "nsfw" in tags)
1173 def set_type(%{"type" => "Answer"} = object) do
1174 Map.put(object, "type", "Note")
1177 def set_type(object), do: object
1179 def add_attributed_to(object) do
1180 attributed_to = object["attributedTo"] || object["actor"]
1181 Map.put(object, "attributedTo", attributed_to)
1184 def prepare_attachments(object) do
1186 (object["attachment"] || [])
1187 |> Enum.map(fn data ->
1188 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
1189 %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
1192 Map.put(object, "attachment", attachments)
1195 def strip_internal_fields(object) do
1197 |> Map.drop(Pleroma.Constants.object_internal_fields())
1200 defp strip_internal_tags(%{"tag" => tags} = object) do
1201 tags = Enum.filter(tags, fn x -> is_map(x) end)
1203 Map.put(object, "tag", tags)
1206 defp strip_internal_tags(object), do: object
1208 def perform(:user_upgrade, user) do
1209 # we pass a fake user so that the followers collection is stripped away
1210 old_follower_address = User.ap_followers(%User{nickname: user.nickname})
1214 where: ^old_follower_address in a.recipients,
1219 "array_replace(?,?,?)",
1221 ^old_follower_address,
1222 ^user.follower_address
1227 |> Repo.update_all([])
1230 def upgrade_user_from_ap_id(ap_id) do
1231 with %User{local: false} = user <- User.get_cached_by_ap_id(ap_id),
1232 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id),
1233 already_ap <- User.ap_enabled?(user),
1234 {:ok, user} <- upgrade_user(user, data) do
1235 if not already_ap do
1236 TransmogrifierWorker.enqueue("user_upgrade", %{"user_id" => user.id})
1241 %User{} = user -> {:ok, user}
1246 defp upgrade_user(user, data) do
1248 |> User.upgrade_changeset(data, true)
1249 |> User.update_and_set_cache()
1252 def maybe_fix_user_url(%{"url" => url} = data) when is_map(url) do
1253 Map.put(data, "url", url["href"])
1256 def maybe_fix_user_url(data), do: data
1258 def maybe_fix_user_object(data), do: maybe_fix_user_url(data)
1260 defp ensure_context_presence(%{"context" => context} = data) when is_binary(context),
1263 defp ensure_context_presence(%{"object" => object} = data) when is_binary(object) do
1264 with %{data: %{"context" => context}} when is_binary(context) <- Object.normalize(object) do
1265 {:ok, Map.put(data, "context", context)}
1268 {:error, :no_context}
1272 defp ensure_context_presence(_) do
1273 {:error, :no_context}
1276 defp ensure_recipients_presence(%{"to" => [_ | _], "cc" => [_ | _]} = data),
1279 defp ensure_recipients_presence(%{"object" => object} = data) do
1280 case Object.normalize(object) do
1281 %{data: %{"actor" => actor}} ->
1284 |> Map.put("to", [actor])
1285 |> Map.put("cc", data["cc"] || [])
1290 {:error, :no_object}
1297 defp ensure_recipients_presence(_) do
1298 {:error, :no_object}