Simplify notification filtering settings further
authorMark Felder <feld@FreeBSD.org>
Fri, 26 Jun 2020 16:24:28 +0000 (11:24 -0500)
committerMark Felder <feld@FreeBSD.org>
Fri, 26 Jun 2020 16:24:28 +0000 (11:24 -0500)
CHANGELOG.md
docs/API/pleroma_api.md
lib/pleroma/notification.ex
lib/pleroma/user/notification_setting.ex
lib/pleroma/web/api_spec/schemas/account.ex
priv/repo/migrations/20200528160439_users_update_notification_settings.exs [deleted file]
test/notification_test.exs
test/web/mastodon_api/views/account_view_test.exs
test/web/twitter_api/util_controller_test.exs

index 82915dcfbd5caf2a3b60a76fd2213028e952cf0c..1d835fee2a9c16a4c05ff4541d217b1f6d42a8a3 100644 (file)
@@ -17,9 +17,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
   <summary>API Changes</summary>
 
 - **Breaking:** Emoji API: changed methods and renamed routes.
-- **Breaking:** Notification Settings API for suppressing notification
-  now supports the following controls: `from_followers`, `from_following`,
-  and `from_strangers`.
+- **Breaking:** Notification Settings API for suppressing notifications
+  has been simplified down to `block_from_strangers`.
 </details>
 
 <details>
index 9ad1f5c1bf96c03d4d4113e4cf881f9012325100..6d8a88a447b9e11470357af515e2eee0e70ed2cb 100644 (file)
@@ -287,9 +287,7 @@ See [Admin-API](admin_api.md)
 * Method `PUT`
 * Authentication: required
 * Params:
-    * `from_followers`: BOOLEAN field, receives notifications from followers
-    * `from_following`: BOOLEAN field, receives notifications from people the user follows
-    * `from_strangers`: BOOLEAN field, receives notifications from people without an established relationship
+    * `block_from_strangers`: BOOLEAN field, blocks notifications from accounts you do not follow
     * `privacy_option`: BOOLEAN field. When set to true, it removes the contents of a message from the push notification.
 * Response: JSON. Returns `{"status": "success"}` if the update was successful, otherwise returns `{"error": "error_msg"}`
 
index 9d09cf0821020922a2d3b1f5791962a5486d431d..8a28a182144ad3748fb97b799221830a8408a8c4 100644 (file)
@@ -550,9 +550,7 @@ defmodule Pleroma.Notification do
     [
       :self,
       :invisible,
-      :from_followers,
-      :from_following,
-      :from_strangers,
+      :block_from_strangers,
       :recently_followed
     ]
     |> Enum.find(&skip?(&1, activity, user))
@@ -572,35 +570,15 @@ defmodule Pleroma.Notification do
   end
 
   def skip?(
-        :from_followers,
+        :block_from_strangers,
         %Activity{} = activity,
-        %User{notification_settings: %{from_followers: false}} = user
-      ) do
-    actor = activity.data["actor"]
-    follower = User.get_cached_by_ap_id(actor)
-    User.following?(follower, user)
-  end
-
-  def skip?(
-        :from_strangers,
-        %Activity{} = activity,
-        %User{notification_settings: %{from_strangers: false}} = user
+        %User{notification_settings: %{block_from_strangers: true}} = user
       ) do
     actor = activity.data["actor"]
     follower = User.get_cached_by_ap_id(actor)
     !User.following?(follower, user)
   end
 
-  def skip?(
-        :from_following,
-        %Activity{} = activity,
-        %User{notification_settings: %{from_following: false}} = user
-      ) do
-    actor = activity.data["actor"]
-    followed = User.get_cached_by_ap_id(actor)
-    User.following?(user, followed)
-  end
-
   # To do: consider defining recency in hours and checking FollowingRelationship with a single SQL
   def skip?(:recently_followed, %Activity{data: %{"type" => "Follow"}} = activity, %User{} = user) do
     actor = activity.data["actor"]
index e47ac4cab130048899f997dcc11313532a6410cf..ffe9860de28706513589e0dc574aed58d3c82db8 100644 (file)
@@ -10,18 +10,14 @@ defmodule Pleroma.User.NotificationSetting do
   @primary_key false
 
   embedded_schema do
-    field(:from_followers, :boolean, default: true)
-    field(:from_following, :boolean, default: true)
-    field(:from_strangers, :boolean, default: true)
+    field(:block_from_strangers, :boolean, default: false)
     field(:privacy_option, :boolean, default: false)
   end
 
   def changeset(schema, params) do
     schema
     |> cast(prepare_attrs(params), [
-      :from_followers,
-      :from_following,
-      :from_strangers,
+      :block_from_strangers,
       :privacy_option
     ])
   end
index ed90ef3dbda4c75bd5a86ecd8dade3b6ae0b1958..91bb1ba88ff359456749f95b1cdea69995fa9877 100644 (file)
@@ -57,9 +57,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do
           notification_settings: %Schema{
             type: :object,
             properties: %{
-              from_followers: %Schema{type: :boolean},
-              from_following: %Schema{type: :boolean},
-              from_strangers: %Schema{type: :boolean},
+              block_from_strangers: %Schema{type: :boolean},
               privacy_option: %Schema{type: :boolean}
             }
           },
