From e0a521bbfb3df5792d0c6879df71338a4c9e3f26 Mon Sep 17 00:00:00 2001 From: Mark Felder Date: Sat, 12 Jun 2021 12:41:12 -0500 Subject: [PATCH] Web Push notifications should not embed HTML for preserving newlines, so give it its own filtering --- lib/pleroma/web/metadata/utils.ex | 17 ----------------- lib/pleroma/web/push/impl.ex | 21 +++++++++++++++++---- test/pleroma/web/push/impl_test.exs | 25 +++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 21 deletions(-) diff --git a/lib/pleroma/web/metadata/utils.ex b/lib/pleroma/web/metadata/utils.ex index 4c763a877..dd80c3277 100644 --- a/lib/pleroma/web/metadata/utils.ex +++ b/lib/pleroma/web/metadata/utils.ex @@ -13,12 +13,6 @@ defmodule Pleroma.Web.Metadata.Utils do def filter_html_and_truncate(content, max_length \\ nil), do: do_filter_html_and_truncate(content, max_length) - def scrub_html_and_truncate(%{data: %{"content" => content}} = _object), - do: do_scrub_html_and_truncate(content) - - def scrub_html_and_truncate(content, max_length \\ nil), - do: do_scrub_html_and_truncate(content, max_length) - def user_name_string(user) do "#{user.name} " <> if user.local do @@ -45,15 +39,4 @@ defmodule Pleroma.Web.Metadata.Utils do |> String.replace(~r//, " ") |> Formatter.truncate(max_length) end - - defp do_scrub_html_and_truncate(content, max_length \\ 200) when is_binary(content) do - # html content comes from DB already encoded - content - |> HtmlEntities.decode() - |> Emoji.Formatter.demojify() - |> String.replace(~r//, " ") - |> HTML.strip_tags() - |> HtmlEntities.decode() - |> Formatter.truncate(max_length) - end end diff --git a/lib/pleroma/web/push/impl.ex b/lib/pleroma/web/push/impl.ex index 83cbdc870..6d83df8ab 100644 --- a/lib/pleroma/web/push/impl.ex +++ b/lib/pleroma/web/push/impl.ex @@ -6,11 +6,13 @@ defmodule Pleroma.Web.Push.Impl do @moduledoc "The module represents implementation push web notification" alias Pleroma.Activity + alias Pleroma.Emoji + alias Pleroma.Formatter + alias Pleroma.HTML alias Pleroma.Notification alias Pleroma.Object alias Pleroma.Repo alias Pleroma.User - alias Pleroma.Web.Metadata.Utils alias Pleroma.Web.Push.Subscription require Logger @@ -127,7 +129,7 @@ defmodule Pleroma.Web.Push.Impl do def format_body(_activity, actor, %{data: %{"type" => "ChatMessage", "content" => content}}, _) do case content do nil -> "@#{actor.nickname}: (Attachment)" - content -> "@#{actor.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}" + content -> "@#{actor.nickname}: #{filter_html_and_truncate(content, 80)}" end end @@ -137,7 +139,7 @@ defmodule Pleroma.Web.Push.Impl do %{data: %{"content" => content}}, _mastodon_type ) do - "@#{actor.nickname}: #{Utils.scrub_html_and_truncate(content, 80)}" + "@#{actor.nickname}: #{filter_html_and_truncate(content, 80)}" end def format_body( @@ -146,7 +148,7 @@ defmodule Pleroma.Web.Push.Impl do %{data: %{"content" => content}}, _mastodon_type ) do - "@#{actor.nickname} repeated: #{Utils.scrub_html_and_truncate(content, 80)}" + "@#{actor.nickname} repeated: #{filter_html_and_truncate(content, 80)}" end def format_body( @@ -192,4 +194,15 @@ defmodule Pleroma.Web.Push.Impl do type -> "New #{String.capitalize(type || "event")}" end end + + defp filter_html_and_truncate(content, max_length) when is_binary(content) do + # html content comes from DB already encoded + content + |> HtmlEntities.decode() + |> Emoji.Formatter.demojify() + |> HTML.filter_tags(Pleroma.HTML.Scrubber.BreaksOnly) + |> HtmlEntities.decode() + |> String.replace(~r//, "\r\n") + |> Formatter.truncate(max_length) + end end diff --git a/test/pleroma/web/push/impl_test.exs b/test/pleroma/web/push/impl_test.exs index b3ca1a337..24304a317 100644 --- a/test/pleroma/web/push/impl_test.exs +++ b/test/pleroma/web/push/impl_test.exs @@ -359,4 +359,29 @@ defmodule Pleroma.Web.Push.ImplTest do } end end + + test "body for create activity handles newlines" do + user = insert(:user, nickname: "bob") + _user2 = insert(:user, nickname: "alice") + + {:ok, activity} = + CommonAPI.post(user, %{ + status: """ + @alice Line one + Line two + Line three + """ + }) + + object = Object.normalize(activity, fetch: false) + + assert Impl.format_body( + %{ + activity: activity + }, + user, + object + ) == + "@bob: @alice Line one\r\nLine two\r\nLine three" + end end