Merge branch 'bugfix/activitypub-object-actor' into 'develop'
[akkoma] / lib / pleroma / web / activity_pub / transmogrifier.ex
index 300e0fcdd350ecbec0a2b8b64a6b148258d7cdb2..3dd3df5534e4b883e9c1e9caa64ebcfa28a920c3 100644 (file)
@@ -18,12 +18,26 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
   """
   def fix_object(object) do
     object
-    |> Map.put("actor", object["attributedTo"])
+    |> fix_actor
     |> fix_attachments
     |> fix_context
     |> fix_in_reply_to
     |> fix_emoji
     |> fix_tag
+    |> fix_content_map
+  end
+
+  def fix_actor(%{"attributedTo" => actor} = object) do
+    # attributedTo can be a list in the case of peertube or plume
+    actor =
+      if is_list(actor) do
+        Enum.at(actor, 0)
+      else
+        actor
+      end
+
+    object
+    |> Map.put("actor", actor)
   end
 
   def fix_in_reply_to(%{"inReplyTo" => in_reply_to_id} = object)
@@ -107,13 +121,28 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
     |> Map.put("tag", combined)
   end
 
+  # content map usually only has one language so this will do for now.
+  def fix_content_map(%{"contentMap" => content_map} = object) do
+    content_groups = Map.to_list(content_map)
+    {_, content} = Enum.at(content_groups, 0)
+
+    object
+    |> Map.put("content", content)
+  end
+
+  def fix_content_map(object), do: object
+
   # TODO: validate those with a Ecto scheme
   # - tags
   # - emoji
-  def handle_incoming(%{"type" => "Create", "object" => %{"type" => "Note"} = object} = data) do
+  def handle_incoming(%{"type" => "Create", "object" => %{"type" => objtype} = object} = data)
+      when objtype in ["Article", "Note"] do
     with nil <- Activity.get_create_activity_by_object_ap_id(object["id"]),
          %User{} = user <- User.get_or_fetch_by_ap_id(data["actor"]) do
-      object = fix_object(data["object"])
+      # prefer the activity's actor instead of attributedTo
+      object =
+        fix_object(data["object"])
+        |> Map.put("actor", data["actor"])
 
       params = %{
         to: data["to"],