Add timezone handling

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-10-10 16:25:50 +02:00
parent eba3c70c9b
commit d58ca5743d
49 changed files with 1218 additions and 429 deletions

View File

@@ -12,17 +12,18 @@ defmodule Mobilizon.Addresses.Address do
alias Mobilizon.Web.Endpoint
@type t :: %__MODULE__{
country: String.t(),
locality: String.t(),
region: String.t(),
description: String.t(),
geom: Geo.PostGIS.Geometry.t(),
postal_code: String.t(),
street: String.t(),
type: String.t(),
country: String.t() | nil,
locality: String.t() | nil,
region: String.t() | nil,
description: String.t() | nil,
geom: Geo.PostGIS.Geometry.t() | nil,
postal_code: String.t() | nil,
street: String.t() | nil,
type: String.t() | nil,
url: String.t(),
origin_id: String.t(),
events: [Event.t()]
origin_id: String.t() | nil,
events: [Event.t()],
timezone: String.t() | nil
}
@required_attrs [:url]
@@ -35,7 +36,8 @@ defmodule Mobilizon.Addresses.Address do
:postal_code,
:street,
:origin_id,
:type
:type,
:timezone
]
@attrs @required_attrs ++ @optional_attrs
@@ -50,6 +52,7 @@ defmodule Mobilizon.Addresses.Address do
field(:type, :string)
field(:url, :string)
field(:origin_id, :string)
field(:timezone, :string)
has_many(:events, Event, foreign_key: :physical_address_id)
@@ -61,6 +64,7 @@ defmodule Mobilizon.Addresses.Address do
def changeset(%__MODULE__{} = address, attrs) do
address
|> cast(attrs, @attrs)
|> maybe_set_timezone()
|> set_url()
|> validate_required(@required_attrs)
|> unique_constraint(:url, name: :addresses_url_index)
@@ -90,4 +94,29 @@ defmodule Mobilizon.Addresses.Address do
"#{address.street} #{address.postal_code} #{address.locality} #{address.region} #{address.country}"
)
end
@spec maybe_set_timezone(Ecto.Changeset.t()) :: Ecto.Changeset.t()
defp maybe_set_timezone(%Ecto.Changeset{} = changeset) do
case get_change(changeset, :geom) do
nil ->
changeset
geom ->
case get_field(changeset, :timezone) do
# Only update the timezone if the geom has change and we don't already have a set timezone
nil -> put_change(changeset, :timezone, timezone(geom))
_ -> changeset
end
end
end
@spec timezone(Geo.PostGIS.Geometry.t() | nil) :: String.t() | nil
defp timezone(nil), do: nil
defp timezone(geom) do
case TzWorld.timezone_at(geom) do
{:ok, tz} -> tz
{:error, _err} -> nil
end
end
end

View File

@@ -27,6 +27,7 @@ defmodule Mobilizon.Events.EventOptions do
participation_condition: [EventParticipationCondition.t()],
show_start_time: boolean,
show_end_time: boolean,
timezone: String.t() | nil,
hide_organizer_when_group_event: boolean
}
@@ -41,6 +42,7 @@ defmodule Mobilizon.Events.EventOptions do
:show_participation_price,
:show_start_time,
:show_end_time,
:timezone,
:hide_organizer_when_group_event
]
@@ -57,6 +59,7 @@ defmodule Mobilizon.Events.EventOptions do
field(:show_participation_price, :boolean)
field(:show_start_time, :boolean, default: true)
field(:show_end_time, :boolean, default: true)
field(:timezone, :string)
field(:hide_organizer_when_group_event, :boolean, default: false)
embeds_many(:offers, EventOffer)

View File

@@ -401,7 +401,8 @@ defmodule Mobilizon.Events do
|> Page.build_page(page, limit)
end
@spec list_organized_events_for_actor(Actor.t(), integer | nil, integer | nil) :: Page.t()
@spec list_organized_events_for_actor(Actor.t(), integer | nil, integer | nil) ::
Page.t(Event.t())
def list_organized_events_for_actor(%Actor{id: actor_id}, page \\ nil, limit \\ nil) do
actor_id
|> event_for_actor_query(desc: :begins_on)
@@ -409,13 +410,15 @@ defmodule Mobilizon.Events do
|> Page.build_page(page, limit)
end
@spec list_simple_organized_events_for_group(Actor.t(), integer | nil, integer | nil) ::
Page.t(Event.t())
def list_simple_organized_events_for_group(%Actor{} = actor, page \\ nil, limit \\ nil) do
list_organized_events_for_group(actor, :all, nil, nil, page, limit)
end
@spec list_organized_events_for_group(
Actor.t(),
EventVisibility.t(),
EventVisibility.t() | :all,
DateTime.t() | nil,
DateTime.t() | nil,
integer | nil,
@@ -885,7 +888,9 @@ defmodule Mobilizon.Events do
@doc """
Creates a participant.
"""
@spec create_participant(map) :: {:ok, Participant.t()} | {:error, Changeset.t()}
@spec create_participant(map) ::
{:ok, Participant.t()}
| {:error, :participant | :update_event_participation_stats, Changeset.t(), map()}
def create_participant(attrs \\ %{}, update_event_participation_stats \\ true) do
with {:ok, %{participant: %Participant{} = participant}} <-
Multi.new()
@@ -912,7 +917,8 @@ defmodule Mobilizon.Events do
Updates a participant.
"""
@spec update_participant(Participant.t(), map) ::
{:ok, Participant.t()} | {:error, Changeset.t()}
{:ok, Participant.t()}
| {:error, :participant | :update_event_participation_stats, Changeset.t(), map()}
def update_participant(%Participant{role: old_role} = participant, attrs) do
with {:ok, %{participant: %Participant{} = participant}} <-
Multi.new()
@@ -1625,11 +1631,12 @@ defmodule Mobilizon.Events do
from(p in query, where: p.role == ^role)
end
@spec event_filter_visibility(Ecto.Queryable.t(), :public | :all) ::
Ecto.Queryable.t() | Ecto.Query.t()
defp event_filter_visibility(query, :all), do: query
defp event_filter_visibility(query, :public) do
query
|> where(visibility: ^:public, draft: false)
where(query, visibility: ^:public, draft: false)
end
defp event_filter_begins_on(query, nil, nil),