commit: 39dc6c65ef7a95412a985a3edce019914af12df8
parent 47e00524f7c443d5313c1b05f05d61f50c3a1f7c
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Fri, 29 Dec 2023 05:23:01 +0100
ChatMessage: Tolerate attachment field set to an empty array
Closes: https://git.pleroma.social/pleroma/pleroma/-/issues/3224
Diffstat:
3 files changed, 21 insertions(+), 0 deletions(-)
diff --git a/changelog.d/chat-attachment-empty-array.fix b/changelog.d/chat-attachment-empty-array.fix
@@ -0,0 +1 @@
+ChatMessage: Tolerate attachment field set to an empty array
diff --git a/lib/pleroma/web/activity_pub/object_validators/chat_message_validator.ex b/lib/pleroma/web/activity_pub/object_validators/chat_message_validator.ex
@@ -57,6 +57,11 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ChatMessageValidator do
|> Map.put("attachment", attachment)
end
+ def fix_attachment(%{"attachment" => attachment} = data) when attachment == [] do
+ data
+ |> Map.drop(["attachment"])
+ end
+
def fix_attachment(data), do: data
def changeset(struct, data) do
diff --git a/test/pleroma/web/activity_pub/object_validators/chat_validation_test.exs b/test/pleroma/web/activity_pub/object_validators/chat_validation_test.exs
@@ -147,6 +147,21 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ChatValidationTest do
assert object["attachment"]
end
+ test "validates for a basic object with content but attachment set to empty array", %{
+ user: user,
+ recipient: recipient
+ } do
+ {:ok, valid_chat_message, _} = Builder.chat_message(user, recipient.ap_id, "Hello!")
+
+ valid_chat_message =
+ valid_chat_message
+ |> Map.put("attachment", [])
+
+ assert {:ok, object, _meta} = ObjectValidator.validate(valid_chat_message, [])
+
+ assert object == Map.drop(valid_chat_message, ["attachment"])
+ end
+
test "does not validate if the message has no content", %{
valid_chat_message: valid_chat_message
} do