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.FollowingRelationship
14 alias Pleroma.Object.Containment
17 alias Pleroma.Web.ActivityPub.ActivityPub
18 alias Pleroma.Web.ActivityPub.Builder
19 alias Pleroma.Web.ActivityPub.ObjectValidator
20 alias Pleroma.Web.ActivityPub.ObjectValidators.Types
21 alias Pleroma.Web.ActivityPub.Pipeline
22 alias Pleroma.Web.ActivityPub.Utils
23 alias Pleroma.Web.ActivityPub.Visibility
24 alias Pleroma.Web.Federator
25 alias Pleroma.Workers.TransmogrifierWorker
30 require Pleroma.Constants
33 Modifies an incoming AP object (mastodon format) to our internal format.
35 def fix_object(object, options \\ []) do
37 |> strip_internal_fields
42 |> fix_in_reply_to(options)
52 def fix_summary(%{"summary" => nil} = object) do
53 Map.put(object, "summary", "")
56 def fix_summary(%{"summary" => _} = object) do
57 # summary is present, nothing to do
61 def fix_summary(object), do: Map.put(object, "summary", "")
63 def fix_addressing_list(map, field) do
65 is_binary(map[field]) ->
66 Map.put(map, field, [map[field]])
69 Map.put(map, field, [])
76 def fix_explicit_addressing(
77 %{"to" => to, "cc" => cc} = object,
81 explicit_to = Enum.filter(to, fn x -> x in explicit_mentions end)
83 explicit_cc = Enum.filter(to, fn x -> x not in explicit_mentions end)
87 |> Enum.reject(fn x -> String.ends_with?(x, "/followers") and x != follower_collection end)
91 |> Map.put("to", explicit_to)
92 |> Map.put("cc", final_cc)
95 def fix_explicit_addressing(object, _explicit_mentions, _followers_collection), do: object
97 # if directMessage flag is set to true, leave the addressing alone
98 def fix_explicit_addressing(%{"directMessage" => true} = object), do: object
100 def fix_explicit_addressing(object) do
101 explicit_mentions = Utils.determine_explicit_mentions(object)
103 %User{follower_address: follower_collection} =
105 |> Containment.get_actor()
106 |> User.get_cached_by_ap_id()
111 Pleroma.Constants.as_public(),
115 fix_explicit_addressing(object, explicit_mentions, follower_collection)
118 # if as:Public is addressed, then make sure the followers collection is also addressed
119 # so that the activities will be delivered to local users.
120 def fix_implicit_addressing(%{"to" => to, "cc" => cc} = object, followers_collection) do
121 recipients = to ++ cc
123 if followers_collection not in recipients do
125 Pleroma.Constants.as_public() in cc ->
126 to = to ++ [followers_collection]
127 Map.put(object, "to", to)
129 Pleroma.Constants.as_public() in to ->
130 cc = cc ++ [followers_collection]
131 Map.put(object, "cc", cc)
141 def fix_implicit_addressing(object, _), do: object
143 def fix_addressing(object) do
144 {:ok, %User{} = user} = User.get_or_fetch_by_ap_id(object["actor"])
145 followers_collection = User.ap_followers(user)
148 |> fix_addressing_list("to")
149 |> fix_addressing_list("cc")
150 |> fix_addressing_list("bto")
151 |> fix_addressing_list("bcc")
152 |> fix_explicit_addressing()
153 |> fix_implicit_addressing(followers_collection)
156 def fix_actor(%{"attributedTo" => actor} = object) do
157 Map.put(object, "actor", Containment.get_actor(%{"actor" => actor}))
160 def fix_in_reply_to(object, options \\ [])
162 def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object, options)
163 when not is_nil(in_reply_to) do
164 in_reply_to_id = prepare_in_reply_to(in_reply_to)
165 object = Map.put(object, "inReplyToAtomUri", in_reply_to_id)
166 depth = (options[:depth] || 0) + 1
168 if Federator.allowed_thread_distance?(depth) do
169 with {:ok, replied_object} <- get_obj_helper(in_reply_to_id, options),
170 %Activity{} <- Activity.get_create_by_object_ap_id(replied_object.data["id"]) do
172 |> Map.put("inReplyTo", replied_object.data["id"])
173 |> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
174 |> Map.put("conversation", replied_object.data["context"] || object["conversation"])
175 |> Map.put("context", replied_object.data["context"] || object["conversation"])
178 Logger.error("Couldn't fetch #{inspect(in_reply_to_id)}, error: #{inspect(e)}")
186 def fix_in_reply_to(object, _options), do: object
188 defp prepare_in_reply_to(in_reply_to) do
190 is_bitstring(in_reply_to) ->
193 is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) ->
196 is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) ->
197 Enum.at(in_reply_to, 0)
204 def fix_context(object) do
205 context = object["context"] || object["conversation"] || Utils.generate_context_id()
208 |> Map.put("context", context)
209 |> Map.put("conversation", context)
212 def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
214 Enum.map(attachment, fn data ->
217 is_list(data["url"]) -> List.first(data["url"])
218 is_map(data["url"]) -> data["url"]
224 is_map(url) && MIME.valid?(url["mediaType"]) -> url["mediaType"]
225 MIME.valid?(data["mediaType"]) -> data["mediaType"]
226 MIME.valid?(data["mimeType"]) -> data["mimeType"]
232 is_map(url) && is_binary(url["href"]) -> url["href"]
233 is_binary(data["url"]) -> data["url"]
234 is_binary(data["href"]) -> data["href"]
239 |> Maps.put_if_present("mediaType", media_type)
240 |> Maps.put_if_present("type", Map.get(url || %{}, "type"))
242 %{"url" => [attachment_url]}
243 |> Maps.put_if_present("mediaType", media_type)
244 |> Maps.put_if_present("type", data["type"])
245 |> Maps.put_if_present("name", data["name"])
248 Map.put(object, "attachment", attachments)
251 def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
253 |> Map.put("attachment", [attachment])
257 def fix_attachments(object), do: object
259 def fix_url(%{"url" => url} = object) when is_map(url) do
260 Map.put(object, "url", url["href"])
263 def fix_url(%{"type" => object_type, "url" => url} = object)
264 when object_type in ["Video", "Audio"] and is_list(url) do
265 first_element = Enum.at(url, 0)
267 link_element = Enum.find(url, fn x -> is_map(x) and x["mimeType"] == "text/html" end)
270 |> Map.put("attachment", [first_element])
271 |> Map.put("url", link_element["href"])
274 def fix_url(%{"type" => object_type, "url" => url} = object)
275 when object_type != "Video" and is_list(url) do
276 first_element = Enum.at(url, 0)
280 is_bitstring(first_element) -> first_element
281 is_map(first_element) -> first_element["href"] || ""
285 Map.put(object, "url", url_string)
288 def fix_url(object), do: object
290 def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
293 |> Enum.filter(fn data -> data["type"] == "Emoji" and data["icon"] end)
294 |> Enum.reduce(%{}, fn data, mapping ->
295 name = String.trim(data["name"], ":")
297 Map.put(mapping, name, data["icon"]["url"])
300 # we merge mastodon and pleroma emoji into a single mapping, to allow for both wire formats
301 emoji = Map.merge(object["emoji"] || %{}, emoji)
303 Map.put(object, "emoji", emoji)
306 def fix_emoji(%{"tag" => %{"type" => "Emoji"} = tag} = object) do
307 name = String.trim(tag["name"], ":")
308 emoji = %{name => tag["icon"]["url"]}
310 Map.put(object, "emoji", emoji)
313 def fix_emoji(object), do: object
315 def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
318 |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
319 |> Enum.map(fn data -> String.slice(data["name"], 1..-1) end)
321 Map.put(object, "tag", tag ++ tags)
324 def fix_tag(%{"tag" => %{"type" => "Hashtag", "name" => hashtag} = tag} = object) do
325 combined = [tag, String.slice(hashtag, 1..-1)]
327 Map.put(object, "tag", combined)
330 def fix_tag(%{"tag" => %{} = tag} = object), do: Map.put(object, "tag", [tag])
332 def fix_tag(object), do: object
334 # content map usually only has one language so this will do for now.
335 def fix_content_map(%{"contentMap" => content_map} = object) do
336 content_groups = Map.to_list(content_map)
337 {_, content} = Enum.at(content_groups, 0)
339 Map.put(object, "content", content)
342 def fix_content_map(object), do: object
344 def fix_type(object, options \\ [])
346 def fix_type(%{"inReplyTo" => reply_id, "name" => _} = object, options)
347 when is_binary(reply_id) do
348 with true <- Federator.allowed_thread_distance?(options[:depth]),
349 {:ok, %{data: %{"type" => "Question"} = _} = _} <- get_obj_helper(reply_id, options) do
350 Map.put(object, "type", "Answer")
356 def fix_type(object, _), do: object
358 defp fix_content(%{"mediaType" => "text/markdown", "content" => content} = object)
359 when is_binary(content) do
362 |> Earmark.as_html!(%Earmark.Options{renderer: EarmarkRenderer})
363 |> Pleroma.HTML.filter_tags()
365 Map.merge(object, %{"content" => html_content, "mediaType" => "text/html"})
368 defp fix_content(object), do: object
370 defp mastodon_follow_hack(%{"id" => id, "actor" => follower_id}, followed) do
371 with true <- id =~ "follows",
372 %User{local: true} = follower <- User.get_cached_by_ap_id(follower_id),
373 %Activity{} = activity <- Utils.fetch_latest_follow(follower, followed) do
380 defp mastodon_follow_hack(_, _), do: {:error, nil}
382 defp get_follow_activity(follow_object, followed) do
383 with object_id when not is_nil(object_id) <- Utils.get_ap_id(follow_object),
384 {_, %Activity{} = activity} <- {:activity, Activity.get_by_ap_id(object_id)} do
387 # Can't find the activity. This might a Mastodon 2.3 "Accept"
389 mastodon_follow_hack(follow_object, followed)
396 # Reduce the object list to find the reported user.
397 defp get_reported(objects) do
398 Enum.reduce_while(objects, nil, fn ap_id, _ ->
399 with %User{} = user <- User.get_cached_by_ap_id(ap_id) do
407 def handle_incoming(data, options \\ [])
409 # Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them
411 def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} = data, _options) do
412 with context <- data["context"] || Utils.generate_context_id(),
413 content <- data["content"] || "",
414 %User{} = actor <- User.get_cached_by_ap_id(actor),
415 # Reduce the object list to find the reported user.
416 %User{} = account <- get_reported(objects),
417 # Remove the reported user from the object list.
418 statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do
425 additional: %{"cc" => [account.ap_id]}
427 |> ActivityPub.flag()
431 # disallow objects with bogus IDs
432 def handle_incoming(%{"id" => nil}, _options), do: :error
433 def handle_incoming(%{"id" => ""}, _options), do: :error
434 # length of https:// = 8, should validate better, but good enough for now.
435 def handle_incoming(%{"id" => id}, _options) when is_binary(id) and byte_size(id) < 8,
438 # TODO: validate those with a Ecto scheme
442 %{"type" => "Create", "object" => %{"type" => objtype} = object} = data,
445 when objtype in ["Article", "Event", "Note", "Video", "Page", "Question", "Answer", "Audio"] do
446 actor = Containment.get_actor(data)
449 Map.put(data, "actor", actor)
452 with nil <- Activity.get_create_by_object_ap_id(object["id"]),
453 {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
454 object = fix_object(object, options)
460 context: object["conversation"],
462 published: data["published"],
471 with {:ok, created_activity} <- ActivityPub.create(params) do
472 reply_depth = (options[:depth] || 0) + 1
474 if Federator.allowed_thread_distance?(reply_depth) do
475 for reply_id <- replies(object) do
476 Pleroma.Workers.RemoteFetcherWorker.enqueue("fetch_remote", %{
478 "depth" => reply_depth
483 {:ok, created_activity}
486 %Activity{} = activity -> {:ok, activity}
492 %{"type" => "Listen", "object" => %{"type" => "Audio"} = object} = data,
495 actor = Containment.get_actor(data)
498 Map.put(data, "actor", actor)
501 with {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
502 reply_depth = (options[:depth] || 0) + 1
503 options = Keyword.put(options, :depth, reply_depth)
504 object = fix_object(object, options)
512 published: data["published"],
513 additional: Map.take(data, ["cc", "id"])
516 ActivityPub.listen(params)
523 %{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data,
526 with %User{local: true} = followed <-
527 User.get_cached_by_ap_id(Containment.get_actor(%{"actor" => followed})),
528 {:ok, %User{} = follower} <-
529 User.get_or_fetch_by_ap_id(Containment.get_actor(%{"actor" => follower})),
530 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
531 with deny_follow_blocked <- Pleroma.Config.get([:user, :deny_follow_blocked]),
532 {_, false} <- {:user_blocked, User.blocks?(followed, follower) && deny_follow_blocked},
533 {_, false} <- {:user_locked, User.locked?(followed)},
534 {_, {:ok, follower}} <- {:follow, User.follow(follower, followed)},
536 {:follow_state_update, Utils.update_follow_state_for_all(activity, "accept")},
537 {:ok, _relationship} <-
538 FollowingRelationship.update(follower, followed, :follow_accept) do
539 ActivityPub.accept(%{
540 to: [follower.ap_id],
546 {:user_blocked, true} ->
547 {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
548 {:ok, _relationship} = FollowingRelationship.update(follower, followed, :follow_reject)
550 ActivityPub.reject(%{
551 to: [follower.ap_id],
557 {:follow, {:error, _}} ->
558 {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
559 {:ok, _relationship} = FollowingRelationship.update(follower, followed, :follow_reject)
561 ActivityPub.reject(%{
562 to: [follower.ap_id],
568 {:user_locked, true} ->
569 {:ok, _relationship} = FollowingRelationship.update(follower, followed, :follow_pending)
581 %{"type" => "Accept", "object" => follow_object, "actor" => _actor, "id" => id} = data,
584 with actor <- Containment.get_actor(data),
585 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
586 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
587 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "accept"),
588 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
589 {:ok, _relationship} <- FollowingRelationship.update(follower, followed, :follow_accept) do
590 User.update_follower_count(followed)
591 User.update_following_count(follower)
593 ActivityPub.accept(%{
594 to: follow_activity.data["to"],
597 object: follow_activity.data["id"],
608 %{"type" => "Reject", "object" => follow_object, "actor" => _actor, "id" => id} = data,
611 with actor <- Containment.get_actor(data),
612 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
613 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
614 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "reject"),
615 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
616 {:ok, _relationship} <- FollowingRelationship.update(follower, followed, :follow_reject),
618 ActivityPub.reject(%{
619 to: follow_activity.data["to"],
622 object: follow_activity.data["id"],
632 @misskey_reactions %{
646 @doc "Rewrite misskey likes into EmojiReacts"
650 "_misskey_reaction" => reaction
655 |> Map.put("type", "EmojiReact")
656 |> Map.put("content", @misskey_reactions[reaction] || reaction)
657 |> handle_incoming(options)
660 def handle_incoming(%{"type" => type} = data, _options)
661 when type in ["Like", "EmojiReact", "Announce"] do
662 with :ok <- ObjectValidator.fetch_actor_and_object(data),
663 {:ok, activity, _meta} <-
664 Pipeline.common_pipeline(data, local: false) do
672 %{"type" => "Update", "object" => %{"type" => object_type} = object, "actor" => actor_id} =
676 when object_type in [
682 with %User{ap_id: ^actor_id} = actor <- User.get_cached_by_ap_id(object["id"]) do
683 {:ok, new_user_data} = ActivityPub.user_data_from_user_object(object)
686 |> User.remote_user_changeset(new_user_data)
687 |> User.update_and_set_cache()
689 ActivityPub.update(%{
691 to: data["to"] || [],
692 cc: data["cc"] || [],
695 activity_id: data["id"]
705 %{"type" => "Delete"} = data,
708 with {:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
711 {:error, {:validate_object, _}} = e ->
712 # Check if we have a create activity for this
713 with {:ok, object_id} <- Types.ObjectID.cast(data["object"]),
714 %Activity{data: %{"actor" => actor}} <-
715 Activity.create_by_object_ap_id(object_id) |> Repo.one(),
716 # We have one, insert a tombstone and retry
717 {:ok, tombstone_data, _} <- Builder.tombstone(actor, object_id),
718 {:ok, _tombstone} <- Object.create(tombstone_data) do
719 handle_incoming(data)
729 "object" => %{"type" => "Follow", "object" => followed},
735 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
736 {:ok, %User{} = follower} <- User.get_or_fetch_by_ap_id(follower),
737 {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
738 User.unfollow(follower, followed)
748 "object" => %{"type" => type}
752 when type in ["Like", "EmojiReact", "Announce", "Block"] do
753 with {:ok, activity, _} <- Pipeline.common_pipeline(data, local: false) do
758 # For Undos that don't have the complete object attached, try to find it in our database.
766 when is_binary(object) do
767 with %Activity{data: data} <- Activity.get_by_ap_id(object) do
769 |> Map.put("object", data)
770 |> handle_incoming(options)
777 %{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = _data,
780 with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked),
781 {:ok, %User{} = blocker} = User.get_or_fetch_by_ap_id(blocker),
782 {:ok, activity} <- ActivityPub.block(blocker, blocked, id, false) do
783 User.unfollow(blocker, blocked)
784 User.block(blocker, blocked)
794 "actor" => origin_actor,
795 "object" => origin_actor,
796 "target" => target_actor
800 with %User{} = origin_user <- User.get_cached_by_ap_id(origin_actor),
801 {:ok, %User{} = target_user} <- User.get_or_fetch_by_ap_id(target_actor),
802 true <- origin_actor in target_user.also_known_as do
803 ActivityPub.move(origin_user, target_user, false)
809 def handle_incoming(_, _), do: :error
811 @spec get_obj_helper(String.t(), Keyword.t()) :: {:ok, Object.t()} | nil
812 def get_obj_helper(id, options \\ []) do
813 case Object.normalize(id, true, options) do
814 %Object{} = object -> {:ok, object}
819 @spec get_embedded_obj_helper(String.t() | Object.t(), User.t()) :: {:ok, Object.t()} | nil
820 def get_embedded_obj_helper(%{"attributedTo" => attributed_to, "id" => object_id} = data, %User{
823 when attributed_to == ap_id do
824 with {:ok, activity} <-
829 "actor" => attributed_to,
832 {:ok, Object.normalize(activity)}
834 _ -> get_obj_helper(object_id)
838 def get_embedded_obj_helper(object_id, _) do
839 get_obj_helper(object_id)
842 def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
843 with false <- String.starts_with?(in_reply_to, "http"),
844 {:ok, %{data: replied_to_object}} <- get_obj_helper(in_reply_to) do
845 Map.put(object, "inReplyTo", replied_to_object["external_url"] || in_reply_to)
851 def set_reply_to_uri(obj), do: obj
854 Serialized Mastodon-compatible `replies` collection containing _self-replies_.
855 Based on Mastodon's ActivityPub::NoteSerializer#replies.
857 def set_replies(obj_data) do
859 with limit when limit > 0 <-
860 Pleroma.Config.get([:activitypub, :note_replies_output_limit], 0),
861 %Object{} = object <- Object.get_cached_by_ap_id(obj_data["id"]) do
863 |> Object.self_replies()
864 |> select([o], fragment("?->>'id'", o.data))
871 set_replies(obj_data, replies_uris)
874 defp set_replies(obj, []) do
878 defp set_replies(obj, replies_uris) do
879 replies_collection = %{
880 "type" => "Collection",
881 "items" => replies_uris
884 Map.merge(obj, %{"replies" => replies_collection})
887 def replies(%{"replies" => %{"first" => %{"items" => items}}}) when not is_nil(items) do
891 def replies(%{"replies" => %{"items" => items}}) when not is_nil(items) do
895 def replies(_), do: []
897 # Prepares the object of an outgoing create activity.
898 def prepare_object(object) do
905 |> prepare_attachments
909 |> strip_internal_fields
910 |> strip_internal_tags
916 # internal -> Mastodon
919 def prepare_outgoing(%{"type" => activity_type, "object" => object_id} = data)
920 when activity_type in ["Create", "Listen"] do
923 |> Object.normalize()
929 |> Map.put("object", object)
930 |> Map.merge(Utils.make_json_ld_header())
936 def prepare_outgoing(%{"type" => "Announce", "actor" => ap_id, "object" => object_id} = data) do
939 |> Object.normalize()
942 if Visibility.is_private?(object) && object.data["actor"] == ap_id do
943 data |> Map.put("object", object |> Map.get(:data) |> prepare_object)
945 data |> maybe_fix_object_url
950 |> strip_internal_fields
951 |> Map.merge(Utils.make_json_ld_header())
957 # Mastodon Accept/Reject requires a non-normalized object containing the actor URIs,
958 # because of course it does.
959 def prepare_outgoing(%{"type" => "Accept"} = data) do
960 with follow_activity <- Activity.normalize(data["object"]) do
962 "actor" => follow_activity.actor,
963 "object" => follow_activity.data["object"],
964 "id" => follow_activity.data["id"],
970 |> Map.put("object", object)
971 |> Map.merge(Utils.make_json_ld_header())
977 def prepare_outgoing(%{"type" => "Reject"} = data) do
978 with follow_activity <- Activity.normalize(data["object"]) do
980 "actor" => follow_activity.actor,
981 "object" => follow_activity.data["object"],
982 "id" => follow_activity.data["id"],
988 |> Map.put("object", object)
989 |> Map.merge(Utils.make_json_ld_header())
995 def prepare_outgoing(%{"type" => _type} = data) do
998 |> strip_internal_fields
999 |> maybe_fix_object_url
1000 |> Map.merge(Utils.make_json_ld_header())
1005 def maybe_fix_object_url(%{"object" => object} = data) when is_binary(object) do
1006 with false <- String.starts_with?(object, "http"),
1007 {:fetch, {:ok, relative_object}} <- {:fetch, get_obj_helper(object)},
1008 %{data: %{"external_url" => external_url}} when not is_nil(external_url) <-
1010 Map.put(data, "object", external_url)
1013 Logger.error("Couldn't fetch #{object} #{inspect(e)}")
1021 def maybe_fix_object_url(data), do: data
1023 def add_hashtags(object) do
1025 (object["tag"] || [])
1027 # Expand internal representation tags into AS2 tags.
1028 tag when is_binary(tag) ->
1030 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
1031 "name" => "##{tag}",
1035 # Do not process tags which are already AS2 tag objects.
1036 tag when is_map(tag) ->
1040 Map.put(object, "tag", tags)
1043 # TODO These should be added on our side on insertion, it doesn't make much
1044 # sense to regenerate these all the time
1045 def add_mention_tags(object) do
1046 to = object["to"] || []
1047 cc = object["cc"] || []
1048 mentioned = User.get_users_from_set(to ++ cc, local_only: false)
1050 mentions = Enum.map(mentioned, &build_mention_tag/1)
1052 tags = object["tag"] || []
1053 Map.put(object, "tag", tags ++ mentions)
1056 defp build_mention_tag(%{ap_id: ap_id, nickname: nickname} = _) do
1057 %{"type" => "Mention", "href" => ap_id, "name" => "@#{nickname}"}
1060 def take_emoji_tags(%User{emoji: emoji}) do
1063 |> Enum.map(&build_emoji_tag/1)
1066 # TODO: we should probably send mtime instead of unix epoch time for updated
1067 def add_emoji_tags(%{"emoji" => emoji} = object) do
1068 tags = object["tag"] || []
1070 out = Enum.map(emoji, &build_emoji_tag/1)
1072 Map.put(object, "tag", tags ++ out)
1075 def add_emoji_tags(object), do: object
1077 defp build_emoji_tag({name, url}) do
1079 "icon" => %{"url" => url, "type" => "Image"},
1080 "name" => ":" <> name <> ":",
1082 "updated" => "1970-01-01T00:00:00Z",
1087 def set_conversation(object) do
1088 Map.put(object, "conversation", object["context"])
1091 def set_sensitive(%{"sensitive" => true} = object) do
1095 def set_sensitive(object) do
1096 tags = object["tag"] || []
1097 Map.put(object, "sensitive", "nsfw" in tags)
1100 def set_type(%{"type" => "Answer"} = object) do
1101 Map.put(object, "type", "Note")
1104 def set_type(object), do: object
1106 def add_attributed_to(object) do
1107 attributed_to = object["attributedTo"] || object["actor"]
1108 Map.put(object, "attributedTo", attributed_to)
1111 def prepare_attachments(object) do
1114 |> Map.get("attachment", [])
1115 |> Enum.map(fn data ->
1116 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
1120 "mediaType" => media_type,
1121 "name" => data["name"],
1122 "type" => "Document"
1126 Map.put(object, "attachment", attachments)
1129 def strip_internal_fields(object) do
1130 Map.drop(object, Pleroma.Constants.object_internal_fields())
1133 defp strip_internal_tags(%{"tag" => tags} = object) do
1134 tags = Enum.filter(tags, fn x -> is_map(x) end)
1136 Map.put(object, "tag", tags)
1139 defp strip_internal_tags(object), do: object
1141 def perform(:user_upgrade, user) do
1142 # we pass a fake user so that the followers collection is stripped away
1143 old_follower_address = User.ap_followers(%User{nickname: user.nickname})
1147 where: ^old_follower_address in a.recipients,
1152 "array_replace(?,?,?)",
1154 ^old_follower_address,
1155 ^user.follower_address
1160 |> Repo.update_all([])
1163 def upgrade_user_from_ap_id(ap_id) do
1164 with %User{local: false} = user <- User.get_cached_by_ap_id(ap_id),
1165 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id),
1166 {:ok, user} <- update_user(user, data) do
1167 TransmogrifierWorker.enqueue("user_upgrade", %{"user_id" => user.id})
1170 %User{} = user -> {:ok, user}
1175 defp update_user(user, data) do
1177 |> User.remote_user_changeset(data)
1178 |> User.update_and_set_cache()
1181 def maybe_fix_user_url(%{"url" => url} = data) when is_map(url) do
1182 Map.put(data, "url", url["href"])
1185 def maybe_fix_user_url(data), do: data
1187 def maybe_fix_user_object(data), do: maybe_fix_user_url(data)