Add User.Info.email_notifications
authorRoman Chvanikov <chvanikoff@gmail.com>
Sat, 13 Apr 2019 10:40:42 +0000 (17:40 +0700)
committerRoman Chvanikov <chvanikoff@gmail.com>
Sat, 13 Apr 2019 10:40:42 +0000 (17:40 +0700)
lib/pleroma/user/info.ex
priv/repo/migrations/20190412052952_add_user_info_fields.exs [new file with mode: 0644]
test/user_info_test.exs [new file with mode: 0644]

index 5afa7988cee7287750e2f6e1a0169732c12cd486..194dd55814eb6ad715be41c23614138cd797d3c7 100644 (file)
@@ -8,6 +8,8 @@ defmodule Pleroma.User.Info do
 
   alias Pleroma.User.Info
 
+  @type t :: %__MODULE__{}
+
   embedded_schema do
     field(:banner, :map, default: %{})
     field(:background, :map, default: %{})
@@ -40,6 +42,7 @@ defmodule Pleroma.User.Info do
     field(:hide_follows, :boolean, default: false)
     field(:pinned_activities, {:array, :string}, default: [])
     field(:flavour, :string, default: nil)
+    field(:email_notifications, :map, default: %{"digest" => true})
 
     field(:notification_settings, :map,
       default: %{"remote" => true, "local" => true, "followers" => true, "follows" => true}
@@ -75,6 +78,30 @@ defmodule Pleroma.User.Info do
     |> validate_required([:notification_settings])
   end
 
+  @doc """
+  Update email notifications in the given User.Info struct.
+
+  Examples:
+
+      iex> update_email_notifications(%Pleroma.User.Info{email_notifications: %{"digest" => false}}, %{"digest" => true})
+      %Pleroma.User.Info{email_notifications: %{"digest" => true}}
+
+  """
+  @spec update_email_notifications(t(), map()) :: Ecto.Changeset.t()
+  def update_email_notifications(info, settings) do
+    email_notifications =
+      info.email_notifications
+      |> Map.merge(settings)
+      |> Map.take(["digest"])
+
+    params = %{email_notifications: email_notifications}
+    fields = [:email_notifications]
+
+    info
+    |> cast(params, fields)
+    |> validate_required(fields)
+  end
+
   def add_to_note_count(info, number) do
     set_note_count(info, info.note_count + number)
   end
diff --git a/priv/repo/migrations/20190412052952_add_user_info_fields.exs b/priv/repo/migrations/20190412052952_add_user_info_fields.exs
new file mode 100644 (file)
index 0000000..203d0fc
--- /dev/null
@@ -0,0 +1,20 @@
+defmodule Pleroma.Repo.Migrations.AddEmailNotificationsToUserInfo do
+  use Ecto.Migration
+
+  def up do
+    execute("
+    UPDATE users
+    SET info = info || '{
+      \"email_notifications\": {
+        \"digest\": true
+      }
+    }'")
+  end
+
+  def down do
+    execute("
+      UPDATE users
+      SET info = info - 'email_notifications'
+    ")
+  end
+end
diff --git a/test/user_info_test.exs b/test/user_info_test.exs
new file mode 100644 (file)
index 0000000..2d79559
--- /dev/null
@@ -0,0 +1,24 @@
+defmodule Pleroma.UserInfoTest do
+  alias Pleroma.Repo
+  alias Pleroma.User.Info
+
+  use Pleroma.DataCase
+
+  import Pleroma.Factory
+
+  describe "update_email_notifications/2" do
+    setup do
+      user = insert(:user, %{info: %{email_notifications: %{"digest" => true}}})
+
+      {:ok, user: user}
+    end
+
+    test "Notifications are updated", %{user: user} do
+      true = user.info.email_notifications["digest"]
+      changeset = Info.update_email_notifications(user.info, %{"digest" => false})
+      assert changeset.valid?
+      {:ok, result} = Ecto.Changeset.apply_action(changeset, :insert)
+      assert result.email_notifications["digest"] == false
+    end
+  end
+end