Web Push notifications should not embed HTML for preserving newlines, so give it its own filtering

This commit is contained in:
Mark Felder 2021-06-12 12:41:12 -05:00
parent bb4130d48c
commit e0a521bbfb
3 changed files with 42 additions and 21 deletions

View file

@ -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/<br\s?\/?>/, "&#10;&#13;")
|> 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/<br\s?\/?>/, " ")
|> HTML.strip_tags()
|> HtmlEntities.decode()
|> Formatter.truncate(max_length)
end
end

View file

@ -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/<br\s?\/?>/, "\r\n")
|> Formatter.truncate(max_length)
end
end

View file

@ -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