ChatMessage: Tolerate attachment field set to an empty array

Closes: https://git.pleroma.social/pleroma/pleroma/-/issues/3224
This commit is contained in:
Haelwenn (lanodan) Monnier 2023-12-29 05:23:01 +01:00
parent 47e00524f7
commit 39dc6c65ef
3 changed files with 21 additions and 0 deletions

View File

@ -0,0 +1 @@
ChatMessage: Tolerate attachment field set to an empty array

View File

@ -57,6 +57,11 @@ def fix_attachment(%{"attachment" => [attachment | _]} = data) do
|> Map.put("attachment", attachment) |> Map.put("attachment", attachment)
end end
def fix_attachment(%{"attachment" => attachment} = data) when attachment == [] do
data
|> Map.drop(["attachment"])
end
def fix_attachment(data), do: data def fix_attachment(data), do: data
def changeset(struct, data) do def changeset(struct, data) do

View File

@ -147,6 +147,21 @@ test "validates for a basic object with an attachment but without content", %{
assert object["attachment"] assert object["attachment"]
end 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", %{ test "does not validate if the message has no content", %{
valid_chat_message: valid_chat_message valid_chat_message: valid_chat_message
} do } do