Merge branch 'rework-emoji-management' into 'develop'
[akkoma] / test / notification_test.exs
index 94fb0ab154ae81a10583041a549f247091632cc7..c3db77b6c0943239ce53465e19677e49bebee67c 100644 (file)
@@ -4,10 +4,11 @@
 
 defmodule Pleroma.NotificationTest do
   use Pleroma.DataCase
-  alias Pleroma.Web.TwitterAPI.TwitterAPI
-  alias Pleroma.Web.CommonAPI
-  alias Pleroma.{User, Notification}
+  alias Pleroma.Notification
+  alias Pleroma.User
   alias Pleroma.Web.ActivityPub.Transmogrifier
+  alias Pleroma.Web.CommonAPI
+  alias Pleroma.Web.TwitterAPI.TwitterAPI
   import Pleroma.Factory
 
   describe "create_notifications" do
@@ -28,6 +29,18 @@ defmodule Pleroma.NotificationTest do
       assert notification.activity_id == activity.id
       assert other_notification.activity_id == activity.id
     end
+
+    test "it creates a notification for subscribed users" do
+      user = insert(:user)
+      subscriber = insert(:user)
+
+      User.subscribe(subscriber, user)
+
+      {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
+      {:ok, [notification]} = Notification.create_notifications(status)
+
+      assert notification.user_id == subscriber.id
+    end
   end
 
   describe "create_notification" do
@@ -40,6 +53,75 @@ defmodule Pleroma.NotificationTest do
       assert nil == Notification.create_notification(activity, user)
     end
 
+    test "it doesn't create a notificatin for the user if the user mutes the activity author" do
+      muter = insert(:user)
+      muted = insert(:user)
+      {:ok, _} = User.mute(muter, muted)
+      muter = Repo.get(User, muter.id)
+      {:ok, activity} = CommonAPI.post(muted, %{"status" => "Hi @#{muter.nickname}"})
+
+      assert nil == Notification.create_notification(activity, muter)
+    end
+
+    test "it doesn't create a notification for an activity from a muted thread" do
+      muter = insert(:user)
+      other_user = insert(:user)
+      {:ok, activity} = CommonAPI.post(muter, %{"status" => "hey"})
+      CommonAPI.add_mute(muter, activity)
+
+      {:ok, activity} =
+        CommonAPI.post(other_user, %{
+          "status" => "Hi @#{muter.nickname}",
+          "in_reply_to_status_id" => activity.id
+        })
+
+      assert nil == Notification.create_notification(activity, muter)
+    end
+
+    test "it disables notifications from people on remote instances" do
+      user = insert(:user, info: %{notification_settings: %{"remote" => false}})
+      other_user = insert(:user)
+
+      create_activity = %{
+        "@context" => "https://www.w3.org/ns/activitystreams",
+        "type" => "Create",
+        "to" => ["https://www.w3.org/ns/activitystreams#Public"],
+        "actor" => other_user.ap_id,
+        "object" => %{
+          "type" => "Note",
+          "content" => "Hi @#{user.nickname}",
+          "attributedTo" => other_user.ap_id
+        }
+      }
+
+      {:ok, %{local: false} = activity} = Transmogrifier.handle_incoming(create_activity)
+      assert nil == Notification.create_notification(activity, user)
+    end
+
+    test "it disables notifications from people on the local instance" do
+      user = insert(:user, info: %{notification_settings: %{"local" => false}})
+      other_user = insert(:user)
+      {:ok, activity} = CommonAPI.post(other_user, %{"status" => "hey @#{user.nickname}"})
+      assert nil == Notification.create_notification(activity, user)
+    end
+
+    test "it disables notifications from followers" do
+      follower = insert(:user)
+      followed = insert(:user, info: %{notification_settings: %{"followers" => false}})
+      User.follow(follower, followed)
+      {:ok, activity} = CommonAPI.post(follower, %{"status" => "hey @#{followed.nickname}"})
+      assert nil == Notification.create_notification(activity, followed)
+    end
+
+    test "it disables notifications from people the user follows" do
+      follower = insert(:user, info: %{notification_settings: %{"follows" => false}})
+      followed = insert(:user)
+      User.follow(follower, followed)
+      follower = Repo.get(User, follower.id)
+      {:ok, activity} = CommonAPI.post(followed, %{"status" => "hey @#{follower.nickname}"})
+      assert nil == Notification.create_notification(activity, follower)
+    end
+
     test "it doesn't create a notification for user if he is the activity author" do
       activity = insert(:note_activity)
       author = User.get_by_ap_id(activity.data["actor"])
@@ -83,6 +165,28 @@ defmodule Pleroma.NotificationTest do
       {:ok, dupe} = TwitterAPI.repeat(user, status.id)
       assert nil == Notification.create_notification(dupe, retweeted_user)
     end
+
+    test "it doesn't create duplicate notifications for follow+subscribed users" do
+      user = insert(:user)
+      subscriber = insert(:user)
+
+      {:ok, _, _, _} = TwitterAPI.follow(subscriber, %{"user_id" => user.id})
+      User.subscribe(subscriber, user)
+      {:ok, status} = TwitterAPI.create_status(user, %{"status" => "Akariiiin"})
+      {:ok, [_notif]} = Notification.create_notifications(status)
+    end
+
+    test "it doesn't create subscription notifications if the recipient cannot see the status" do
+      user = insert(:user)
+      subscriber = insert(:user)
+
+      User.subscribe(subscriber, user)
+
+      {:ok, status} =
+        TwitterAPI.create_status(user, %{"status" => "inwisible", "visibility" => "direct"})
+
+      assert {:ok, []} == Notification.create_notifications(status)
+    end
   end
 
   describe "get notification" do
@@ -299,7 +403,7 @@ defmodule Pleroma.NotificationTest do
 
       {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
 
-      assert length(Notification.for_user(user)) == 0
+      assert Enum.empty?(Notification.for_user(user))
 
       {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
 
@@ -307,7 +411,7 @@ defmodule Pleroma.NotificationTest do
 
       {:ok, _} = CommonAPI.delete(activity.id, user)
 
-      assert length(Notification.for_user(user)) == 0
+      assert Enum.empty?(Notification.for_user(user))
     end
 
     test "liking an activity results in 1 notification, then 0 if the activity is unliked" do
@@ -316,7 +420,7 @@ defmodule Pleroma.NotificationTest do
 
       {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
 
-      assert length(Notification.for_user(user)) == 0
+      assert Enum.empty?(Notification.for_user(user))
 
       {:ok, _, _} = CommonAPI.favorite(activity.id, other_user)
 
@@ -324,7 +428,7 @@ defmodule Pleroma.NotificationTest do
 
       {:ok, _, _, _} = CommonAPI.unfavorite(activity.id, other_user)
 
-      assert length(Notification.for_user(user)) == 0
+      assert Enum.empty?(Notification.for_user(user))
     end
 
     test "repeating an activity results in 1 notification, then 0 if the activity is deleted" do
@@ -333,7 +437,7 @@ defmodule Pleroma.NotificationTest do
 
       {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
 
-      assert length(Notification.for_user(user)) == 0
+      assert Enum.empty?(Notification.for_user(user))
 
       {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
 
@@ -341,7 +445,7 @@ defmodule Pleroma.NotificationTest do
 
       {:ok, _} = CommonAPI.delete(activity.id, user)
 
-      assert length(Notification.for_user(user)) == 0
+      assert Enum.empty?(Notification.for_user(user))
     end
 
     test "repeating an activity results in 1 notification, then 0 if the activity is unrepeated" do
@@ -350,7 +454,7 @@ defmodule Pleroma.NotificationTest do
 
       {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
 
-      assert length(Notification.for_user(user)) == 0
+      assert Enum.empty?(Notification.for_user(user))
 
       {:ok, _, _} = CommonAPI.repeat(activity.id, other_user)
 
@@ -358,7 +462,7 @@ defmodule Pleroma.NotificationTest do
 
       {:ok, _, _} = CommonAPI.unrepeat(activity.id, other_user)
 
-      assert length(Notification.for_user(user)) == 0
+      assert Enum.empty?(Notification.for_user(user))
     end
 
     test "liking an activity which is already deleted does not generate a notification" do
@@ -367,15 +471,15 @@ defmodule Pleroma.NotificationTest do
 
       {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
 
-      assert length(Notification.for_user(user)) == 0
+      assert Enum.empty?(Notification.for_user(user))
 
       {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
 
-      assert length(Notification.for_user(user)) == 0
+      assert Enum.empty?(Notification.for_user(user))
 
       {:error, _} = CommonAPI.favorite(activity.id, other_user)
 
-      assert length(Notification.for_user(user)) == 0
+      assert Enum.empty?(Notification.for_user(user))
     end
 
     test "repeating an activity which is already deleted does not generate a notification" do
@@ -384,15 +488,15 @@ defmodule Pleroma.NotificationTest do
 
       {:ok, activity} = CommonAPI.post(user, %{"status" => "test post"})
 
-      assert length(Notification.for_user(user)) == 0
+      assert Enum.empty?(Notification.for_user(user))
 
       {:ok, _deletion_activity} = CommonAPI.delete(activity.id, user)
 
-      assert length(Notification.for_user(user)) == 0
+      assert Enum.empty?(Notification.for_user(user))
 
       {:error, _} = CommonAPI.repeat(activity.id, other_user)
 
-      assert length(Notification.for_user(user)) == 0
+      assert Enum.empty?(Notification.for_user(user))
     end
 
     test "replying to a deleted post without tagging does not generate a notification" do
@@ -408,7 +512,7 @@ defmodule Pleroma.NotificationTest do
           "in_reply_to_status_id" => activity.id
         })
 
-      assert length(Notification.for_user(user)) == 0
+      assert Enum.empty?(Notification.for_user(user))
     end
   end
 end