Merge branch 'develop' into refactor/notification_settings
authorMark Felder <feld@FreeBSD.org>
Mon, 13 Jul 2020 18:32:21 +0000 (13:32 -0500)
committerMark Felder <feld@FreeBSD.org>
Mon, 13 Jul 2020 18:32:21 +0000 (13:32 -0500)
13 files changed:
CHANGELOG.md
docs/API/pleroma_api.md
lib/mix/tasks/pleroma/notification_settings.ex
lib/pleroma/notification.ex
lib/pleroma/user/notification_setting.ex
lib/pleroma/web/api_spec/schemas/account.ex
lib/pleroma/web/push/impl.ex
priv/repo/migrations/20200626163359_rename_notification_privacy_option.exs [new file with mode: 0644]
test/notification_test.exs
test/user/notification_setting_test.exs
test/web/mastodon_api/views/account_view_test.exs
test/web/push/impl_test.exs
test/web/twitter_api/util_controller_test.exs

index 5fed80a99e08775e4fdecbbfca297a964b311aea..4af195f7dd2d1f73f9fe0181a19447a96817fbbf 100644 (file)
@@ -24,6 +24,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
 - Mastodon API: Added `pleroma.metadata.fields_limits` to /api/v1/instance
 - Mastodon API: On deletion, returns the original post text.
 - Mastodon API: Add `pleroma.unread_count` to the Marker entity.
+- **Breaking:** Notification Settings API for suppressing notifications
+  has been simplified down to `block_from_strangers`.
+- **Breaking:** Notification Settings API option for hiding push notification
+  contents has been renamed to `hide_notification_contents`
 </details>
 
 <details>
index b7eee51928f1112e69e30461ad0e094dd228c966..5bd38ad364d9db7be00fbb0750a9de3ddbe1e1e8 100644 (file)
@@ -287,11 +287,8 @@ See [Admin-API](admin_api.md)
 * Method `PUT`
 * Authentication: required
 * Params:
-    * `followers`: BOOLEAN field, receives notifications from followers
-    * `follows`: BOOLEAN field, receives notifications from people the user follows
-    * `remote`: BOOLEAN field, receives notifications from people on remote instances
-    * `local`: BOOLEAN field, receives notifications from people on the local instance
-    * `privacy_option`: BOOLEAN field. When set to true, it removes the contents of a message from the push notification.
+    * `block_from_strangers`: BOOLEAN field, blocks notifications from accounts you do not follow
+    * `hide_notification_contents`: 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"}`
 
 ## `/api/pleroma/healthcheck`
index 7d65f058763365e0a724b6ee1b57bb1ac1674631..00f5ba7bfe61ce5aba33238deb81fce5e1aab65a 100644 (file)
@@ -3,8 +3,8 @@ defmodule Mix.Tasks.Pleroma.NotificationSettings do
   @moduledoc """
   Example:
 
-  > mix pleroma.notification_settings --privacy-option=false --nickname-users="parallel588"  # set false only for parallel588 user
-  > mix pleroma.notification_settings --privacy-option=true # set true for all users
+  > mix pleroma.notification_settings --hide-notification-contents=false --nickname-users="parallel588"  # set false only for parallel588 user
+  > mix pleroma.notification_settings --hide-notification-contents=true # set true for all users
 
   """
 
@@ -19,16 +19,16 @@ defmodule Mix.Tasks.Pleroma.NotificationSettings do
       OptionParser.parse(
         args,
         strict: [
-          privacy_option: :boolean,
+          hide_notification_contents: :boolean,
           email_users: :string,
           nickname_users: :string
         ]
       )
 
-    privacy_option = Keyword.get(options, :privacy_option)
+    hide_notification_contents = Keyword.get(options, :hide_notification_contents)
 
-    if not is_nil(privacy_option) do
-      privacy_option
+    if not is_nil(hide_notification_contents) do
+      hide_notification_contents
       |> build_query(options)
       |> Pleroma.Repo.update_all([])
     end
@@ -36,15 +36,15 @@ defmodule Mix.Tasks.Pleroma.NotificationSettings do
     shell_info("Done")
   end
 
