added privacy option to push notifications
authorMaksim Pechnikov <parallel588@gmail.com>
Tue, 29 Oct 2019 18:33:17 +0000 (21:33 +0300)
committerMaksim Pechnikov <parallel588@gmail.com>
Thu, 28 Nov 2019 19:13:11 +0000 (22:13 +0300)
lib/pleroma/user/notification_setting.ex
lib/pleroma/web/push/impl.ex
lib/pleroma/workers/web_pusher_worker.ex
test/user/notification_setting_test.exs
test/web/push/impl_test.exs
test/web/twitter_api/util_controller_test.exs

index 64100c0e63ff37c0ff61b46f7da5700d16f75c23..f0899613e6e950186a4f647af4f92f191f12eaf1 100644 (file)
@@ -9,18 +9,12 @@ defmodule Pleroma.User.NotificationSetting do
   @derive Jason.Encoder
   @primary_key false
 
-  @privacy_options %{
-    name_and_message: "name_and_message",
-    name_only: "name_only",
-    no_name_or_message: "no_name_or_message"
-  }
-
   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, :string, default: @privacy_options.name_and_message)
+    field(:privacy_option, :boolean, default: false)
   end
 
   def changeset(schema, params) do
@@ -32,14 +26,11 @@ defmodule Pleroma.User.NotificationSetting do
       :non_followers,
       :privacy_option
     ])
-    |> validate_inclusion(:privacy_option, Map.values(@privacy_options))
   end
 
   defp prepare_attrs(params) do
     Enum.reduce(params, %{}, fn
-      {k, v}, acc
-      when k in ["followers", "follows", "non_follows", "non_followers"] and
-             is_binary(v) ->
+      {k, v}, acc when is_binary(v) ->
         Map.put(acc, k, String.downcase(v))
 
       {k, v}, acc ->
index 3de7af7086ca8f1c7814529defa538d6fd1d0da5..53f93c1edf7b0e3d8e5fd36469c5721ea9973bdb 100644 (file)
@@ -22,8 +22,8 @@ defmodule Pleroma.Web.Push.Impl do
   @spec perform(Notification.t()) :: list(any) | :error
   def perform(
         %{
-          activity: %{data: %{"type" => activity_type}, id: activity_id} = activity,
-          user_id: user_id
+          activity: %{data: %{"type" => activity_type}} = activity,
+          user: %User{id: user_id}
         } = notif
       )
       when activity_type in @types do
@@ -39,18 +39,17 @@ defmodule Pleroma.Web.Push.Impl do
     for subscription <- fetch_subsriptions(user_id),
         get_in(subscription.data, ["alerts", type]) do
       %{
-        title: format_title(notif),
         access_token: subscription.token.token,
-        body: format_body(notif, actor, object),
         notification_id: notif.id,
         notification_type: type,
         icon: avatar_url,
         preferred_locale: "en",
         pleroma: %{
-          activity_id: activity_id,
+          activity_id: notif.activity.id,
           direct_conversation_id: direct_conversation_id
         }
       }
+      |> Map.merge(build_content(notif, actor, object))
       |> Jason.encode!()
       |> push_message(build_sub(subscription), gcm_api_key, subscription)
     end
@@ -100,6 +99,24 @@ defmodule Pleroma.Web.Push.Impl do
     }
   end
 
