Notification controls
authoreugenijm <eugenijm@protonmail.com>
Wed, 27 Mar 2019 19:09:39 +0000 (22:09 +0300)
committereugenijm <eugenijm@protonmail.com>
Thu, 28 Mar 2019 04:52:07 +0000 (07:52 +0300)
Allow users to configure whether they want to receive notifications from people they follow / who follow them, people from remote / local instances

lib/pleroma/notification.ex
lib/pleroma/user/info.ex
test/notification_test.exs

index cac10f24af4ef984f46a8710227a32ff304284ef..caa6b755e40027a0737d10e146babc0d6a7738c2 100644 (file)
@@ -122,13 +122,7 @@ 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
-             CommonAPI.thread_muted?(user, activity) or 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
+    unless skip?(activity, user) do
       notification = %Notification{user_id: user.id, activity: activity}
       {:ok, notification} = Repo.insert(notification)
       Pleroma.Web.Streamer.stream("user", notification)
@@ -154,4 +148,61 @@ defmodule Pleroma.Notification do
   end
 
   def get_notified_from_activity(_, _local_only), do: []
+
+  def skip?(activity, user) do
+    [:self, :blocked, :local, :muted, :followers, :follows, :recently_followed]
+    |> Enum.any?(&skip?(&1, activity, user))
+  end
+
+  def skip?(:self, activity, user) do
+    activity.data["actor"] == user.ap_id
+  end
+
+  def skip?(:blocked, activity, user) do
+    actor = activity.data["actor"]
+    User.blocks?(user, %{ap_id: actor})
+  end
+
+  def skip?(:local, %{local: true}, user) do
+    user.info.notification_settings["local"] == false
+  end
+
+  def skip?(:local, %{local: false}, user) do
+    user.info.notification_settings["remote"] == false
+  end
+
+  def skip?(:muted, activity, user) do
+    actor = activity.data["actor"]
+
+    User.mutes?(user, %{ap_id: actor}) or
+      CommonAPI.thread_muted?(user, activity)
+  end
+
+  def skip?(
+        :followers,
+        activity,
+        %{info: %{notification_settings: %{"followers" => false}}} = user
+      ) do
+    actor = activity.data["actor"]
+    follower = User.get_cached_by_ap_id(actor)
+    User.following?(follower, user)
+  end
+
+  def skip?(:follows, activity, %{info: %{notification_settings: %{"follows" => false}}} = user) do
+    actor = activity.data["actor"]
+    followed = User.get_by_ap_id(actor)
+    User.following?(user, followed)
+  end
+
+  def skip?(:recently_followed, activity, user) do
+    actor = activity.data["actor"]
+
+    Notification.for_user(user)
+    |> Enum.any?(fn
+      %{activity: %{data: %{"type" => "Follow", "actor" => ^actor}}} -> true
+      _ -> false
+    end)
+  end
+
+  def skip?(_, _, _), do: false
 end
index 740a46727b50ffe1e1ff9a3ed443c5e6205c2af0..c36efa126e7fd8564f635e8944b069787e362e44 100644 (file)
@@ -40,6 +40,10 @@ defmodule Pleroma.User.Info do
     field(:pinned_activities, {:array, :string}, default: [])
     field(:flavour, :string, default: nil)
 
+    field(:notification_settings, :map,
+      default: %{"remote" => true, "local" => true, "followers" => true, "follows" => true}
+    )
+
     # Found in the wild
     # ap_id -> Where is this used?
     # bio -> Where is this used?
index 12b4292aa8c4259aa977303b3a0695abb9dbcf5c..89d06b3a23c99649621d8646b2d19187eabcc719 100644 (file)
@@ -41,6 +41,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"])