Mark notifications about statuses from muted users as read automatically
authorSergey Suprunenko <suprunenko.s@gmail.com>
Sat, 15 Aug 2020 13:27:41 +0000 (15:27 +0200)
committerSergey Suprunenko <suprunenko.s@gmail.com>
Sat, 15 Aug 2020 13:27:41 +0000 (15:27 +0200)
lib/pleroma/notification.ex
test/notification_test.exs
test/web/mastodon_api/views/notification_view_test.exs

index 0b171563b291873000182e7345711fb2939fc1d5..b4719896e28fac82191b8bb53b0b43ad4b5a57e1 100644 (file)
@@ -441,6 +441,7 @@ defmodule Pleroma.Notification do
         |> Multi.insert(:notification, %Notification{
           user_id: user.id,
           activity: activity,
+          seen: mark_as_read?(activity, user),
           type: type_from_activity(activity)
         })
         |> Marker.multi_set_last_read_id(user, "notifications")
@@ -634,6 +635,11 @@ defmodule Pleroma.Notification do
 
   def skip?(_, _, _), do: false
 
+  def mark_as_read?(activity, target_user) do
+    user = Activity.user_actor(activity)
+    User.mutes_user?(target_user, user)
+  end
+
   def for_user_and_activity(user, activity) do
     from(n in __MODULE__,
       where: n.user_id == ^user.id,
index 8243cfd347309ec54b5ff69faf1a4af7a6e7649e..93f4761da513bc511a9f945b794925574b50552a 100644 (file)
@@ -217,7 +217,10 @@ defmodule Pleroma.NotificationTest do
       muter = Repo.get(User, muter.id)
       {:ok, activity} = CommonAPI.post(muted, %{status: "Hi @#{muter.nickname}"})
 
-      assert Notification.create_notification(activity, muter)
+      notification = Notification.create_notification(activity, muter)
+
+      assert notification.id
+      assert notification.seen
     end
 
     test "notification created if user is muted without notifications" do
@@ -1012,6 +1015,7 @@ defmodule Pleroma.NotificationTest do
       [notification] = Notification.for_user(user)
 
       assert notification.activity.object
+      assert notification.seen
     end
 
     test "it doesn't return notifications for muted user with notifications", %{user: user} do
index 8e0e58538a45bfa5d0b21e657ca0a4b5ed66fc9b..2f6a808f1c540414b851e0791c37f868568b6eeb 100644 (file)
@@ -219,7 +219,7 @@ defmodule Pleroma.Web.MastodonAPI.NotificationViewTest do
 
     expected = %{
       id: to_string(notification.id),
-      pleroma: %{is_seen: false, is_muted: true},
+      pleroma: %{is_seen: true, is_muted: true},
       type: "favourite",
       account: AccountView.render("show.json", %{user: another_user, for: user}),
       status: StatusView.render("show.json", %{activity: create_activity, for: user}),