@@ -122,9 +120,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do
         "unread_conversation_count" => 0,
         "tags" => [],
         "notification_settings" => %{
-          "from_followers" => true,
-          "from_following" => true,
-          "from_strangers" => true,
+          "block_from_strangers" => false,
           "privacy_option" => false
         },
         "relationship" => %{
diff --git a/priv/repo/migrations/20200528160439_users_update_notification_settings.exs b/priv/repo/migrations/20200528160439_users_update_notification_settings.exs
deleted file mode 100644 (file)
index 561f7a2..0000000
+++ /dev/null
@@ -1,43 +0,0 @@
-defmodule Pleroma.Repo.Migrations.UsersUpdateNotificationSettings do
-  use Ecto.Migration
-
-  def up do
-    execute(
-      "UPDATE users SET notification_settings = notification_settings - 'followers' || jsonb_build_object('from_followers', notification_settings->'followers')
-where notification_settings ? 'followers'
-and local"
-    )
-
-    execute(
-      "UPDATE users SET notification_settings = notification_settings - 'follows' || jsonb_build_object('from_following', notification_settings->'follows')
-where notification_settings ? 'follows'
-and local"
-    )
-
-    execute(
-      "UPDATE users SET notification_settings = notification_settings - 'non_followers' || jsonb_build_object('from_strangers', notification_settings->'non_followers')
-where notification_settings ? 'non_followers'
-and local"
-    )
-  end
-
-  def down do
-    execute(
-      "UPDATE users SET notification_settings = notification_settings - 'from_followers' || jsonb_build_object('followers', notification_settings->'from_followers')
-where notification_settings ? 'from_followers'
-and local"
-    )
-
-    execute(
-      "UPDATE users SET notification_settings = notification_settings - 'from_following' || jsonb_build_object('follows', notification_settings->'from_following')
-where notification_settings ? 'from_following'
-and local"
-    )
-
-    execute(
-      "UPDATE users SET notification_settings = notification_settings - 'from_strangers' || jsonb_build_object('non_follows', notification_settings->'from_strangers')
-where notification_settings ? 'from_strangers'
-and local"
-    )
-  end
-end
index d7df9c36c5ab10ec66ecf7f7199923ada5305373..d8cb9360ae662ecdd152c053425fe2cef79f49e9 100644 (file)
@@ -236,44 +236,18 @@ defmodule Pleroma.NotificationTest do
       assert Notification.create_notification(activity, muter)
     end
 
-    test "it disables notifications from followers" do
-      follower = insert(:user)
-
-      followed =
-        insert(:user,
-          notification_settings: %Pleroma.User.NotificationSetting{from_followers: false}
-        )
-
-      User.follow(follower, followed)
-      {:ok, activity} = CommonAPI.post(follower, %{status: "hey @#{followed.nickname}"})
-      refute Notification.create_notification(activity, followed)
-    end
-
     test "it disables notifications from strangers" do
       follower = insert(:user)
 
       followed =
         insert(:user,
-          notification_settings: %Pleroma.User.NotificationSetting{from_strangers: false}
+          notification_settings: %Pleroma.User.NotificationSetting{block_from_strangers: true}
         )
 
       {:ok, activity} = CommonAPI.post(follower, %{status: "hey @#{followed.nickname}"})
       refute Notification.create_notification(activity, followed)
     end
 
-    test "it disables notifications from people the user follows" do
-      follower =
-        insert(:user,
-          notification_settings: %Pleroma.User.NotificationSetting{from_following: false}
-        )
-
-      followed = insert(:user)
-      User.follow(follower, followed)
-      follower = Repo.get(User, follower.id)
-      {:ok, activity} = CommonAPI.post(followed, %{status: "hey @#{follower.nickname}"})
-      refute 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_cached_by_ap_id(activity.data["actor"])
index 57283019458dca5e72f21b00540c5129ebe2ece8..b6d820b3fdf97786dee87101a0e4cba36cef500c 100644 (file)
@@ -96,9 +96,7 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
     user = insert(:user)
 
     notification_settings = %{
-      from_followers: true,
-      from_following: true,
-      from_strangers: true,
+      block_from_strangers: false,
       privacy_option: false
     }
 
index 1133107f49cd63208b4572f4504aa49fb116ad7b..da3f6fa61ef633c36e0ae72ec457f0d88aa92f6a 100644 (file)
@@ -191,7 +191,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
     test "it updates notification settings", %{user: user, conn: conn} do
       conn
       |> put("/api/pleroma/notification_settings", %{
-        "from_followers" => false,
+        "block_from_strangers" => true,
         "bar" => 1
       })
       |> json_response(:ok)
@@ -199,9 +199,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
       user = refresh_record(user)
 
       assert %Pleroma.User.NotificationSetting{
-               from_followers: false,
-               from_following: true,
-               from_strangers: true,
+               block_from_strangers: true,
                privacy_option: false
              } == user.notification_settings
     end
@@ -214,9 +212,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
       user = refresh_record(user)
 
       assert %Pleroma.User.NotificationSetting{
-               from_followers: true,
-               from_following: true,
-               from_strangers: true,
+               block_from_strangers: false,
                privacy_option: true
              } == user.notification_settings
     end