-  defp build_query(privacy_option, options) do
+  defp build_query(hide_notification_contents, options) do
     query =
       from(u in Pleroma.User,
         update: [
           set: [
             notification_settings:
               fragment(
-                "jsonb_set(notification_settings, '{privacy_option}', ?)",
-                ^privacy_option
+                "jsonb_set(notification_settings, '{hide_notification_contents}', ?)",
+                ^hide_notification_contents
               )
           ]
         ]
index 32bcfcaba34d9c830211c3f8180481feff193b5d..0b171563b291873000182e7345711fb2939fc1d5 100644 (file)
@@ -571,10 +571,7 @@ defmodule Pleroma.Notification do
     [
       :self,
       :invisible,
-      :followers,
-      :follows,
-      :non_followers,
-      :non_follows,
+      :block_from_strangers,
       :recently_followed,
       :filtered
     ]
@@ -595,45 +592,15 @@ defmodule Pleroma.Notification do
   end
 
   def skip?(
-        :followers,
+        :block_from_strangers,
         %Activity{} = activity,
-        %User{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?(
-        :non_followers,
-        %Activity{} = activity,
-        %User{notification_settings: %{non_followers: 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?(
-        :follows,
-        %Activity{} = activity,
-        %User{notification_settings: %{follows: false}} = user
-      ) do
-    actor = activity.data["actor"]
-    followed = User.get_cached_by_ap_id(actor)
-    User.following?(user, followed)
-  end
-
-  def skip?(
-        :non_follows,
-        %Activity{} = activity,
-        %User{notification_settings: %{non_follows: 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 4bd55e139d2c8c916f3ca5020e06dfaa5033aa3b..7d9e8a000f095c06036b93db168e065d6b96379c 100644 (file)
@@ -10,21 +10,15 @@ defmodule Pleroma.User.NotificationSetting do
   @primary_key false
 
   embedded_schema do
-    field(:followers, :boolean, default: true)
-    field(:follows, :boolean, default: true)
-    field(:non_follows, :boolean, default: true)
-    field(:non_followers, :boolean, default: true)
-    field(:privacy_option, :boolean, default: false)
+    field(:block_from_strangers, :boolean, default: false)
+    field(:hide_notification_contents, :boolean, default: false)
   end
 
   def changeset(schema, params) do
     schema
     |> cast(prepare_attrs(params), [
-      :followers,
-      :follows,
-      :non_follows,
-      :non_followers,
-      :privacy_option
+      :block_from_strangers,
+      :hide_notification_contents
     ])
   end
 
index cf148bc9d944dea753aed36edd64dd3bf25bd39c..ca79f0747861b93bb63e5d40022878369ffad247 100644 (file)
@@ -90,11 +90,8 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do
           notification_settings: %Schema{
             type: :object,
             properties: %{
-              followers: %Schema{type: :boolean},
-              follows: %Schema{type: :boolean},
-              non_followers: %Schema{type: :boolean},
-              non_follows: %Schema{type: :boolean},
-              privacy_option: %Schema{type: :boolean}
+              block_from_strangers: %Schema{type: :boolean},
+              hide_notification_contents: %Schema{type: :boolean}
             }
           },
           relationship: AccountRelationship,
@@ -182,11 +179,8 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Account do
         "unread_conversation_count" => 0,
         "tags" => [],
         "notification_settings" => %{
-          "followers" => true,
-          "follows" => true,
-          "non_followers" => true,
-          "non_follows" => true,
-          "privacy_option" => false
+          "block_from_strangers" => false,
+          "hide_notification_contents" => false
         },
         "relationship" => %{
           "blocked_by" => false,
index cdb827e7664820f7d08088ad489a7e7f21a033ef..16368485e57d1028e241c8302c103d942cc99123 100644 (file)
@@ -104,7 +104,7 @@ defmodule Pleroma.Web.Push.Impl do
 
   def build_content(
         %{
-          user: %{notification_settings: %{privacy_option: true}}
+          user: %{notification_settings: %{hide_notification_contents: true}}
         } = notification,
         _actor,
         _object,
diff --git a/priv/repo/migrations/20200626163359_rename_notification_privacy_option.exs b/priv/repo/migrations/20200626163359_rename_notification_privacy_option.exs
new file mode 100644 (file)
index 0000000..06d7f72
--- /dev/null
@@ -0,0 +1,19 @@
+defmodule Pleroma.Repo.Migrations.RenameNotificationPrivacyOption do
+  use Ecto.Migration
+
+  def up do
+    execute(
+      "UPDATE users SET notification_settings = notification_settings - 'privacy_option' || jsonb_build_object('hide_notification_contents', notification_settings->'privacy_option')
+where notification_settings ? 'privacy_option'
+and local"
+    )
+  end
+
+  def down do
+    execute(
+      "UPDATE users SET notification_settings = notification_settings - 'hide_notification_contents' || jsonb_build_object('privacy_option', notification_settings->'hide_notification_contents')
+where notification_settings ? 'hide_notification_contents'
+and local"
+    )
+  end
+end
index 13e82ab2a7bca7a7f290b067f7ec580c4bdd149b..8243cfd347309ec54b5ff69faf1a4af7a6e7649e 100644 (file)
@@ -246,49 +246,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{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 non-followers" do
+    test "it disables notifications from strangers" do
       follower = insert(:user)
 
       followed =
         insert(:user,
-          notification_settings: %Pleroma.User.NotificationSetting{non_followers: 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{follows: 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 disables notifications from people the user does not follow" do
-      follower =
-        insert(:user, notification_settings: %Pleroma.User.NotificationSetting{non_follows: false})
-
-      followed = insert(:user)
-      {: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 95bca22c41229042337c141e172475429a4ebf98..308da216a3a0fd97f1188751658df31bc2dc34af 100644 (file)
@@ -8,11 +8,11 @@ defmodule Pleroma.User.NotificationSettingTest do
   alias Pleroma.User.NotificationSetting
 
   describe "changeset/2" do
-    test "sets valid privacy option" do
+    test "sets option to hide notification contents" do
       changeset =
         NotificationSetting.changeset(
           %NotificationSetting{},
-          %{"privacy_option" => true}
+          %{"hide_notification_contents" => true}
         )
 
       assert %Ecto.Changeset{valid?: true} = changeset
index 17f035add993fa88603a8846cacf621b4aa62de2..a83bf90a31cdc57ee0612d515dfe07edeb86a57e 100644 (file)
@@ -119,11 +119,8 @@ defmodule Pleroma.Web.MastodonAPI.AccountViewTest do
     user = insert(:user)
 
     notification_settings = %{
-      followers: true,
-      follows: true,
-      non_followers: true,
-      non_follows: true,
-      privacy_option: false
+      block_from_strangers: false,
+      hide_notification_contents: false
     }
 
     privacy = user.default_scope
index b48952b29026ffde4f12e2017736d4875ea69063..aeb5c1fbd3216dbbb243f0cd6cbda62a158eef1d 100644 (file)
@@ -238,9 +238,11 @@ defmodule Pleroma.Web.Push.ImplTest do
              }
     end
 
-    test "hides details for notifications when privacy option enabled" do
+    test "hides contents of notifications when option enabled" do
       user = insert(:user, nickname: "Bob")
-      user2 = insert(:user, nickname: "Rob", notification_settings: %{privacy_option: true})
+
+      user2 =
+        insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: true})
 
       {:ok, activity} =
         CommonAPI.post(user, %{
@@ -284,9 +286,11 @@ defmodule Pleroma.Web.Push.ImplTest do
              }
     end
 
-    test "returns regular content for notifications with privacy option disabled" do
+    test "returns regular content when hiding contents option disabled" do
       user = insert(:user, nickname: "Bob")
-      user2 = insert(:user, nickname: "Rob", notification_settings: %{privacy_option: false})
+
+      user2 =
+        insert(:user, nickname: "Rob", notification_settings: %{hide_notification_contents: false})
 
       {:ok, activity} =
         CommonAPI.post(user, %{
index 76e9369f79ef15f756baf71c34fb33ea18a06327..109c1e63766f8ae68837629f45e9832c5dcc3941 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", %{
-        "followers" => false,
+        "block_from_strangers" => true,
         "bar" => 1
       })
       |> json_response(:ok)
@@ -199,27 +199,21 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
       user = refresh_record(user)
 
       assert %Pleroma.User.NotificationSetting{
-               followers: false,
-               follows: true,
-               non_follows: true,
-               non_followers: true,
-               privacy_option: false
+               block_from_strangers: true,
+               hide_notification_contents: false
              } == user.notification_settings
     end
 
-    test "it updates notification privacy option", %{user: user, conn: conn} do
+    test "it updates notification settings to enable hiding contents", %{user: user, conn: conn} do
       conn
-      |> put("/api/pleroma/notification_settings", %{"privacy_option" => "1"})
+      |> put("/api/pleroma/notification_settings", %{"hide_notification_contents" => "1"})
       |> json_response(:ok)
 
       user = refresh_record(user)
 
       assert %Pleroma.User.NotificationSetting{
-               followers: true,
-               follows: true,
-               non_follows: true,
-               non_followers: true,
-               privacy_option: true
+               block_from_strangers: false,
+               hide_notification_contents: true
              } == user.notification_settings
     end
   end