ChatMessageValidator: Allow one message in an array, too.
authorlain <lain@soykaf.club>
Mon, 18 May 2020 16:45:33 +0000 (18:45 +0200)
committerlain <lain@soykaf.club>
Mon, 18 May 2020 16:45:33 +0000 (18:45 +0200)
lib/pleroma/web/activity_pub/object_validators/chat_message_validator.ex
test/web/activity_pub/object_validator_test.exs

index 9c20c188ae78e784b4af8fa834e076dd041e9b28..138736f230f8c70a50ed353eb795b2771ac0771d 100644 (file)
@@ -47,9 +47,18 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator do
   def fix(data) do
     data
     |> fix_emoji()
+    |> fix_attachment()
     |> Map.put_new("actor", data["attributedTo"])
   end
 
+  # Throws everything but the first one away
+  def fix_attachment(%{"attachment" => [attachment | _]} = data) do
+    data
+    |> Map.put("attachment", attachment)
+  end
+
+  def fix_attachment(data), do: data
+
   def changeset(struct, data) do
     data = fix(data)
 
index da33d3dbc0b8b732c7a30e264cbc650ee91201a9..a79e50a29e812d83246ca9123ff9d50ec8101490 100644 (file)
@@ -13,6 +13,20 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
   import Pleroma.Factory
 
   describe "attachments" do
+    test "works with honkerific attachments" do
+      attachment = %{
+        "mediaType" => "image/jpeg",
+        "name" => "298p3RG7j27tfsZ9RQ.jpg",
+        "summary" => "298p3RG7j27tfsZ9RQ.jpg",
+        "type" => "Document",
+        "url" => "https://honk.tedunangst.com/d/298p3RG7j27tfsZ9RQ.jpg"
+      }
+
+      assert {:ok, attachment} =
+               AttachmentValidator.cast_and_validate(attachment)
+               |> Ecto.Changeset.apply_action(:insert)
+    end
+
     test "it turns mastodon attachments into our attachments" do
       attachment = %{
         "url" =>
@@ -103,6 +117,27 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidatorTest do
       assert object["attachment"]
     end
 
+    test "validates for a basic object with an attachment in an array", %{
+      valid_chat_message: valid_chat_message,
+      user: user
+    } do
+      file = %Plug.Upload{
+        content_type: "image/jpg",
+        path: Path.absname("test/fixtures/image.jpg"),
+        filename: "an_image.jpg"
+      }
+
+      {:ok, attachment} = ActivityPub.upload(file, actor: user.ap_id)
+
+      valid_chat_message =
+        valid_chat_message
+        |> Map.put("attachment", [attachment.data])
+
+      assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
+
+      assert object["attachment"]
+    end
+
     test "validates for a basic object with an attachment but without content", %{
       valid_chat_message: valid_chat_message,
       user: user