2020-02-18 08:57:00 +01:00
|
|
|
defmodule Mobilizon.Users.Setting do
|
|
|
|
@moduledoc """
|
|
|
|
Module to manage users settings
|
|
|
|
"""
|
|
|
|
|
|
|
|
use Ecto.Schema
|
|
|
|
import Ecto.Changeset
|
2020-06-08 12:28:19 +02:00
|
|
|
alias Mobilizon.Users.{NotificationPendingNotificationDelay, User}
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2020-07-09 17:24:28 +02:00
|
|
|
@type t :: %__MODULE__{
|
|
|
|
timezone: String.t(),
|
|
|
|
notification_on_day: boolean,
|
|
|
|
notification_each_week: boolean,
|
|
|
|
notification_before_event: boolean,
|
|
|
|
notification_pending_participation: NotificationPendingNotificationDelay.t(),
|
2020-11-06 11:34:32 +01:00
|
|
|
notification_pending_membership: NotificationPendingNotificationDelay.t(),
|
2021-05-06 12:27:04 +02:00
|
|
|
group_notifications: NotificationPendingNotificationDelay.t(),
|
|
|
|
last_notification_sent: DateTime.t(),
|
2020-07-09 17:24:28 +02:00
|
|
|
user: User.t()
|
|
|
|
}
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@required_attrs [:user_id]
|
|
|
|
|
|
|
|
@optional_attrs [
|
|
|
|
:timezone,
|
|
|
|
:notification_on_day,
|
|
|
|
:notification_each_week,
|
2020-06-08 12:28:19 +02:00
|
|
|
:notification_before_event,
|
2020-11-06 11:34:32 +01:00
|
|
|
:notification_pending_participation,
|
2021-05-06 12:27:04 +02:00
|
|
|
:notification_pending_membership,
|
|
|
|
:group_notifications,
|
|
|
|
:last_notification_sent
|
2020-02-18 08:57:00 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
@attrs @required_attrs ++ @optional_attrs
|
|
|
|
|
2021-02-12 18:19:49 +01:00
|
|
|
@location_attrs [:name, :range, :geohash]
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
@primary_key {:user_id, :id, autogenerate: false}
|
|
|
|
schema "user_settings" do
|
|
|
|
field(:timezone, :string)
|
|
|
|
field(:notification_on_day, :boolean)
|
|
|
|
field(:notification_each_week, :boolean)
|
|
|
|
field(:notification_before_event, :boolean)
|
2020-06-08 12:28:19 +02:00
|
|
|
|
|
|
|
field(:notification_pending_participation, NotificationPendingNotificationDelay,
|
2020-10-06 15:36:19 +02:00
|
|
|
default: :one_day
|
2020-06-08 12:28:19 +02:00
|
|
|
)
|
|
|
|
|
2020-11-06 11:34:32 +01:00
|
|
|
field(:notification_pending_membership, NotificationPendingNotificationDelay,
|
|
|
|
default: :one_day
|
|
|
|
)
|
|
|
|
|
2021-05-06 12:27:04 +02:00
|
|
|
field(:group_notifications, NotificationPendingNotificationDelay, default: :one_day)
|
|
|
|
field(:last_notification_sent, :utc_datetime)
|
|
|
|
|
2021-02-12 18:19:49 +01:00
|
|
|
embeds_one :location, Location, on_replace: :update, primary_key: false do
|
|
|
|
field(:name, :string)
|
|
|
|
field(:range, :integer)
|
|
|
|
field(:geohash, :string)
|
|
|
|
end
|
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
belongs_to(:user, User, primary_key: true, type: :id, foreign_key: :id, define_field: false)
|
|
|
|
|
|
|
|
timestamps()
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc false
|
|
|
|
def changeset(setting, attrs) do
|
|
|
|
setting
|
|
|
|
|> cast(attrs, @attrs)
|
2021-02-12 18:19:49 +01:00
|
|
|
|> cast_embed(:location, with: &location_changeset/2)
|
2020-02-18 08:57:00 +01:00
|
|
|
|> validate_required(@required_attrs)
|
|
|
|
end
|
2021-02-12 18:19:49 +01:00
|
|
|
|
|
|
|
def location_changeset(schema, params) do
|
|
|
|
schema
|
|
|
|
|> cast(params, @location_attrs)
|
|
|
|
end
|
2020-02-18 08:57:00 +01:00
|
|
|
end
|