+  def build_content(
+        %{
+          activity: %{data: %{"directMessage" => true}},
+          user: %{notification_settings: %{privacy_option: true}}
+        },
+        actor,
+        _
+      ) do
+    %{title: "New Direct Message", body: "@#{actor.nickname}"}
+  end
+
+  def build_content(notif, actor, object) do
+    %{
+      title: format_title(notif),
+      body: format_body(notif, actor, object)
+    }
+  end
+
   def format_body(
         %{activity: %{data: %{"type" => "Create"}}},
         actor,
index 61b451e3ed99aecd8fb7bdee08c084b0138bf74f..a978c4013d03837dca7fc24cec2166e1f6ff0ec7 100644 (file)
@@ -13,7 +13,7 @@ defmodule Pleroma.Workers.WebPusherWorker do
     notification =
       Notification
       |> Repo.get(notification_id)
-      |> Repo.preload([:activity])
+      |> Repo.preload([:activity, :user])
 
     Pleroma.Web.Push.Impl.perform(notification)
   end
index d1f766eb346532d8e0aec97ac6a920ac85299bd1..4744d7b4a00dc04a50985861ce790efd2c52c7e1 100644 (file)
@@ -12,29 +12,10 @@ defmodule Pleroma.User.NotificationSettingTest do
       changeset =
         NotificationSetting.changeset(
           %NotificationSetting{},
-          %{"privacy_option" => "name_only"}
+          %{"privacy_option" => true}
         )
 
       assert %Ecto.Changeset{valid?: true} = changeset
     end
-
-    test "returns invalid changeset when privacy option is incorrect" do
-      changeset =
-        NotificationSetting.changeset(
-          %NotificationSetting{},
-          %{"privacy_option" => "full_content"}
-        )
-
-      assert %Ecto.Changeset{valid?: false} = changeset
-
-      assert [
-               privacy_option:
-                 {"is invalid",
-                  [
-                    validation: :inclusion,
-                    enum: ["name_and_message", "name_only", "no_name_or_message"]
-                  ]}
-             ] = changeset.errors
-    end
   end
 end
index 9b554601d9c146079ccb1809a49ba37aef522bbd..acae7a734deca67cb8d147f0c58e8d26feebb349 100644 (file)
@@ -6,6 +6,7 @@ defmodule Pleroma.Web.Push.ImplTest do
   use Pleroma.DataCase
 
   alias Pleroma.Object
+  alias Pleroma.User
   alias Pleroma.Web.CommonAPI
   alias Pleroma.Web.Push.Impl
   alias Pleroma.Web.Push.Subscription
@@ -182,4 +183,50 @@ defmodule Pleroma.Web.Push.ImplTest do
     assert Impl.format_title(%{activity: activity}) ==
              "New Direct Message"
   end
+
+  describe "build_content/3" do
+    test "returns info content for direct message with enabled privacy option" do
+      user = insert(:user, nickname: "Bob")
+      user2 = insert(:user, nickname: "Rob", notification_settings: %{privacy_option: true})
+
+      {:ok, activity} =
+        CommonAPI.post(user, %{
+          "visibility" => "direct",
+          "status" => "<Lorem ipsum dolor sit amet."
+        })
+
+      notif = insert(:notification, user: user2, activity: activity)
+
+      actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
+      object = Object.normalize(activity)
+
+      assert Impl.build_content(notif, actor, object) == %{
+               body: "@Bob",
+               title: "New Direct Message"
+             }
+    end
+
+    test "returns regular content for direct message with disabled privacy option" do
+      user = insert(:user, nickname: "Bob")
+      user2 = insert(:user, nickname: "Rob", notification_settings: %{privacy_option: false})
+
+      {:ok, activity} =
+        CommonAPI.post(user, %{
+          "visibility" => "direct",
+          "status" =>
+            "<span>Lorem ipsum dolor sit amet</span>, consectetur :firefox: adipiscing elit. Fusce sagittis finibus turpis."
+        })
+
+      notif = insert(:notification, user: user2, activity: activity)
+
+      actor = User.get_cached_by_ap_id(notif.activity.data["actor"])
+      object = Object.normalize(activity)
+
+      assert Impl.build_content(notif, actor, object) == %{
+               body:
+                 "@Bob: Lorem ipsum dolor sit amet, consectetur  adipiscing elit. Fusce sagittis fini...",
+               title: "New Direct Message"
+             }
+    end
+  end
 end
index f1557c1931c5000f6e76c37529e92a852db74401..5568c479d9fbddf7623b0151ce10bc6065668889 100644 (file)
@@ -164,7 +164,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
                follows: true,
                non_follows: true,
                non_followers: true,
-               privacy_option: "name_and_message"
+               privacy_option: false
              } == user.notification_settings
     end
 
@@ -173,7 +173,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
 
       conn
       |> assign(:user, user)
-      |> put("/api/pleroma/notification_settings", %{"privacy_option" => "name_only"})
+      |> put("/api/pleroma/notification_settings", %{"privacy_option" => "1"})
       |> json_response(:ok)
 
       user = refresh_record(user)
@@ -183,7 +183,7 @@ defmodule Pleroma.Web.TwitterAPI.UtilControllerTest do
                follows: true,
                non_follows: true,
                non_followers: true,
-               privacy_option: "name_only"
+               privacy_option: true
              } == user.notification_settings
     end
   end