diff options
| author | Mark Felder <feld@feld.me> | 2024-06-08 20:30:43 -0400 | 
|---|---|---|
| committer | Mark Felder <feld@feld.me> | 2024-06-08 22:08:12 -0400 | 
| commit | 3211557f742e07d5144426a71c39267480656a38 (patch) | |
| tree | aa60ce570d4f716f8f927541946713f1fae79ec7 | |
| parent | b1ef6e5e9ad5afc9d556a4d88ff34c9b62f5fea2 (diff) | |
| download | pleroma-3211557f742e07d5144426a71c39267480656a38.tar.gz pleroma-3211557f742e07d5144426a71c39267480656a38.zip | |
Render nice web push notifications for polls
| -rw-r--r-- | changelog.d/webpush-polls.change | 1 | ||||
| -rw-r--r-- | lib/pleroma/web/push/impl.ex | 19 | ||||
| -rw-r--r-- | test/pleroma/web/push/impl_test.exs | 21 | ||||
| -rw-r--r-- | test/support/factory.ex | 1 | 
4 files changed, 42 insertions, 0 deletions
| diff --git a/changelog.d/webpush-polls.change b/changelog.d/webpush-polls.change new file mode 100644 index 000000000..5607d6bfc --- /dev/null +++ b/changelog.d/webpush-polls.change @@ -0,0 +1 @@ +Render nice web push notifications for polls diff --git a/lib/pleroma/web/push/impl.ex b/lib/pleroma/web/push/impl.ex index df7e0c53f..98e7659ab 100644 --- a/lib/pleroma/web/push/impl.ex +++ b/lib/pleroma/web/push/impl.ex @@ -131,6 +131,24 @@ defmodule Pleroma.Web.Push.Impl do    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(          %{activity: %{data: %{"type" => "Create"}}},          user,          %{data: %{"content" => content}} @@ -191,6 +209,7 @@ defmodule Pleroma.Web.Push.Impl do        "update" -> "New Update"        "pleroma:chat_mention" -> "New Chat Message"        "pleroma:emoji_reaction" -> "New Reaction" +      "poll" -> "Poll Results"        type -> "New #{String.capitalize(type || "event")}"      end    end diff --git a/test/pleroma/web/push/impl_test.exs b/test/pleroma/web/push/impl_test.exs index 3ceea3d71..e1bbf8d17 100644 --- a/test/pleroma/web/push/impl_test.exs +++ b/test/pleroma/web/push/impl_test.exs @@ -232,6 +232,27 @@ defmodule Pleroma.Web.Push.ImplTest do               "New Direct Message"    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      test "builds content for chat messages" do        user = insert(:user) diff --git a/test/support/factory.ex b/test/support/factory.ex index 20bc5162e..b248508fa 100644 --- a/test/support/factory.ex +++ b/test/support/factory.ex @@ -249,6 +249,7 @@ defmodule Pleroma.Factory do        "cc" => [user.follower_address],        "context" => Pleroma.Web.ActivityPub.Utils.generate_context_id(),        "closed" => DateTime.utc_now() |> DateTime.add(86_400) |> DateTime.to_iso8601(), +      "content" => "Which flavor of ice cream do you prefer?",        "oneOf" => [          %{            "type" => "Note", | 
