Add check to prevent multiple follow notifications from the same user
authorSadposter <hannah+pleroma@coffee-and-dreams.uk>
Sat, 5 Jan 2019 22:54:25 +0000 (22:54 +0000)
committerkaniini <nenolod@gmail.com>
Sat, 5 Jan 2019 22:54:25 +0000 (22:54 +0000)
lib/pleroma/notification.ex
test/notification_test.exs

index 51d59870ce25ed86d9b7aee2ba798e36abe12394..c7d01f63b83517c36b15779ec9a9772524d5b6d4 100644 (file)
@@ -109,7 +109,12 @@ defmodule Pleroma.Notification do
   # TODO move to sql, too.
   def create_notification(%Activity{} = activity, %User{} = user) do
     unless User.blocks?(user, %{ap_id: activity.data["actor"]}) or
-             user.ap_id == activity.data["actor"] do
+             user.ap_id == activity.data["actor"] or
+             (activity.data["type"] == "Follow" and
+                Enum.any?(Notification.for_user(user), fn notif ->
+                  notif.activity.data["type"] == "Follow" and
+                    notif.activity.data["actor"] == activity.data["actor"]
+                end)) do
       notification = %Notification{user_id: user.id, activity: activity}
       {:ok, notification} = Repo.insert(notification)
       Pleroma.Web.Streamer.stream("user", notification)
index 31c0d2c64848f258748786722d8c14e35ca6fac9..94fb0ab154ae81a10583041a549f247091632cc7 100644 (file)
@@ -46,6 +46,43 @@ defmodule Pleroma.NotificationTest do
 
       assert nil == Notification.create_notification(activity, author)
     end
+
+    test "it doesn't create a notification for follow-unfollow-follow chains" do
+      user = insert(:user)
+      followed_user = insert(:user)
+      {:ok, _, _, activity} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
+      Notification.create_notification(activity, followed_user)
+      TwitterAPI.unfollow(user, %{"user_id" => followed_user.id})
+      {:ok, _, _, activity_dupe} = TwitterAPI.follow(user, %{"user_id" => followed_user.id})
+      assert nil == Notification.create_notification(activity_dupe, followed_user)
+    end
+
+    test "it doesn't create a notification for like-unlike-like chains" do
+      user = insert(:user)
+      liked_user = insert(:user)
+      {:ok, status} = TwitterAPI.create_status(liked_user, %{"status" => "Yui is best yuru"})
+      {:ok, fav_status} = TwitterAPI.fav(user, status.id)
+      Notification.create_notification(fav_status, liked_user)
+      TwitterAPI.unfav(user, status.id)
+      {:ok, dupe} = TwitterAPI.fav(user, status.id)
+      assert nil == Notification.create_notification(dupe, liked_user)
+    end
+
+    test "it doesn't create a notification for repeat-unrepeat-repeat chains" do
+      user = insert(:user)
+      retweeted_user = insert(:user)
+
+      {:ok, status} =
+        TwitterAPI.create_status(retweeted_user, %{
+          "status" => "Send dupe notifications to the shadow realm"
+        })
+
+      {:ok, retweeted_activity} = TwitterAPI.repeat(user, status.id)
+      Notification.create_notification(retweeted_activity, retweeted_user)
+      TwitterAPI.unrepeat(user, status.id)
+      {:ok, dupe} = TwitterAPI.repeat(user, status.id)
+      assert nil == Notification.create_notification(dupe, retweeted_user)
+    end
   end
 
   describe "get notification" do