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.
10 alias Pleroma.FollowingRelationship
12 alias Pleroma.Object.Containment
15 alias Pleroma.Web.ActivityPub.ActivityPub
16 alias Pleroma.Web.ActivityPub.Utils
17 alias Pleroma.Web.ActivityPub.Visibility
18 alias Pleroma.Web.Federator
19 alias Pleroma.Workers.TransmogrifierWorker
24 require Pleroma.Constants
27 Modifies an incoming AP object (mastodon format) to our internal format.
29 def fix_object(object, options \\ []) do
31 |> strip_internal_fields
36 |> fix_in_reply_to(options)
45 def fix_summary(%{"summary" => nil} = object) do
46 Map.put(object, "summary", "")
49 def fix_summary(%{"summary" => _} = object) do
50 # summary is present, nothing to do
54 def fix_summary(object), do: Map.put(object, "summary", "")
56 def fix_addressing_list(map, field) do
58 is_binary(map[field]) ->
59 Map.put(map, field, [map[field]])
62 Map.put(map, field, [])
69 def fix_explicit_addressing(
70 %{"to" => to, "cc" => cc} = object,
74 explicit_to = Enum.filter(to, fn x -> x in explicit_mentions end)
76 explicit_cc = Enum.filter(to, fn x -> x not in explicit_mentions end)
80 |> Enum.reject(fn x -> String.ends_with?(x, "/followers") and x != follower_collection end)
84 |> Map.put("to", explicit_to)
85 |> Map.put("cc", final_cc)
88 def fix_explicit_addressing(object, _explicit_mentions, _followers_collection), do: object
90 # if directMessage flag is set to true, leave the addressing alone
91 def fix_explicit_addressing(%{"directMessage" => true} = object), do: object
93 def fix_explicit_addressing(object) do
94 explicit_mentions = Utils.determine_explicit_mentions(object)
96 %User{follower_address: follower_collection} =
98 |> Containment.get_actor()
99 |> User.get_cached_by_ap_id()
104 Pleroma.Constants.as_public(),
108 fix_explicit_addressing(object, explicit_mentions, follower_collection)
111 # if as:Public is addressed, then make sure the followers collection is also addressed
112 # so that the activities will be delivered to local users.
113 def fix_implicit_addressing(%{"to" => to, "cc" => cc} = object, followers_collection) do
114 recipients = to ++ cc
116 if followers_collection not in recipients do
118 Pleroma.Constants.as_public() in cc ->
119 to = to ++ [followers_collection]
120 Map.put(object, "to", to)
122 Pleroma.Constants.as_public() in to ->
123 cc = cc ++ [followers_collection]
124 Map.put(object, "cc", cc)
134 def fix_implicit_addressing(object, _), do: object
136 def fix_addressing(object) do
137 {:ok, %User{} = user} = User.get_or_fetch_by_ap_id(object["actor"])
138 followers_collection = User.ap_followers(user)
141 |> fix_addressing_list("to")
142 |> fix_addressing_list("cc")
143 |> fix_addressing_list("bto")
144 |> fix_addressing_list("bcc")
145 |> fix_explicit_addressing()
146 |> fix_implicit_addressing(followers_collection)
149 def fix_actor(%{"attributedTo" => actor} = object) do
150 Map.put(object, "actor", Containment.get_actor(%{"actor" => actor}))
153 def fix_in_reply_to(object, options \\ [])
155 def fix_in_reply_to(%{"inReplyTo" => in_reply_to} = object, options)
156 when not is_nil(in_reply_to) do
157 in_reply_to_id = prepare_in_reply_to(in_reply_to)
158 object = Map.put(object, "inReplyToAtomUri", in_reply_to_id)
160 if Federator.allowed_incoming_reply_depth?(options[:depth]) do
161 with {:ok, replied_object} <- get_obj_helper(in_reply_to_id, options),
162 %Activity{} = _ <- Activity.get_create_by_object_ap_id(replied_object.data["id"]) do
164 |> Map.put("inReplyTo", replied_object.data["id"])
165 |> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
166 |> Map.put("conversation", replied_object.data["context"] || object["conversation"])
167 |> Map.put("context", replied_object.data["context"] || object["conversation"])
170 Logger.error("Couldn't fetch #{inspect(in_reply_to_id)}, error: #{inspect(e)}")
178 def fix_in_reply_to(object, _options), do: object
180 defp prepare_in_reply_to(in_reply_to) do
182 is_bitstring(in_reply_to) ->
185 is_map(in_reply_to) && is_bitstring(in_reply_to["id"]) ->
188 is_list(in_reply_to) && is_bitstring(Enum.at(in_reply_to, 0)) ->
189 Enum.at(in_reply_to, 0)
196 def fix_context(object) do
197 context = object["context"] || object["conversation"] || Utils.generate_context_id()
200 |> Map.put("context", context)
201 |> Map.put("conversation", context)
204 def fix_attachments(%{"attachment" => attachment} = object) when is_list(attachment) do
206 Enum.map(attachment, fn data ->
207 media_type = data["mediaType"] || data["mimeType"]
208 href = data["url"] || data["href"]
209 url = [%{"type" => "Link", "mediaType" => media_type, "href" => href}]
212 |> Map.put("mediaType", media_type)
213 |> Map.put("url", url)
216 Map.put(object, "attachment", attachments)
219 def fix_attachments(%{"attachment" => attachment} = object) when is_map(attachment) do
221 |> Map.put("attachment", [attachment])
225 def fix_attachments(object), do: object
227 def fix_url(%{"url" => url} = object) when is_map(url) do
228 Map.put(object, "url", url["href"])
231 def fix_url(%{"type" => "Video", "url" => url} = object) when is_list(url) do
232 first_element = Enum.at(url, 0)
234 link_element = Enum.find(url, fn x -> is_map(x) and x["mimeType"] == "text/html" end)
237 |> Map.put("attachment", [first_element])
238 |> Map.put("url", link_element["href"])
241 def fix_url(%{"type" => object_type, "url" => url} = object)
242 when object_type != "Video" and is_list(url) do
243 first_element = Enum.at(url, 0)
247 is_bitstring(first_element) -> first_element
248 is_map(first_element) -> first_element["href"] || ""
252 Map.put(object, "url", url_string)
255 def fix_url(object), do: object
257 def fix_emoji(%{"tag" => tags} = object) when is_list(tags) do
260 |> Enum.filter(fn data -> data["type"] == "Emoji" and data["icon"] end)
261 |> Enum.reduce(%{}, fn data, mapping ->
262 name = String.trim(data["name"], ":")
264 Map.put(mapping, name, data["icon"]["url"])
267 # we merge mastodon and pleroma emoji into a single mapping, to allow for both wire formats
268 emoji = Map.merge(object["emoji"] || %{}, emoji)
270 Map.put(object, "emoji", emoji)
273 def fix_emoji(%{"tag" => %{"type" => "Emoji"} = tag} = object) do
274 name = String.trim(tag["name"], ":")
275 emoji = %{name => tag["icon"]["url"]}
277 Map.put(object, "emoji", emoji)
280 def fix_emoji(object), do: object
282 def fix_tag(%{"tag" => tag} = object) when is_list(tag) do
285 |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
286 |> Enum.map(fn data -> String.slice(data["name"], 1..-1) end)
288 Map.put(object, "tag", tag ++ tags)
291 def fix_tag(%{"tag" => %{"type" => "Hashtag", "name" => hashtag} = tag} = object) do
292 combined = [tag, String.slice(hashtag, 1..-1)]
294 Map.put(object, "tag", combined)
297 def fix_tag(%{"tag" => %{} = tag} = object), do: Map.put(object, "tag", [tag])
299 def fix_tag(object), do: object
301 # content map usually only has one language so this will do for now.
302 def fix_content_map(%{"contentMap" => content_map} = object) do
303 content_groups = Map.to_list(content_map)
304 {_, content} = Enum.at(content_groups, 0)
306 Map.put(object, "content", content)
309 def fix_content_map(object), do: object
311 def fix_type(object, options \\ [])
313 def fix_type(%{"inReplyTo" => reply_id, "name" => _} = object, options)
314 when is_binary(reply_id) do
315 with true <- Federator.allowed_incoming_reply_depth?(options[:depth]),
316 {:ok, %{data: %{"type" => "Question"} = _} = _} <- get_obj_helper(reply_id, options) do
317 Map.put(object, "type", "Answer")
323 def fix_type(object, _), do: object
325 defp mastodon_follow_hack(%{"id" => id, "actor" => follower_id}, followed) do
326 with true <- id =~ "follows",
327 %User{local: true} = follower <- User.get_cached_by_ap_id(follower_id),
328 %Activity{} = activity <- Utils.fetch_latest_follow(follower, followed) do
335 defp mastodon_follow_hack(_, _), do: {:error, nil}
337 defp get_follow_activity(follow_object, followed) do
338 with object_id when not is_nil(object_id) <- Utils.get_ap_id(follow_object),
339 {_, %Activity{} = activity} <- {:activity, Activity.get_by_ap_id(object_id)} do
342 # Can't find the activity. This might a Mastodon 2.3 "Accept"
344 mastodon_follow_hack(follow_object, followed)
351 # Reduce the object list to find the reported user.
352 defp get_reported(objects) do
353 Enum.reduce_while(objects, nil, fn ap_id, _ ->
354 with %User{} = user <- User.get_cached_by_ap_id(ap_id) do
362 def handle_incoming(data, options \\ [])
364 # Flag objects are placed ahead of the ID check because Mastodon 2.8 and earlier send them
366 def handle_incoming(%{"type" => "Flag", "object" => objects, "actor" => actor} = data, _options) do
367 with context <- data["context"] || Utils.generate_context_id(),
368 content <- data["content"] || "",
369 %User{} = actor <- User.get_cached_by_ap_id(actor),
370 # Reduce the object list to find the reported user.
371 %User{} = account <- get_reported(objects),
372 # Remove the reported user from the object list.
373 statuses <- Enum.filter(objects, fn ap_id -> ap_id != account.ap_id end) do
380 additional: %{"cc" => [account.ap_id]}
382 |> ActivityPub.flag()
386 # disallow objects with bogus IDs
387 def handle_incoming(%{"id" => nil}, _options), do: :error
388 def handle_incoming(%{"id" => ""}, _options), do: :error
389 # length of https:// = 8, should validate better, but good enough for now.
390 def handle_incoming(%{"id" => id}, _options) when not (is_binary(id) and length(id) > 8),
393 # TODO: validate those with a Ecto scheme
397 %{"type" => "Create", "object" => %{"type" => objtype} = object} = data,
400 when objtype in ["Article", "Note", "Video", "Page", "Question", "Answer"] do
401 actor = Containment.get_actor(data)
404 Map.put(data, "actor", actor)
407 with nil <- Activity.get_create_by_object_ap_id(object["id"]),
408 {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
409 options = Keyword.put(options, :depth, (options[:depth] || 0) + 1)
410 object = fix_object(data["object"], options)
416 context: object["conversation"],
418 published: data["published"],
427 ActivityPub.create(params)
429 %Activity{} = activity -> {:ok, activity}
435 %{"type" => "Listen", "object" => %{"type" => "Audio"} = object} = data,
438 actor = Containment.get_actor(data)
441 Map.put(data, "actor", actor)
444 with {:ok, %User{} = user} <- User.get_or_fetch_by_ap_id(data["actor"]) do
445 options = Keyword.put(options, :depth, (options[:depth] || 0) + 1)
446 object = fix_object(object, options)
454 published: data["published"],
455 additional: Map.take(data, ["cc", "id"])
458 ActivityPub.listen(params)
465 %{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data,
468 with %User{local: true} = followed <-
469 User.get_cached_by_ap_id(Containment.get_actor(%{"actor" => followed})),
470 {:ok, %User{} = follower} <-
471 User.get_or_fetch_by_ap_id(Containment.get_actor(%{"actor" => follower})),
472 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
473 with deny_follow_blocked <- Pleroma.Config.get([:user, :deny_follow_blocked]),
474 {_, false} <- {:user_blocked, User.blocks?(followed, follower) && deny_follow_blocked},
475 {_, false} <- {:user_locked, User.locked?(followed)},
476 {_, {:ok, follower}} <- {:follow, User.follow(follower, followed)},
478 {:follow_state_update, Utils.update_follow_state_for_all(activity, "accept")},
479 {:ok, _relationship} <- FollowingRelationship.update(follower, followed, "accept") do
480 ActivityPub.accept(%{
481 to: [follower.ap_id],
487 {:user_blocked, true} ->
488 {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
489 {:ok, _relationship} = FollowingRelationship.update(follower, followed, "reject")
491 ActivityPub.reject(%{
492 to: [follower.ap_id],
498 {:follow, {:error, _}} ->
499 {:ok, _} = Utils.update_follow_state_for_all(activity, "reject")
500 {:ok, _relationship} = FollowingRelationship.update(follower, followed, "reject")
502 ActivityPub.reject(%{
503 to: [follower.ap_id],
509 {:user_locked, true} ->
510 {:ok, _relationship} = FollowingRelationship.update(follower, followed, "pending")
522 %{"type" => "Accept", "object" => follow_object, "actor" => _actor, "id" => id} = data,
525 with actor <- Containment.get_actor(data),
526 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
527 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
528 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "accept"),
529 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
530 {:ok, _relationship} <- FollowingRelationship.update(follower, followed, "accept") do
531 ActivityPub.accept(%{
532 to: follow_activity.data["to"],
535 object: follow_activity.data["id"],
545 %{"type" => "Reject", "object" => follow_object, "actor" => _actor, "id" => id} = data,
548 with actor <- Containment.get_actor(data),
549 {:ok, %User{} = followed} <- User.get_or_fetch_by_ap_id(actor),
550 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
551 {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "reject"),
552 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
553 {:ok, _relationship} <- FollowingRelationship.update(follower, followed, "reject"),
555 ActivityPub.reject(%{
556 to: follow_activity.data["to"],
559 object: follow_activity.data["id"],
569 @misskey_reactions %{
583 @doc "Rewrite misskey likes into EmojiReactions"
587 "_misskey_reaction" => reaction
592 |> Map.put("type", "EmojiReaction")
593 |> Map.put("content", @misskey_reactions[reaction] || reaction)
594 |> handle_incoming(options)
598 %{"type" => "Like", "object" => object_id, "actor" => _actor, "id" => id} = data,
601 with actor <- Containment.get_actor(data),
602 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
603 {:ok, object} <- get_obj_helper(object_id),
604 {:ok, activity, _object} <- ActivityPub.like(actor, object, id, false) do
613 "type" => "EmojiReaction",
614 "object" => object_id,
621 with actor <- Containment.get_actor(data),
622 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
623 {:ok, object} <- get_obj_helper(object_id),
624 {:ok, activity, _object} <-
625 ActivityPub.react_with_emoji(actor, object, emoji, activity_id: id, local: false) do
633 %{"type" => "Announce", "object" => object_id, "actor" => _actor, "id" => id} = data,
636 with actor <- Containment.get_actor(data),
637 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
638 {:ok, object} <- get_embedded_obj_helper(object_id, actor),
639 public <- Visibility.is_public?(data),
640 {:ok, activity, _object} <- ActivityPub.announce(actor, object, id, false, public) do
648 %{"type" => "Update", "object" => %{"type" => object_type} = object, "actor" => actor_id} =
652 when object_type in [
658 with %User{ap_id: ^actor_id} = actor <- User.get_cached_by_ap_id(object["id"]) do
659 {:ok, new_user_data} = ActivityPub.user_data_from_user_object(object)
661 locked = new_user_data[:locked] || false
662 attachment = get_in(new_user_data, [:source_data, "attachment"]) || []
663 invisible = new_user_data[:invisible] || false
667 |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end)
668 |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end)
672 |> Map.take([:avatar, :banner, :bio, :name])
673 |> Map.put(:fields, fields)
674 |> Map.put(:locked, locked)
675 |> Map.put(:invisible, invisible)
678 |> User.upgrade_changeset(update_data, true)
679 |> User.update_and_set_cache()
681 ActivityPub.update(%{
683 to: data["to"] || [],
684 cc: data["cc"] || [],
687 activity_id: data["id"]
696 # TODO: We presently assume that any actor on the same origin domain as the object being
697 # deleted has the rights to delete that object. A better way to validate whether or not
698 # the object should be deleted is to refetch the object URI, which should return either
699 # an error or a tombstone. This would allow us to verify that a deletion actually took
702 %{"type" => "Delete", "object" => object_id, "actor" => actor, "id" => id} = data,
705 object_id = Utils.get_ap_id(object_id)
707 with actor <- Containment.get_actor(data),
708 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
709 {:ok, object} <- get_obj_helper(object_id),
710 :ok <- Containment.contain_origin(actor.ap_id, object.data),
712 ActivityPub.delete(object, local: false, activity_id: id, actor: actor.ap_id) do
716 case User.get_cached_by_ap_id(object_id) do
717 %User{ap_id: ^actor} = user ->
732 "object" => %{"type" => "Announce", "object" => object_id},
738 with actor <- Containment.get_actor(data),
739 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
740 {:ok, object} <- get_obj_helper(object_id),
741 {:ok, activity, _} <- ActivityPub.unannounce(actor, object, id, false) do
751 "object" => %{"type" => "Follow", "object" => followed},
757 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
758 {:ok, %User{} = follower} <- User.get_or_fetch_by_ap_id(follower),
759 {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
760 User.unfollow(follower, followed)
770 "object" => %{"type" => "EmojiReaction", "id" => reaction_activity_id},
776 with actor <- Containment.get_actor(data),
777 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
778 {:ok, activity, _} <-
779 ActivityPub.unreact_with_emoji(actor, reaction_activity_id,
792 "object" => %{"type" => "Block", "object" => blocked},
798 with %User{local: true} = blocked <- User.get_cached_by_ap_id(blocked),
799 {:ok, %User{} = blocker} <- User.get_or_fetch_by_ap_id(blocker),
800 {:ok, activity} <- ActivityPub.unblock(blocker, blocked, id, false) do
801 User.unblock(blocker, blocked)
809 %{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = _data,
812 with %User{local: true} = blocked = User.get_cached_by_ap_id(blocked),
813 {:ok, %User{} = blocker} = User.get_or_fetch_by_ap_id(blocker),
814 {:ok, activity} <- ActivityPub.block(blocker, blocked, id, false) do
815 User.unfollow(blocker, blocked)
816 User.block(blocker, blocked)
826 "object" => %{"type" => "Like", "object" => object_id},
832 with actor <- Containment.get_actor(data),
833 {:ok, %User{} = actor} <- User.get_or_fetch_by_ap_id(actor),
834 {:ok, object} <- get_obj_helper(object_id),
835 {:ok, activity, _, _} <- ActivityPub.unlike(actor, object, id, false) do
842 # For Undos that don't have the complete object attached, try to find it in our database.
850 when is_binary(object) do
851 with %Activity{data: data} <- Activity.get_by_ap_id(object) do
853 |> Map.put("object", data)
854 |> handle_incoming(options)
860 def handle_incoming(_, _), do: :error
862 @spec get_obj_helper(String.t(), Keyword.t()) :: {:ok, Object.t()} | nil
863 def get_obj_helper(id, options \\ []) do
864 case Object.normalize(id, true, options) do
865 %Object{} = object -> {:ok, object}
870 @spec get_embedded_obj_helper(String.t() | Object.t(), User.t()) :: {:ok, Object.t()} | nil
871 def get_embedded_obj_helper(%{"attributedTo" => attributed_to, "id" => object_id} = data, %User{
874 when attributed_to == ap_id do
875 with {:ok, activity} <-
880 "actor" => attributed_to,
883 {:ok, Object.normalize(activity)}
885 _ -> get_obj_helper(object_id)
889 def get_embedded_obj_helper(object_id, _) do
890 get_obj_helper(object_id)
893 def set_reply_to_uri(%{"inReplyTo" => in_reply_to} = object) when is_binary(in_reply_to) do
894 with false <- String.starts_with?(in_reply_to, "http"),
895 {:ok, %{data: replied_to_object}} <- get_obj_helper(in_reply_to) do
896 Map.put(object, "inReplyTo", replied_to_object["external_url"] || in_reply_to)
902 def set_reply_to_uri(obj), do: obj
904 # Prepares the object of an outgoing create activity.
905 def prepare_object(object) do
912 |> prepare_attachments
915 |> strip_internal_fields
916 |> strip_internal_tags
922 # internal -> Mastodon
925 def prepare_outgoing(%{"type" => activity_type, "object" => object_id} = data)
926 when activity_type in ["Create", "Listen"] do
929 |> Object.normalize()
935 |> Map.put("object", object)
936 |> Map.merge(Utils.make_json_ld_header())
942 def prepare_outgoing(%{"type" => "Announce", "actor" => ap_id, "object" => object_id} = data) do
945 |> Object.normalize()
948 if Visibility.is_private?(object) && object.data["actor"] == ap_id do
949 data |> Map.put("object", object |> Map.get(:data) |> prepare_object)
951 data |> maybe_fix_object_url
956 |> strip_internal_fields
957 |> Map.merge(Utils.make_json_ld_header())
963 # Mastodon Accept/Reject requires a non-normalized object containing the actor URIs,
964 # because of course it does.
965 def prepare_outgoing(%{"type" => "Accept"} = data) do
966 with follow_activity <- Activity.normalize(data["object"]) do
968 "actor" => follow_activity.actor,
969 "object" => follow_activity.data["object"],
970 "id" => follow_activity.data["id"],
976 |> Map.put("object", object)
977 |> Map.merge(Utils.make_json_ld_header())
983 def prepare_outgoing(%{"type" => "Reject"} = data) do
984 with follow_activity <- Activity.normalize(data["object"]) do
986 "actor" => follow_activity.actor,
987 "object" => follow_activity.data["object"],
988 "id" => follow_activity.data["id"],
994 |> Map.put("object", object)
995 |> Map.merge(Utils.make_json_ld_header())
1001 def prepare_outgoing(%{"type" => _type} = data) do
1004 |> strip_internal_fields
1005 |> maybe_fix_object_url
1006 |> Map.merge(Utils.make_json_ld_header())
1011 def maybe_fix_object_url(%{"object" => object} = data) when is_binary(object) do
1012 with false <- String.starts_with?(object, "http"),
1013 {:fetch, {:ok, relative_object}} <- {:fetch, get_obj_helper(object)},
1014 %{data: %{"external_url" => external_url}} when not is_nil(external_url) <-
1016 Map.put(data, "object", external_url)
1019 Logger.error("Couldn't fetch #{object} #{inspect(e)}")
1027 def maybe_fix_object_url(data), do: data
1029 def add_hashtags(object) do
1031 (object["tag"] || [])
1033 # Expand internal representation tags into AS2 tags.
1034 tag when is_binary(tag) ->
1036 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
1037 "name" => "##{tag}",
1041 # Do not process tags which are already AS2 tag objects.
1042 tag when is_map(tag) ->
1046 Map.put(object, "tag", tags)
1049 def add_mention_tags(object) do
1052 |> Utils.get_notified_from_object()
1053 |> Enum.map(&build_mention_tag/1)
1055 tags = object["tag"] || []
1057 Map.put(object, "tag", tags ++ mentions)
1060 defp build_mention_tag(%{ap_id: ap_id, nickname: nickname} = _) do
1061 %{"type" => "Mention", "href" => ap_id, "name" => "@#{nickname}"}
1064 def take_emoji_tags(%User{emoji: emoji}) do
1066 |> Enum.flat_map(&Map.to_list/1)
1067 |> Enum.map(&build_emoji_tag/1)
1070 # TODO: we should probably send mtime instead of unix epoch time for updated
1071 def add_emoji_tags(%{"emoji" => emoji} = object) do
1072 tags = object["tag"] || []
1074 out = Enum.map(emoji, &build_emoji_tag/1)
1076 Map.put(object, "tag", tags ++ out)
1079 def add_emoji_tags(object), do: object
1081 defp build_emoji_tag({name, url}) do
1083 "icon" => %{"url" => url, "type" => "Image"},
1084 "name" => ":" <> name <> ":",
1086 "updated" => "1970-01-01T00:00:00Z",
1091 def set_conversation(object) do
1092 Map.put(object, "conversation", object["context"])
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
1113 (object["attachment"] || [])
1114 |> Enum.map(fn data ->
1115 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
1116 %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
1119 Map.put(object, "attachment", attachments)
1122 def strip_internal_fields(object) do
1124 |> Map.drop(Pleroma.Constants.object_internal_fields())
1127 defp strip_internal_tags(%{"tag" => tags} = object) do
1128 tags = Enum.filter(tags, fn x -> is_map(x) end)
1130 Map.put(object, "tag", tags)
1133 defp strip_internal_tags(object), do: object
1135 def perform(:user_upgrade, user) do
1136 # we pass a fake user so that the followers collection is stripped away
1137 old_follower_address = User.ap_followers(%User{nickname: user.nickname})
1141 where: ^old_follower_address in a.recipients,
1146 "array_replace(?,?,?)",
1148 ^old_follower_address,
1149 ^user.follower_address
1154 |> Repo.update_all([])
1157 def upgrade_user_from_ap_id(ap_id) do
1158 with %User{local: false} = user <- User.get_cached_by_ap_id(ap_id),
1159 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id),
1160 already_ap <- User.ap_enabled?(user),
1161 {:ok, user} <- upgrade_user(user, data) do
1162 if not already_ap do
1163 TransmogrifierWorker.enqueue("user_upgrade", %{"user_id" => user.id})
1168 %User{} = user -> {:ok, user}
1173 defp upgrade_user(user, data) do
1175 |> User.upgrade_changeset(data, true)
1176 |> User.update_and_set_cache()
1179 def maybe_fix_user_url(%{"url" => url} = data) when is_map(url) do
1180 Map.put(data, "url", url["href"])
1183 def maybe_fix_user_url(data), do: data
1185 def maybe_fix_user_object(data), do: maybe_fix_user_url(data)