Add User.Info.email_notifications

This commit is contained in:
Roman Chvanikov 2019-04-13 17:40:42 +07:00
parent 30a4318ef1
commit 371a4aed2c
3 changed files with 71 additions and 0 deletions

View 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

View File

@ -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

24
test/user_info_test.exs Normal file
View File

@ -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