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
16 Modifies an incoming AP object (mastodon format) to our internal format.
18 def fix_object(object) do
20 |> Map.put("actor", object["attributedTo"])
28 def fix_in_reply_to(%{"inReplyTo" => in_reply_to_id} = object)
29 when not is_nil(in_reply_to_id) do
30 case ActivityPub.fetch_object_from_id(in_reply_to_id) do
31 {:ok, replied_object} ->
32 activity = Activity.get_create_activity_by_object_ap_id(replied_object.data["id"])
35 |> Map.put("inReplyTo", replied_object.data["id"])
36 |> Map.put("inReplyToAtomUri", object["inReplyToAtomUri"] || in_reply_to_id)
37 |> Map.put("inReplyToStatusId", activity.id)
38 |> Map.put("conversation", replied_object.data["context"] || object["conversation"])
39 |> Map.put("context", replied_object.data["context"] || object["conversation"])
42 Logger.error("Couldn't fetch #{object["inReplyTo"]} #{inspect(e)}")
47 def fix_in_reply_to(object), do: object
49 def fix_context(object) do
51 |> Map.put("context", object["conversation"])
54 def fix_attachments(object) do
56 (object["attachment"] || [])
57 |> Enum.map(fn data ->
58 url = [%{"type" => "Link", "mediaType" => data["mediaType"], "href" => data["url"]}]
59 Map.put(data, "url", url)
63 |> Map.put("attachment", attachments)
66 def fix_emoji(object) do
67 tags = object["tag"] || []
68 emoji = tags |> Enum.filter(fn data -> data["type"] == "Emoji" and data["icon"] end)
72 |> Enum.reduce(%{}, fn data, mapping ->
75 if String.starts_with?(name, ":") do
76 name = name |> String.slice(1..-2)
79 mapping |> Map.put(name, data["icon"]["url"])
82 # we merge mastodon and pleroma emoji into a single mapping, to allow for both wire formats
83 emoji = Map.merge(object["emoji"] || %{}, emoji)
86 |> Map.put("emoji", emoji)
89 def fix_tag(object) do
92 |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
93 |> Enum.map(fn data -> String.slice(data["name"], 1..-1) end)
95 combined = (object["tag"] || []) ++ tags
98 |> Map.put("tag", combined)
101 # TODO: validate those with a Ecto scheme
104 def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
105 with nil <- Activity.get_create_activity_by_object_ap_id(object["id"]),
106 %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
107 object = fix_object(data["object"])
113 context: object["conversation"],
115 published: data["published"],
123 ActivityPub.create(params)
125 %Activity{} = activity -> {:ok, activity}
131 %{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data
133 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
134 %User{} = follower <- User.get_or_fetch_by_ap_id(follower),
135 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
136 ActivityPub.accept(%{to: [follower.ap_id], actor: followed.ap_id, object: data, local: true})
138 User.follow(follower, followed)
146 %{"type" => "Like", "object" => object_id, "actor" => actor, "id" => id} = data
148 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
150 get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
151 {:ok, activity, object} <- ActivityPub.like(actor, object, id, false) do
159 %{"type" => "Announce", "object" => object_id, "actor" => actor, "id" => id} = data
161 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
163 get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
164 {:ok, activity, object} <- ActivityPub.announce(actor, object, id, false) do
172 %{"type" => "Update", "object" => %{"type" => "Person"} = object, "actor" => actor_id} =
175 with %User{ap_id: ^actor_id} = actor <- User.get_by_ap_id(object["id"]) do
176 {:ok, new_user_data} = ActivityPub.user_data_from_user_object(object)
178 banner = new_user_data[:info]["banner"]
182 |> Map.take([:name, :bio, :avatar])
183 |> Map.put(:info, Map.merge(actor.info, %{"banner" => banner}))
186 |> User.upgrade_changeset(update_data)
187 |> User.update_and_set_cache()
189 ActivityPub.update(%{
191 to: data["to"] || [],
192 cc: data["cc"] || [],
205 %{"type" => "Delete", "object" => object_id, "actor" => actor, "id" => id} = data
213 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
215 get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
216 {:ok, activity} <- ActivityPub.delete(object, false) do
227 def handle_incoming(_), do: :error
229 def get_obj_helper(id) do
230 if object = Object.get_by_ap_id(id), do: {:ok, object}, else: nil
233 def set_reply_to_uri(%{"inReplyTo" => inReplyTo} = object) do
234 with false <- String.starts_with?(inReplyTo, "http"),
235 {:ok, %{data: replied_to_object}} <- get_obj_helper(inReplyTo) do
236 Map.put(object, "inReplyTo", replied_to_object["external_url"] || inReplyTo)
242 def set_reply_to_uri(obj), do: obj
244 # Prepares the object of an outgoing create activity.
245 def prepare_object(object) do
252 |> prepare_attachments
262 def prepare_outgoing(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
269 |> Map.put("object", object)
270 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
275 def prepare_outgoing(%{"type" => type} = data) do
278 |> maybe_fix_object_url
279 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
284 def maybe_fix_object_url(data) do
285 if is_binary(data["object"]) and not String.starts_with?(data["object"], "http") do
286 case ActivityPub.fetch_object_from_id(data["object"]) do
287 {:ok, relative_object} ->
288 if relative_object.data["external_url"] do
291 |> Map.put("object", relative_object.data["external_url"])
297 Logger.error("Couldn't fetch #{data["object"]} #{inspect(e)}")
305 def add_hashtags(object) do
307 (object["tag"] || [])
308 |> Enum.map(fn tag ->
310 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
317 |> Map.put("tag", tags)
320 def add_mention_tags(object) do
321 recipients = object["to"] ++ (object["cc"] || [])
325 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
327 |> Enum.map(fn user ->
328 %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"}
331 tags = object["tag"] || []
334 |> Map.put("tag", tags ++ mentions)
337 # TODO: we should probably send mtime instead of unix epoch time for updated
338 def add_emoji_tags(object) do
339 tags = object["tag"] || []
340 emoji = object["emoji"] || []
344 |> Enum.map(fn {name, url} ->
346 "icon" => %{"url" => url, "type" => "Image"},
347 "name" => ":" <> name <> ":",
349 "updated" => "1970-01-01T00:00:00Z",
355 |> Map.put("tag", tags ++ out)
358 def set_conversation(object) do
359 Map.put(object, "conversation", object["context"])
362 def set_sensitive(object) do
363 tags = object["tag"] || []
364 Map.put(object, "sensitive", "nsfw" in tags)
367 def add_attributed_to(object) do
368 attributedTo = object["attributedTo"] || object["actor"]
371 |> Map.put("attributedTo", attributedTo)
374 def prepare_attachments(object) do
376 (object["attachment"] || [])
377 |> Enum.map(fn data ->
378 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
379 %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
383 |> Map.put("attachment", attachments)
386 defp user_upgrade_task(user) do
387 old_follower_address = User.ap_followers(user)
392 where: ^old_follower_address in u.following,
397 "array_replace(?,?,?)",
399 ^old_follower_address,
400 ^user.follower_address
406 Repo.update_all(q, [])
408 maybe_retire_websub(user.ap_id)
410 # Only do this for recent activties, don't go through the whole db.
411 # Only look at the last 1000 activities.
412 since = (Repo.aggregate(Activity, :max, :id) || 0) - 1_000
417 where: ^old_follower_address in a.recipients,
418 where: a.id > ^since,
423 "array_replace(?,?,?)",
425 ^old_follower_address,
426 ^user.follower_address
432 Repo.update_all(q, [])
435 def upgrade_user_from_ap_id(ap_id, async \\ true) do
436 with %User{local: false} = user <- User.get_by_ap_id(ap_id),
437 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id) do
440 |> Map.put(:info, Map.merge(user.info, data[:info]))
442 already_ap = User.ap_enabled?(user)
445 User.upgrade_changeset(user, data)
449 # This could potentially take a long time, do it in the background
452 user_upgrade_task(user)
455 user_upgrade_task(user)
465 def maybe_retire_websub(ap_id) do
467 if is_binary(ap_id) && String.length(ap_id) > 8 do
470 ws in Pleroma.Web.Websub.WebsubClientSubscription,
471 where: fragment("? like ?", ws.topic, ^"#{ap_id}%")