WebPush: Don't break on contentless chat messages.
authorlain <lain@soykaf.club>
Sat, 6 Jun 2020 07:46:07 +0000 (09:46 +0200)
committerlain <lain@soykaf.club>
Sat, 6 Jun 2020 07:46:07 +0000 (09:46 +0200)
lib/pleroma/web/push/impl.ex
test/web/push/impl_test.exs

index 006a242af275e39eeea307e68c6b5599b29397aa..cdb827e7664820f7d08088ad489a7e7f21a033ef 100644 (file)
@@ -124,6 +124,13 @@ defmodule Pleroma.Web.Push.Impl do
 
   def format_body(activity, actor, object, mastodon_type \\ nil)
 
+  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)}"
+    end
+  end
+
   def format_body(
         %{activity: %{data: %{"type" => "Create"}}},
         actor,
index 8fb7faaa51f9aaa481e36af30a73765f26c7058a..b48952b29026ffde4f12e2017736d4875ea69063 100644 (file)
@@ -8,6 +8,7 @@ defmodule Pleroma.Web.Push.ImplTest do
   alias Pleroma.Notification
   alias Pleroma.Object
   alias Pleroma.User
+  alias Pleroma.Web.ActivityPub.ActivityPub
   alias Pleroma.Web.CommonAPI
   alias Pleroma.Web.Push.Impl
   alias Pleroma.Web.Push.Subscription
@@ -213,6 +214,30 @@ defmodule Pleroma.Web.Push.ImplTest do
              }
     end
 
+    test "builds content for chat messages with no content" do
+      user = insert(:user)
+      recipient = insert(:user)
+
+      file = %Plug.Upload{
+        content_type: "image/jpg",
+        path: Path.absname("test/fixtures/image.jpg"),
+        filename: "an_image.jpg"
+      }
+
+      {:ok, upload} = ActivityPub.upload(file, actor: user.ap_id)
+
+      {:ok, chat} = CommonAPI.post_chat_message(user, recipient, nil, media_id: upload.id)
+      object = Object.normalize(chat, false)
+      [notification] = Notification.for_user(recipient)
+
+      res = Impl.build_content(notification, user, object)
+
+      assert res == %{
+               body: "@#{user.nickname}: (Attachment)",
+               title: "New Chat Message"
+             }
+    end
+
     test "hides details for notifications when privacy option enabled" do
       user = insert(:user, nickname: "Bob")
       user2 = insert(:user, nickname: "Rob", notification_settings: %{privacy_option: true})