1 defmodule Pleroma.Web.ActivityPub.Transmogrifier do
3 A module to handle coding from internal to wire ActivityPub and back.
9 alias Pleroma.Web.ActivityPub.ActivityPub
10 alias Pleroma.Web.ActivityPub.Utils
17 Modifies an incoming AP object (mastodon format) to our internal format.
19 def fix_object(object) do
21 |> Map.put("actor", object["attributedTo"])
29 def fix_in_reply_to(%{"inReplyTo" => in_reply_to_id} = object)
30 when not is_nil(in_reply_to_id) do
31 case ActivityPub.fetch_object_from_id(in_reply_to_id) do
32 {:ok, replied_object} ->
33 activity = Activity.get_create_activity_by_object_ap_id(replied_object.data["id"])
36 |> Map.put("inReplyTo", replied_object.data["id"])
37 |> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
38 |> Map.put("inReplyToStatusId", activity.id)
39 |> Map.put("conversation", replied_object.data["context"] || object["conversation"])
40 |> Map.put("context", replied_object.data["context"] || object["conversation"])
43 Logger.error("Couldn't fetch #{object["inReplyTo"]} #{inspect(e)}")
48 def fix_in_reply_to(object), do: object
50 def fix_context(object) do
52 |> Map.put("context", object["conversation"])
55 def fix_attachments(object) do
57 (object["attachment"] || [])
58 |> Enum.map(fn data ->
59 url = [%{"type" => "Link", "mediaType" => data["mediaType"], "href" => data["url"]}]
60 Map.put(data, "url", url)
64 |> Map.put("attachment", attachments)
67 def fix_emoji(object) do
68 tags = object["tag"] || []
69 emoji = tags |> Enum.filter(fn data -> data["type"] == "Emoji" and data["icon"] end)
73 |> Enum.reduce(%{}, fn data, mapping ->
77 if String.starts_with?(name, ":") do
78 name |> String.slice(1..-2)
83 mapping |> Map.put(name, data["icon"]["url"])
86 # we merge mastodon and pleroma emoji into a single mapping, to allow for both wire formats
87 emoji = Map.merge(object["emoji"] || %{}, emoji)
90 |> Map.put("emoji", emoji)
93 def fix_tag(object) do
96 |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
97 |> Enum.map(fn data -> String.slice(data["name"], 1..-1) end)
99 combined = (object["tag"] || []) ++ tags
102 |> Map.put("tag", combined)
105 # TODO: validate those with a Ecto scheme
108 def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
109 with nil <- Activity.get_create_activity_by_object_ap_id(object["id"]),
110 %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
111 object = fix_object(data["object"])
117 context: object["conversation"],
119 published: data["published"],
127 ActivityPub.create(params)
129 %Activity{} = activity -> {:ok, activity}
135 %{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data
137 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
138 %User{} = follower <- User.get_or_fetch_by_ap_id(follower),
139 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
140 ActivityPub.accept(%{to: [follower.ap_id], actor: followed.ap_id, object: data, local: true})
142 User.follow(follower, followed)
149 defp mastodon_follow_hack(%{"id" => id, "actor" => follower_id}, followed) do
150 with true <- id =~ "follows",
151 %User{local: true} = follower <- User.get_cached_by_ap_id(follower_id),
152 %Activity{} = activity <- Utils.fetch_latest_follow(follower, followed) do
159 defp mastodon_follow_hack(_), do: {:error, nil}
161 defp get_follow_activity(follow_object, followed) do
162 with object_id when not is_nil(object_id) <- Utils.get_ap_id(follow_object),
163 {_, %Activity{} = activity} <- {:activity, Activity.get_by_ap_id(object_id)} do
166 # Can't find the activity. This might a Mastodon 2.3 "Accept"
168 mastodon_follow_hack(follow_object, followed)
176 %{"type" => "Accept", "object" => follow_object, "actor" => actor, "id" => id} = data
178 with %User{} = followed <- User.get_or_fetch_by_ap_id(actor),
179 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
180 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
182 ActivityPub.accept(%{
183 to: follow_activity.data["to"],
185 actor: followed.ap_id,
186 object: follow_activity.data["id"],
189 if not User.following?(follower, followed) do
190 {:ok, follower} = User.follow(follower, followed)
200 %{"type" => "Reject", "object" => follow_object, "actor" => actor, "id" => id} = data
202 with %User{} = followed <- User.get_or_fetch_by_ap_id(actor),
203 {:ok, follow_activity} <- get_follow_activity(follow_object, followed),
204 %User{local: true} = follower <- User.get_cached_by_ap_id(follow_activity.data["actor"]),
206 ActivityPub.accept(%{
207 to: follow_activity.data["to"],
209 actor: followed.ap_id,
210 object: follow_activity.data["id"],
213 User.unfollow(follower, followed)
222 %{"type" => "Like", "object" => object_id, "actor" => actor, "id" => id} = _data
224 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
226 get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
227 {:ok, activity, _object} <- ActivityPub.like(actor, object, id, false) do
235 %{"type" => "Announce", "object" => object_id, "actor" => actor, "id" => id} = _data
237 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
239 get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
240 {:ok, activity, _object} <- ActivityPub.announce(actor, object, id, false) do
248 %{"type" => "Update", "object" => %{"type" => "Person"} = object, "actor" => actor_id} =
251 with %User{ap_id: ^actor_id} = actor <- User.get_by_ap_id(object["id"]) do
252 {:ok, new_user_data} = ActivityPub.user_data_from_user_object(object)
254 banner = new_user_data[:info]["banner"]
258 |> Map.take([:name, :bio, :avatar])
259 |> Map.put(:info, Map.merge(actor.info, %{"banner" => banner}))
262 |> User.upgrade_changeset(update_data)
263 |> User.update_and_set_cache()
265 ActivityPub.update(%{
267 to: data["to"] || [],
268 cc: data["cc"] || [],
281 %{"type" => "Delete", "object" => object_id, "actor" => actor, "id" => _id} = _data
283 object_id = Utils.get_ap_id(object_id)
285 with %User{} = _actor <- User.get_or_fetch_by_ap_id(actor),
287 get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
288 {:ok, activity} <- ActivityPub.delete(object, false) do
298 "object" => %{"type" => "Announce", "object" => object_id},
303 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
305 get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
306 {:ok, activity, _} <- ActivityPub.unannounce(actor, object, id, false) do
316 "object" => %{"type" => "Follow", "object" => followed},
321 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
322 %User{} = follower <- User.get_or_fetch_by_ap_id(follower),
323 {:ok, activity} <- ActivityPub.unfollow(follower, followed, id, false) do
324 User.unfollow(follower, followed)
331 @ap_config Application.get_env(:pleroma, :activitypub)
332 @accept_blocks Keyword.get(@ap_config, :accept_blocks)
337 "object" => %{"type" => "Block", "object" => blocked},
342 with true <- @accept_blocks,
343 %User{local: true} = blocked <- User.get_cached_by_ap_id(blocked),
344 %User{} = blocker <- User.get_or_fetch_by_ap_id(blocker),
345 {:ok, activity} <- ActivityPub.unblock(blocker, blocked, id, false) do
346 User.unblock(blocker, blocked)
354 %{"type" => "Block", "object" => blocked, "actor" => blocker, "id" => id} = data
356 with true <- @accept_blocks,
357 %User{local: true} = blocked = User.get_cached_by_ap_id(blocked),
358 %User{} = blocker = User.get_or_fetch_by_ap_id(blocker),
359 {:ok, activity} <- ActivityPub.block(blocker, blocked, id, false) do
360 User.unfollow(blocker, blocked)
361 User.block(blocker, blocked)
371 "object" => %{"type" => "Like", "object" => object_id},
376 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
378 get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
379 {:ok, activity, _, _} <- ActivityPub.unlike(actor, object, id, false) do
386 def handle_incoming(_), do: :error
388 def get_obj_helper(id) do
389 if object = Object.get_by_ap_id(id), do: {:ok, object}, else: nil
392 def set_reply_to_uri(%{"inReplyTo" => inReplyTo} = object) do
393 with false <- String.starts_with?(inReplyTo, "http"),
394 {:ok, %{data: replied_to_object}} <- get_obj_helper(inReplyTo) do
395 Map.put(object, "inReplyTo", replied_to_object["external_url"] || inReplyTo)
401 def set_reply_to_uri(obj), do: obj
403 # Prepares the object of an outgoing create activity.
404 def prepare_object(object) do
411 |> prepare_attachments
418 # internal -> Mastodon
421 def prepare_outgoing(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
428 |> Map.put("object", object)
429 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
434 def prepare_outgoing(%{"type" => _type} = data) do
437 |> maybe_fix_object_url
438 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
443 def maybe_fix_object_url(data) do
444 if is_binary(data["object"]) and not String.starts_with?(data["object"], "http") do
445 case ActivityPub.fetch_object_from_id(data["object"]) do
446 {:ok, relative_object} ->
447 if relative_object.data["external_url"] do
450 |> Map.put("object", relative_object.data["external_url"])
456 Logger.error("Couldn't fetch #{data["object"]} #{inspect(e)}")
464 def add_hashtags(object) do
466 (object["tag"] || [])
467 |> Enum.map(fn tag ->
469 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
476 |> Map.put("tag", tags)
479 def add_mention_tags(object) do
480 recipients = object["to"] ++ (object["cc"] || [])
484 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
486 |> Enum.map(fn user ->
487 %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"}
490 tags = object["tag"] || []
493 |> Map.put("tag", tags ++ mentions)
496 # TODO: we should probably send mtime instead of unix epoch time for updated
497 def add_emoji_tags(object) do
498 tags = object["tag"] || []
499 emoji = object["emoji"] || []
503 |> Enum.map(fn {name, url} ->
505 "icon" => %{"url" => url, "type" => "Image"},
506 "name" => ":" <> name <> ":",
508 "updated" => "1970-01-01T00:00:00Z",
514 |> Map.put("tag", tags ++ out)
517 def set_conversation(object) do
518 Map.put(object, "conversation", object["context"])
521 def set_sensitive(object) do
522 tags = object["tag"] || []
523 Map.put(object, "sensitive", "nsfw" in tags)
526 def add_attributed_to(object) do
527 attributedTo = object["attributedTo"] || object["actor"]
530 |> Map.put("attributedTo", attributedTo)
533 def prepare_attachments(object) do
535 (object["attachment"] || [])
536 |> Enum.map(fn data ->
537 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
538 %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
542 |> Map.put("attachment", attachments)
545 defp user_upgrade_task(user) do
546 old_follower_address = User.ap_followers(user)
551 where: ^old_follower_address in u.following,
556 "array_replace(?,?,?)",
558 ^old_follower_address,
559 ^user.follower_address
565 Repo.update_all(q, [])
567 maybe_retire_websub(user.ap_id)
569 # Only do this for recent activties, don't go through the whole db.
570 # Only look at the last 1000 activities.
571 since = (Repo.aggregate(Activity, :max, :id) || 0) - 1_000
576 where: ^old_follower_address in a.recipients,
577 where: a.id > ^since,
582 "array_replace(?,?,?)",
584 ^old_follower_address,
585 ^user.follower_address
591 Repo.update_all(q, [])
594 def upgrade_user_from_ap_id(ap_id, async \\ true) do
595 with %User{local: false} = user <- User.get_by_ap_id(ap_id),
596 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id) do
599 |> Map.put(:info, Map.merge(user.info, data[:info]))
601 already_ap = User.ap_enabled?(user)
604 User.upgrade_changeset(user, data)
608 # This could potentially take a long time, do it in the background
611 user_upgrade_task(user)
614 user_upgrade_task(user)
624 def maybe_retire_websub(ap_id) do
626 if is_binary(ap_id) && String.length(ap_id) > 8 do
629 ws in Pleroma.Web.Websub.WebsubClientSubscription,
630 where: fragment("? like ?", ws.topic, ^"#{ap_id}%")
637 def maybe_fix_user_url(data) do
638 if is_map(data["url"]) do
639 Map.put(data, "url", data["url"]["href"])
645 def maybe_fix_user_object(data) do
647 |> maybe_fix_user_url