Render nice web push notifications for polls

This commit is contained in:
Mark Felder 2024-06-08 20:30:43 -04:00
parent b1ef6e5e9a
commit 3211557f74
4 changed files with 42 additions and 0 deletions

View file

@ -0,0 +1 @@
Render nice web push notifications for polls

View file

@ -130,6 +130,24 @@ defmodule Pleroma.Web.Push.Impl do
end end
end end
def format_body(
%{type: "poll"} = _notification,
_user,
%{data: %{"content" => content} = data} = _object
) do
options = Map.get(data, "anyOf") || Map.get(data, "oneOf")
content_text = content <> "\n"
options_text =
Enum.map(options, fn x -> "#{x["name"]}" end)
|> Enum.join("\n")
[content_text, options_text]
|> Enum.join("\n")
|> Utils.scrub_html_and_truncate(80)
end
def format_body( def format_body(
%{activity: %{data: %{"type" => "Create"}}}, %{activity: %{data: %{"type" => "Create"}}},
user, user,
@ -191,6 +209,7 @@ defmodule Pleroma.Web.Push.Impl do
"update" -> "New Update" "update" -> "New Update"
"pleroma:chat_mention" -> "New Chat Message" "pleroma:chat_mention" -> "New Chat Message"
"pleroma:emoji_reaction" -> "New Reaction" "pleroma:emoji_reaction" -> "New Reaction"
"poll" -> "Poll Results"
type -> "New #{String.capitalize(type || "event")}" type -> "New #{String.capitalize(type || "event")}"
end end
end end

View file

@ -232,6 +232,27 @@ defmodule Pleroma.Web.Push.ImplTest do
"New Direct Message" "New Direct Message"
end end
test "renders poll notification" do
user = insert(:user)
question = insert(:question, user: user)
activity = insert(:question_activity, question: question)
{:ok, [notification]} = Notification.create_poll_notifications(activity)
assert Impl.format_title(notification) == "Poll Results"
expected_body =
"""
Which flavor of ice cream do you prefer?
chocolate
vanilla
"""
|> String.trim_trailing("\n")
assert Impl.format_body(notification, user, question) == expected_body
end
describe "build_content/3" do describe "build_content/3" do
test "builds content for chat messages" do test "builds content for chat messages" do
user = insert(:user) user = insert(:user)

View file

@ -249,6 +249,7 @@ defmodule Pleroma.Factory do
"cc" => [user.follower_address], "cc" => [user.follower_address],
"context" => Pleroma.Web.ActivityPub.Utils.generate_context_id(), "context" => Pleroma.Web.ActivityPub.Utils.generate_context_id(),
"closed" => DateTime.utc_now() |> DateTime.add(86_400) |> DateTime.to_iso8601(), "closed" => DateTime.utc_now() |> DateTime.add(86_400) |> DateTime.to_iso8601(),
"content" => "Which flavor of ice cream do you prefer?",
"oneOf" => [ "oneOf" => [
%{ %{
"type" => "Note", "type" => "Note",