From 830385ea2b96dd14a5af200ad559019042190e7a Mon Sep 17 00:00:00 2001 From: thepra Date: Wed, 7 Oct 2026 11:07:17 +0200 Subject: [PATCH] An empty media_ids takes an edited post's media away A JSON body's empty array flattened to nothing, so `media_ids: []` on an edit read as "media not sent" and the post kept every attachment, where Mastodon removes them. An empty array is now a parameter with no values: the edit leaves the post without media, and they go to the trash. MediaLifecycleTests checks it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01LsXgEaXee4GCU1hwYgPJXw --- PrivaPub.Tests/Http/MediaLifecycleTests.cs | 9 ++++++++- PrivaPub/Api/Mastodon/Infrastructure/MastodonParams.cs | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/PrivaPub.Tests/Http/MediaLifecycleTests.cs b/PrivaPub.Tests/Http/MediaLifecycleTests.cs index 0cbd82b..245a888 100644 --- a/PrivaPub.Tests/Http/MediaLifecycleTests.cs +++ b/PrivaPub.Tests/Http/MediaLifecycleTests.cs @@ -101,9 +101,16 @@ namespace PrivaPub.Tests.Http Assert.Equal("it", edited.Body.Text("language")); Assert.Equal("a red square", (await DB.Default.Find().OneAsync(id, Token)).Description); - // an edit that doesn't send media keeps them, described as they were + // an edit that doesn't send media keeps them, described as they were; an empty media_ids takes them away var kept = (await alice.Client.Put($"/api/v1/statuses/{statusId}", ("status", "media kept"))).Ok(); Assert.Equal("a red square", kept.Body["media_attachments"]![0]!.Text("description")); + var bare = (await alice.Client.Json(HttpMethod.Put, $"/api/v1/statuses/{statusId}", new System.Text.Json.Nodes.JsonObject + { + ["status"] = "no picture now", + ["media_ids"] = new System.Text.Json.Nodes.JsonArray() + })).Ok(); + Assert.Empty(bare.Body["media_attachments"]!.AsArray()); + Assert.Equal("edited out", (await DB.Default.Find().OneAsync(id, Token)).TrashReason); } [Fact] diff --git a/PrivaPub/Api/Mastodon/Infrastructure/MastodonParams.cs b/PrivaPub/Api/Mastodon/Infrastructure/MastodonParams.cs index 4b4e1c2..b62ef7e 100644 --- a/PrivaPub/Api/Mastodon/Infrastructure/MastodonParams.cs +++ b/PrivaPub/Api/Mastodon/Infrastructure/MastodonParams.cs @@ -89,6 +89,9 @@ namespace PrivaPub.Api.Mastodon.Infrastructure Flatten(values, prefix == default ? property.Name : $"{prefix}[{property.Name}]", property.Value); break; case JsonValueKind.Array: + // an empty array is still said: media_ids: [] on an edit takes every attachment away + if (prefix != default && element.GetArrayLength() == 0) + values.TryAdd(prefix, new List()); foreach (var item in element.EnumerateArray()) { if (item.ValueKind is JsonValueKind.Object or JsonValueKind.Array)