Dismiss the follow request notification on rejection
authoreugenijm <eugenijm@protonmail.com>
Thu, 30 Apr 2020 12:02:35 +0000 (15:02 +0300)
committereugenijm <eugenijm@protonmail.com>
Thu, 30 Apr 2020 15:38:19 +0000 (18:38 +0300)
lib/pleroma/notification.ex
lib/pleroma/web/common_api/common_api.ex
test/notification_test.exs

index aaa67525366c0aca362970525eb366e2403ba696..9a109dfabf66f6ebe55d56e6cb7731aa37daff70 100644 (file)
@@ -261,6 +261,16 @@ defmodule Pleroma.Notification do
     |> Repo.delete_all()
   end
 
+  def dismiss(%Pleroma.Activity{} = activity) do
+    Notification
+    |> where([n], n.activity_id == ^activity.id)
+    |> Repo.delete_all()
+    |> case do
+      {_, notifications} -> {:ok, notifications}
+      _ -> {:error, "Cannot dismiss notification"}
+    end
+  end
+
   def dismiss(%{id: user_id} = _user, id) do
     notification = Repo.get(Notification, id)
 
index d1efe0c36e0d1a19eb9377d700639c2434ef8bb0..4112e441ad7925bdfb2ae0f2fb0200d8d9ddf7e6 100644 (file)
@@ -7,6 +7,7 @@ defmodule Pleroma.Web.CommonAPI do
   alias Pleroma.ActivityExpiration
   alias Pleroma.Conversation.Participation
   alias Pleroma.FollowingRelationship
+  alias Pleroma.Notification
   alias Pleroma.Object
   alias Pleroma.ThreadMute
   alias Pleroma.User
@@ -61,6 +62,7 @@ defmodule Pleroma.Web.CommonAPI do
     with %Activity{} = follow_activity <- Utils.fetch_latest_follow(follower, followed),
          {:ok, follow_activity} <- Utils.update_follow_state_for_all(follow_activity, "reject"),
          {:ok, _relationship} <- FollowingRelationship.update(follower, followed, :follow_reject),
+         {:ok, _notifications} <- Notification.dismiss(follow_activity),
          {:ok, _activity} <-
            ActivityPub.reject(%{
              to: [follower.ap_id],
index 6ad824c57e1a4e39db0f75bfca20194ba541052d..0e9ffcb1824435d251d21416ae27ad3f29ec289c 100644 (file)
@@ -362,6 +362,16 @@ defmodule Pleroma.NotificationTest do
       notification_id = notification.id
       assert [%{id: ^notification_id}] = Notification.for_user(followed_user)
     end
+
+    test "dismisses the notification on follow request rejection" do
+      clear_config([:notifications, :enable_follow_request_notifications], true)
+      user = insert(:user, locked: true)
+      follower = insert(:user)
+      {:ok, _, _, _follow_activity} = CommonAPI.follow(follower, user)
+      assert [notification] = Notification.for_user(user)
+      {:ok, _follower} = CommonAPI.reject_follow_request(follower, user)
+      assert [] = Notification.for_user(user)
+    end
   end
 
   describe "get notification" do