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),
149 {:ok, object} <- get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
150 {:ok, activity, object} <- ActivityPub.like(actor, object, id, false) do
158 %{"type" => "Announce", "object" => object_id, "actor" => actor, "id" => id} = data
160 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
161 {:ok, object} <- get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
162 {:ok, activity, object} <- ActivityPub.announce(actor, object, id, false) do
170 %{"type" => "Update", "object" => %{"type" => "Person"} = object, "actor" => actor_id} =
173 with %User{ap_id: ^actor_id} = actor <- User.get_by_ap_id(object["id"]) do
174 {:ok, new_user_data} = ActivityPub.user_data_from_user_object(object)
176 banner = new_user_data[:info]["banner"]
180 |> Map.take([:name, :bio, :avatar])
181 |> Map.put(:info, Map.merge(actor.info, %{"banner" => banner}))
184 |> User.upgrade_changeset(update_data)
185 |> User.update_and_set_cache()
187 ActivityPub.update(%{
189 to: data["to"] || [],
190 cc: data["cc"] || [],
203 %{"type" => "Delete", "object" => object_id, "actor" => actor, "id" => id} = data
211 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
212 {:ok, object} <- get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
213 {:ok, activity} <- ActivityPub.delete(object, false) do
224 def handle_incoming(_), do: :error
226 def get_obj_helper(id) do
227 if object = Object.get_by_ap_id(id), do: {:ok, object}, else: nil
230 def set_reply_to_uri(%{"inReplyTo" => inReplyTo} = object) do
231 with false <- String.starts_with?(inReplyTo, "http"),
232 {:ok, %{data: replied_to_object}} <- get_obj_helper(inReplyTo) do
233 Map.put(object, "inReplyTo", replied_to_object["external_url"] || inReplyTo)
239 def set_reply_to_uri(obj), do: obj
241 # Prepares the object of an outgoing create activity.
242 def prepare_object(object) do
249 |> prepare_attachments
259 def prepare_outgoing(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
266 |> Map.put("object", object)
267 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
272 def prepare_outgoing(%{"type" => type} = data) do
275 |> maybe_fix_object_url
276 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
281 def maybe_fix_object_url(data) do
282 if is_binary(data["object"]) and not String.starts_with?(data["object"], "http") do
283 case ActivityPub.fetch_object_from_id(data["object"]) do
284 {:ok, relative_object} ->
285 if relative_object.data["external_url"] do
288 |> Map.put("object", relative_object.data["external_url"])
294 Logger.error("Couldn't fetch #{data["object"]} #{inspect(e)}")
302 def add_hashtags(object) do
304 (object["tag"] || [])
305 |> Enum.map(fn tag ->
307 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
314 |> Map.put("tag", tags)
317 def add_mention_tags(object) do
318 recipients = object["to"] ++ (object["cc"] || [])
322 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
324 |> Enum.map(fn user ->
325 %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"}
328 tags = object["tag"] || []
331 |> Map.put("tag", tags ++ mentions)
334 # TODO: we should probably send mtime instead of unix epoch time for updated
335 def add_emoji_tags(object) do
336 tags = object["tag"] || []
337 emoji = object["emoji"] || []
341 |> Enum.map(fn {name, url} ->
343 "icon" => %{"url" => url, "type" => "Image"},
344 "name" => ":" <> name <> ":",
346 "updated" => "1970-01-01T00:00:00Z",
352 |> Map.put("tag", tags ++ out)
355 def set_conversation(object) do
356 Map.put(object, "conversation", object["context"])
359 def set_sensitive(object) do
360 tags = object["tag"] || []
361 Map.put(object, "sensitive", "nsfw" in tags)
364 def add_attributed_to(object) do
365 attributedTo = object["attributedTo"] || object["actor"]
368 |> Map.put("attributedTo", attributedTo)
371 def prepare_attachments(object) do
373 (object["attachment"] || [])
374 |> Enum.map(fn data ->
375 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
376 %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
380 |> Map.put("attachment", attachments)
383 defp user_upgrade_task(user) do
384 old_follower_address = User.ap_followers(user)
389 where: ^old_follower_address in u.following,
394 "array_replace(?,?,?)",
396 ^old_follower_address,
397 ^user.follower_address
403 Repo.update_all(q, [])
405 maybe_retire_websub(user.ap_id)
407 # Only do this for recent activties, don't go through the whole db.
408 # Only look at the last 1000 activities.
409 since = (Repo.aggregate(Activity, :max, :id) || 0) - 1_000
414 where: ^old_follower_address in a.recipients,
415 where: a.id > ^since,
420 "array_replace(?,?,?)",
422 ^old_follower_address,
423 ^user.follower_address
429 Repo.update_all(q, [])
432 def upgrade_user_from_ap_id(ap_id, async \\ true) do
433 with %User{local: false} = user <- User.get_by_ap_id(ap_id),
434 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id) do
437 |> Map.put(:info, Map.merge(user.info, data[:info]))
439 already_ap = User.ap_enabled?(user)
442 User.upgrade_changeset(user, data)
446 # This could potentially take a long time, do it in the background
449 user_upgrade_task(user)
452 user_upgrade_task(user)
462 def maybe_retire_websub(ap_id) do
464 if is_binary(ap_id) && String.length(ap_id) > 8 do
467 ws in Pleroma.Web.Websub.WebsubClientSubscription,
468 where: fragment("? like ?", ws.topic, ^"#{ap_id}%")