From fb654acfadfeec00a1a52e2af96e922dc4b88b01 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Sat, 20 Jul 2024 23:48:54 -0400 Subject: [PATCH] Fix OpenGraph and Twitter metadata providers when parsing objects with no content or summary fields. --- changelog.d/metadata-provider-empty-post.fix | 1 + lib/pleroma/web/metadata/utils.ex | 5 +++- test/pleroma/web/metadata/utils_test.exs | 28 +++++++++++++++----- 3 files changed, 27 insertions(+), 7 deletions(-) create mode 100644 changelog.d/metadata-provider-empty-post.fix diff --git a/changelog.d/metadata-provider-empty-post.fix b/changelog.d/metadata-provider-empty-post.fix new file mode 100644 index 000000000..8d6341c6c --- /dev/null +++ b/changelog.d/metadata-provider-empty-post.fix @@ -0,0 +1 @@ +Fix OpenGraph and Twitter metadata providers when parsing objects with no content or summary fields. diff --git a/lib/pleroma/web/metadata/utils.ex b/lib/pleroma/web/metadata/utils.ex index 80a8be9a2..8f61ace24 100644 --- a/lib/pleroma/web/metadata/utils.ex +++ b/lib/pleroma/web/metadata/utils.ex @@ -25,11 +25,14 @@ defmodule Pleroma.Web.Metadata.Utils do |> scrub_html_and_truncate_object_field(object) end - def scrub_html_and_truncate(%{data: %{"content" => content}} = object) do + def scrub_html_and_truncate(%{data: %{"content" => content}} = object) + when is_binary(content) and content != "" do content |> scrub_html_and_truncate_object_field(object) end + def scrub_html_and_truncate(%{}), do: "" + def scrub_html_and_truncate(content, max_length \\ 200, omission \\ "...") when is_binary(content) do content diff --git a/test/pleroma/web/metadata/utils_test.exs b/test/pleroma/web/metadata/utils_test.exs index 3daf852fb..9bc02dadf 100644 --- a/test/pleroma/web/metadata/utils_test.exs +++ b/test/pleroma/web/metadata/utils_test.exs @@ -8,7 +8,7 @@ defmodule Pleroma.Web.Metadata.UtilsTest do alias Pleroma.Web.Metadata.Utils describe "scrub_html_and_truncate/1" do - test "it returns content text without encode HTML if summary is nil" do + test "it returns content text without HTML if summary is nil" do user = insert(:user) note = @@ -17,14 +17,14 @@ defmodule Pleroma.Web.Metadata.UtilsTest do "actor" => user.ap_id, "id" => "https://pleroma.gov/objects/whatever", "summary" => nil, - "content" => "Pleroma's really cool!" + "content" => "Pleroma's really cool!
" } }) assert Utils.scrub_html_and_truncate(note) == "Pleroma's really cool!" end - test "it returns context text without encode HTML if summary is empty" do + test "it returns content text without HTML if summary is empty" do user = insert(:user) note = @@ -33,14 +33,14 @@ defmodule Pleroma.Web.Metadata.UtilsTest do "actor" => user.ap_id, "id" => "https://pleroma.gov/objects/whatever", "summary" => "", - "content" => "Pleroma's really cool!" + "content" => "Pleroma's really cool!
" } }) assert Utils.scrub_html_and_truncate(note) == "Pleroma's really cool!" end - test "it returns summary text without encode HTML if summary is filled" do + test "it returns summary text without HTML if summary is filled" do user = insert(:user) note = @@ -48,7 +48,7 @@ defmodule Pleroma.Web.Metadata.UtilsTest do data: %{ "actor" => user.ap_id, "id" => "https://pleroma.gov/objects/whatever", - "summary" => "Public service announcement on caffeine consumption", + "summary" => "Public service announcement on caffeine consumption
", "content" => "cofe" } }) @@ -57,6 +57,22 @@ defmodule Pleroma.Web.Metadata.UtilsTest do "Public service announcement on caffeine consumption" end + test "it returns empty string if summary and content are absent" do + user = insert(:user) + + note = + insert(:note, %{ + data: %{ + "actor" => user.ap_id, + "id" => "https://pleroma.gov/objects/whatever", + "content" => nil, + "summary" => nil + } + }) + + assert Utils.scrub_html_and_truncate(note) == "" + end + test "it does not return old content after editing" do user = insert(:user)