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 ->
76 if String.starts_with?(name, ":") do
77 name |> String.slice(1..-2)
82 mapping |> Map.put(name, data["icon"]["url"])
85 # we merge mastodon and pleroma emoji into a single mapping, to allow for both wire formats
86 emoji = Map.merge(object["emoji"] || %{}, emoji)
89 |> Map.put("emoji", emoji)
92 def fix_tag(object) do
95 |> Enum.filter(fn data -> data["type"] == "Hashtag" and data["name"] end)
96 |> Enum.map(fn data -> String.slice(data["name"], 1..-1) end)
98 combined = (object["tag"] || []) ++ tags
101 |> Map.put("tag", combined)
104 # TODO: validate those with a Ecto scheme
107 def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
108 with nil <- Activity.get_create_activity_by_object_ap_id(object["id"]),
109 %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
110 object = fix_object(data["object"])
116 context: object["conversation"],
118 published: data["published"],
126 ActivityPub.create(params)
128 %Activity{} = activity -> {:ok, activity}
134 %{"type" => "Follow", "object" => followed, "actor" => follower, "id" => id} = data
136 with %User{local: true} = followed <- User.get_cached_by_ap_id(followed),
137 %User{} = follower <- User.get_or_fetch_by_ap_id(follower),
138 {:ok, activity} <- ActivityPub.follow(follower, followed, id, false) do
139 ActivityPub.accept(%{to: [follower.ap_id], actor: followed.ap_id, object: data, local: true})
141 User.follow(follower, followed)
149 %{"type" => "Like", "object" => object_id, "actor" => actor, "id" => id} = _data
151 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
153 get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
154 {:ok, activity, _object} <- ActivityPub.like(actor, object, id, false) do
162 %{"type" => "Announce", "object" => object_id, "actor" => actor, "id" => id} = _data
164 with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
166 get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
167 {:ok, activity, _object} <- ActivityPub.announce(actor, object, id, false) do
175 %{"type" => "Update", "object" => %{"type" => "Person"} = object, "actor" => actor_id} =
178 with %User{ap_id: ^actor_id} = actor <- User.get_by_ap_id(object["id"]) do
179 {:ok, new_user_data} = ActivityPub.user_data_from_user_object(object)
181 banner = new_user_data[:info]["banner"]
185 |> Map.take([:name, :bio, :avatar])
186 |> Map.put(:info, Map.merge(actor.info, %{"banner" => banner}))
189 |> User.upgrade_changeset(update_data)
190 |> User.update_and_set_cache()
192 ActivityPub.update(%{
194 to: data["to"] || [],
195 cc: data["cc"] || [],
208 %{"type" => "Delete", "object" => object_id, "actor" => actor, "id" => _id} = _data
216 with %User{} = _actor <- User.get_or_fetch_by_ap_id(actor),
218 get_obj_helper(object_id) || ActivityPub.fetch_object_from_id(object_id),
219 {:ok, activity} <- ActivityPub.delete(object, false) do
230 def handle_incoming(_), do: :error
232 def get_obj_helper(id) do
233 if object = Object.get_by_ap_id(id), do: {:ok, object}, else: nil
236 def set_reply_to_uri(%{"inReplyTo" => inReplyTo} = object) do
237 with false <- String.starts_with?(inReplyTo, "http"),
238 {:ok, %{data: replied_to_object}} <- get_obj_helper(inReplyTo) do
239 Map.put(object, "inReplyTo", replied_to_object["external_url"] || inReplyTo)
245 def set_reply_to_uri(obj), do: obj
247 # Prepares the object of an outgoing create activity.
248 def prepare_object(object) do
255 |> prepare_attachments
262 # internal -> Mastodon
265 def prepare_outgoing(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
272 |> Map.put("object", object)
273 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
278 def prepare_outgoing(%{"type" => _type} = data) do
281 |> maybe_fix_object_url
282 |> Map.put("@context", "https://www.w3.org/ns/activitystreams")
287 def maybe_fix_object_url(data) do
288 if is_binary(data["object"]) and not String.starts_with?(data["object"], "http") do
289 case ActivityPub.fetch_object_from_id(data["object"]) do
290 {:ok, relative_object} ->
291 if relative_object.data["external_url"] do
294 |> Map.put("object", relative_object.data["external_url"])
300 Logger.error("Couldn't fetch #{data["object"]} #{inspect(e)}")
308 def add_hashtags(object) do
310 (object["tag"] || [])
311 |> Enum.map(fn tag ->
313 "href" => Pleroma.Web.Endpoint.url() <> "/tags/#{tag}",
320 |> Map.put("tag", tags)
323 def add_mention_tags(object) do
324 recipients = object["to"] ++ (object["cc"] || [])
328 |> Enum.map(fn ap_id -> User.get_cached_by_ap_id(ap_id) end)
330 |> Enum.map(fn user ->
331 %{"type" => "Mention", "href" => user.ap_id, "name" => "@#{user.nickname}"}
334 tags = object["tag"] || []
337 |> Map.put("tag", tags ++ mentions)
340 # TODO: we should probably send mtime instead of unix epoch time for updated
341 def add_emoji_tags(object) do
342 tags = object["tag"] || []
343 emoji = object["emoji"] || []
347 |> Enum.map(fn {name, url} ->
349 "icon" => %{"url" => url, "type" => "Image"},
350 "name" => ":" <> name <> ":",
352 "updated" => "1970-01-01T00:00:00Z",
358 |> Map.put("tag", tags ++ out)
361 def set_conversation(object) do
362 Map.put(object, "conversation", object["context"])
365 def set_sensitive(object) do
366 tags = object["tag"] || []
367 Map.put(object, "sensitive", "nsfw" in tags)
370 def add_attributed_to(object) do
371 attributedTo = object["attributedTo"] || object["actor"]
374 |> Map.put("attributedTo", attributedTo)
377 def prepare_attachments(object) do
379 (object["attachment"] || [])
380 |> Enum.map(fn data ->
381 [%{"mediaType" => media_type, "href" => href} | _] = data["url"]
382 %{"url" => href, "mediaType" => media_type, "name" => data["name"], "type" => "Document"}
386 |> Map.put("attachment", attachments)
389 defp user_upgrade_task(user) do
390 old_follower_address = User.ap_followers(user)
395 where: ^old_follower_address in u.following,
400 "array_replace(?,?,?)",
402 ^old_follower_address,
403 ^user.follower_address
409 Repo.update_all(q, [])
411 maybe_retire_websub(user.ap_id)
413 # Only do this for recent activties, don't go through the whole db.
414 # Only look at the last 1000 activities.
415 since = (Repo.aggregate(Activity, :max, :id) || 0) - 1_000
420 where: ^old_follower_address in a.recipients,
421 where: a.id > ^since,
426 "array_replace(?,?,?)",
428 ^old_follower_address,
429 ^user.follower_address
435 Repo.update_all(q, [])
438 def upgrade_user_from_ap_id(ap_id, async \\ true) do
439 with %User{local: false} = user <- User.get_by_ap_id(ap_id),
440 {:ok, data} <- ActivityPub.fetch_and_prepare_user_from_ap_id(ap_id) do
443 |> Map.put(:info, Map.merge(user.info, data[:info]))
445 already_ap = User.ap_enabled?(user)
448 User.upgrade_changeset(user, data)
452 # This could potentially take a long time, do it in the background
455 user_upgrade_task(user)
458 user_upgrade_task(user)
468 def maybe_retire_websub(ap_id) do
470 if is_binary(ap_id) && String.length(ap_id) > 8 do
473 ws in Pleroma.Web.Websub.WebsubClientSubscription,
474 where: fragment("? like ?", ws.topic, ^"#{ap_id}%")