From 5c62c50e9b5309f0368ca66b3b3566868594d3b5 Mon Sep 17 00:00:00 2001 From: tusooa Date: Thu, 29 Dec 2022 12:13:58 -0500 Subject: [PATCH] Generate * from *Map MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- lib/pleroma/multi_language.ex | 2 +- .../object_validators/article_note_page_validator.ex | 4 +++- .../object_validators/audio_image_video_validator.ex | 3 +++ .../web/activity_pub/object_validators/common_fixes.ex | 9 +++++++++ .../activity_pub/object_validators/question_validator.ex | 3 +++ .../article_note_page_validator_test.exs | 6 ++++++ 6 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/pleroma/multi_language.ex b/lib/pleroma/multi_language.ex index 4e0b735e26..0c02757eef 100644 --- a/lib/pleroma/multi_language.ex +++ b/lib/pleroma/multi_language.ex @@ -26,7 +26,7 @@ defp map_to_str_impl(data, mode) do |> Enum.join(sep(mode)) else [lang] -> data[lang] - _ -> "" + _ -> nil end end diff --git a/lib/pleroma/web/activity_pub/object_validators/article_note_page_validator.ex b/lib/pleroma/web/activity_pub/object_validators/article_note_page_validator.ex index 15d0b7dcdd..bd2667b2cf 100644 --- a/lib/pleroma/web/activity_pub/object_validators/article_note_page_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/article_note_page_validator.ex @@ -86,7 +86,9 @@ defp fix(data) do |> fix_attachments() |> CommonFixes.fix_quote_url() |> Transmogrifier.fix_emoji() - |> Transmogrifier.fix_content_map() + |> CommonFixes.fix_multilang_field("content", "contentMap", multiline: true) + |> CommonFixes.fix_multilang_field("summary", "summaryMap", multiline: false) + |> CommonFixes.fix_multilang_field("name", "nameMap", multiline: false) |> CommonFixes.maybe_add_language() |> CommonFixes.maybe_add_content_map() end diff --git a/lib/pleroma/web/activity_pub/object_validators/audio_image_video_validator.ex b/lib/pleroma/web/activity_pub/object_validators/audio_image_video_validator.ex index 65ac6bb935..c500bfd525 100644 --- a/lib/pleroma/web/activity_pub/object_validators/audio_image_video_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/audio_image_video_validator.ex @@ -102,6 +102,9 @@ defp fix(data) do |> CommonFixes.fix_quote_url() |> Transmogrifier.fix_emoji() |> fix_url() + |> CommonFixes.fix_multilang_field("content", "contentMap", multiline: true) + |> CommonFixes.fix_multilang_field("summary", "summaryMap", multiline: false) + |> CommonFixes.fix_multilang_field("name", "nameMap", multiline: false) |> fix_content() end diff --git a/lib/pleroma/web/activity_pub/object_validators/common_fixes.ex b/lib/pleroma/web/activity_pub/object_validators/common_fixes.ex index 3357f57a52..ea90e0819e 100644 --- a/lib/pleroma/web/activity_pub/object_validators/common_fixes.ex +++ b/lib/pleroma/web/activity_pub/object_validators/common_fixes.ex @@ -194,4 +194,13 @@ def maybe_add_content_map(%{"language" => language, "content" => content} = obje end def maybe_add_content_map(object), do: object + + def fix_multilang_field(data, str_field, map_field, opts \\ []) do + with %{} = map <- data[map_field], + str when is_binary(str) <- Pleroma.MultiLanguage.map_to_str(map, opts) do + Map.put(data, str_field, str) + else + _ -> data + end + end end diff --git a/lib/pleroma/web/activity_pub/object_validators/question_validator.ex b/lib/pleroma/web/activity_pub/object_validators/question_validator.ex index b28f096e52..4996a90aac 100644 --- a/lib/pleroma/web/activity_pub/object_validators/question_validator.ex +++ b/lib/pleroma/web/activity_pub/object_validators/question_validator.ex @@ -67,6 +67,9 @@ defp fix(data) do |> CommonFixes.fix_quote_url() |> Transmogrifier.fix_emoji() |> fix_closed() + |> CommonFixes.fix_multilang_field("content", "contentMap", multiline: true) + |> CommonFixes.fix_multilang_field("summary", "summaryMap", multiline: false) + |> CommonFixes.fix_multilang_field("name", "nameMap", multiline: false) end def changeset(struct, data) do diff --git a/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs b/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs index f92f95c6e0..1f2c5a04c1 100644 --- a/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs +++ b/test/pleroma/web/activity_pub/object_validators/article_note_page_validator_test.exs @@ -54,9 +54,14 @@ test "Note with contentMap and summaryMap", %{note: note} do |> Map.put("summaryMap", summary_map) |> Map.put("contentMap", content_map) + expected_summary = Pleroma.MultiLanguage.map_to_str(summary_map, multiline: false) + expected_content = Pleroma.MultiLanguage.map_to_str(content_map, multiline: true) + assert %{ valid?: true, changes: %{ + summary: ^expected_summary, + content: ^expected_content, summaryMap: ^summary_map, contentMap: ^content_map } @@ -74,6 +79,7 @@ test "Note with empty *Map", %{note: note} do changes: changes } = ArticleNotePageValidator.cast_and_validate(note) + assert changes.content == note["content"] assert Map.has_key?(changes, :summaryMap) refute Map.has_key?(changes, :contentMap) end