@@ -66,12 +66,15 @@ defmodule Mobilizon.Service.Geospatial.Addok do
|
||||
defp process_data(features) do
|
||||
features
|
||||
|> Enum.map(fn %{"geometry" => geometry, "properties" => properties} ->
|
||||
coordinates = geometry |> Map.get("coordinates") |> Provider.coordinates()
|
||||
|
||||
%Address{
|
||||
country: Map.get(properties, "country", default_country()),
|
||||
locality: Map.get(properties, "city"),
|
||||
region: Map.get(properties, "context"),
|
||||
description: Map.get(properties, "name") || street_address(properties),
|
||||
geom: geometry |> Map.get("coordinates") |> Provider.coordinates(),
|
||||
geom: coordinates,
|
||||
timezone: Provider.timezone(coordinates),
|
||||
postal_code: Map.get(properties, "postcode"),
|
||||
street: properties |> street_address()
|
||||
}
|
||||
|
||||
@@ -124,12 +124,15 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
|
||||
description
|
||||
end
|
||||
|
||||
coordinates = Provider.coordinates([lon, lat])
|
||||
|
||||
%Address{
|
||||
country: Map.get(components, "country"),
|
||||
locality: Map.get(components, "locality"),
|
||||
region: Map.get(components, "administrative_area_level_1"),
|
||||
description: description,
|
||||
geom: [lon, lat] |> Provider.coordinates(),
|
||||
geom: coordinates,
|
||||
timezone: Provider.timezone(coordinates),
|
||||
postal_code: Map.get(components, "postal_code"),
|
||||
street: street_address(components),
|
||||
origin_id: "gm:" <> to_string(place_id)
|
||||
|
||||
@@ -98,12 +98,15 @@ defmodule Mobilizon.Service.Geospatial.MapQuest do
|
||||
end
|
||||
|
||||
defp produce_address(address, lat, lng) do
|
||||
coordinates = Provider.coordinates([lng, lat])
|
||||
|
||||
%Address{
|
||||
country: Map.get(address, "adminArea1"),
|
||||
locality: Map.get(address, "adminArea5"),
|
||||
region: Map.get(address, "adminArea3"),
|
||||
description: Map.get(address, "street"),
|
||||
geom: [lng, lat] |> Provider.coordinates(),
|
||||
geom: coordinates,
|
||||
timezone: Provider.timezone(coordinates),
|
||||
postal_code: Map.get(address, "postalCode"),
|
||||
street: Map.get(address, "street")
|
||||
}
|
||||
|
||||
@@ -75,7 +75,8 @@ defmodule Mobilizon.Service.Geospatial.Mimirsbrunn do
|
||||
"properties" => %{"geocoding" => geocoding}
|
||||
} ->
|
||||
address = process_address(geocoding)
|
||||
%Address{address | geom: Provider.coordinates(coordinates)}
|
||||
coordinates = Provider.coordinates(coordinates)
|
||||
%Address{address | geom: coordinates, timezone: Provider.timezone(coordinates)}
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
@@ -75,7 +75,8 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
|
||||
"properties" => %{"geocoding" => geocoding}
|
||||
} ->
|
||||
address = process_address(geocoding)
|
||||
%Address{address | geom: Provider.coordinates(coordinates)}
|
||||
coordinates = Provider.coordinates(coordinates)
|
||||
%Address{address | geom: coordinates, timezone: Provider.timezone(coordinates)}
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
@@ -76,7 +76,8 @@ defmodule Mobilizon.Service.Geospatial.Pelias do
|
||||
"properties" => properties
|
||||
} ->
|
||||
address = process_address(properties)
|
||||
%Address{address | geom: Provider.coordinates(coordinates)}
|
||||
coordinates = Provider.coordinates(coordinates)
|
||||
%Address{address | geom: coordinates, timezone: Provider.timezone(coordinates)}
|
||||
end)
|
||||
end
|
||||
|
||||
|
||||
@@ -69,12 +69,15 @@ defmodule Mobilizon.Service.Geospatial.Photon do
|
||||
defp process_data(features) do
|
||||
features
|
||||
|> Enum.map(fn %{"geometry" => geometry, "properties" => properties} ->
|
||||
coordinates = geometry |> Map.get("coordinates") |> Provider.coordinates()
|
||||
|
||||
%Address{
|
||||
country: Map.get(properties, "country"),
|
||||
locality: Map.get(properties, "city"),
|
||||
region: Map.get(properties, "state"),
|
||||
description: Map.get(properties, "name") || street_address(properties),
|
||||
geom: geometry |> Map.get("coordinates") |> Provider.coordinates(),
|
||||
geom: coordinates,
|
||||
timezone: Provider.timezone(coordinates),
|
||||
postal_code: Map.get(properties, "postcode"),
|
||||
street: properties |> street_address()
|
||||
}
|
||||
|
||||
@@ -79,6 +79,19 @@ defmodule Mobilizon.Service.Geospatial.Provider do
|
||||
|
||||
def coordinates(_), do: nil
|
||||
|
||||
@doc """
|
||||
Returns the timezone for a Geo.Point
|
||||
"""
|
||||
@spec timezone(nil | Geo.Point.t()) :: nil | String.t()
|
||||
def timezone(nil), do: nil
|
||||
|
||||
def timezone(%Geo.Point{} = point) do
|
||||
case TzWorld.timezone_at(point) do
|
||||
{:ok, tz} -> tz
|
||||
{:error, _err} -> nil
|
||||
end
|
||||
end
|
||||
|
||||
@spec endpoint(atom()) :: String.t()
|
||||
def endpoint(provider) do
|
||||
Application.get_env(:mobilizon, provider) |> get_in([:endpoint])
|
||||
|
||||
@@ -2,7 +2,7 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
|
||||
alias Phoenix.HTML
|
||||
alias Phoenix.HTML.Tag
|
||||
alias Mobilizon.Addresses.Address
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Events.{Event, EventOptions}
|
||||
alias Mobilizon.Web.JsonLD.ObjectView
|
||||
|
||||
import Mobilizon.Service.Metadata.Utils,
|
||||
@@ -53,20 +53,52 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
|
||||
%Event{
|
||||
description: description,
|
||||
begins_on: begins_on,
|
||||
physical_address: %Address{} = address
|
||||
physical_address: address,
|
||||
options: %EventOptions{timezone: timezone},
|
||||
language: language
|
||||
},
|
||||
locale
|
||||
) do
|
||||
"#{datetime_to_string(begins_on, locale)} - #{render_address(address)} - #{process_description(description, locale)}"
|
||||
language = build_language(language, locale)
|
||||
begins_on = build_begins_on(begins_on, timezone, language)
|
||||
|
||||
begins_on
|
||||
|> datetime_to_string(language)
|
||||
|> (&[&1]).()
|
||||
|> add_timezone(begins_on)
|
||||
|> maybe_build_address(address)
|
||||
|> build_description(description, language)
|
||||
|> Enum.join(" - ")
|
||||
end
|
||||
|
||||
defp description(
|
||||
%Event{
|
||||
description: description,
|
||||
begins_on: begins_on
|
||||
},
|
||||
locale
|
||||
) do
|
||||
"#{datetime_to_string(begins_on, locale)} - #{process_description(description, locale)}"
|
||||
@spec build_language(String.t() | nil, String.t()) :: String.t()
|
||||
defp build_language(language, locale), do: language || locale
|
||||
|
||||
@spec build_begins_on(DateTime.t(), String.t() | nil, String.t()) :: DateTime.t()
|
||||
defp build_begins_on(begins_on, timezone, language) do
|
||||
if timezone do
|
||||
case DateTime.shift_zone(begins_on, timezone) do
|
||||
{:ok, begins_on} -> begins_on
|
||||
{:error, _err} -> begins_on
|
||||
end
|
||||
else
|
||||
begins_on
|
||||
end
|
||||
end
|
||||
|
||||
defp add_timezone(elements, %DateTime{} = begins_on) do
|
||||
elements ++ [Cldr.DateTime.Formatter.zone_gmt(begins_on)]
|
||||
end
|
||||
|
||||
@spec maybe_build_address(list(String.t()), Address.t() | nil) :: list(String.t())
|
||||
defp maybe_build_address(elements, %Address{} = address) do
|
||||
elements ++ [render_address(address)]
|
||||
end
|
||||
|
||||
defp maybe_build_address(elements, _address), do: elements
|
||||
|
||||
@spec build_description(list(String.t()), String.t(), String.t()) :: list(String.t())
|
||||
defp build_description(elements, description, language) do
|
||||
elements ++ [process_description(description, language)]
|
||||
end
|
||||
end
|
||||
|
||||
40
lib/service/timezone_detector.ex
Normal file
40
lib/service/timezone_detector.ex
Normal file
@@ -0,0 +1,40 @@
|
||||
defmodule Mobilizon.Service.TimezoneDetector do
|
||||
@moduledoc """
|
||||
Detect the timezone from a point
|
||||
"""
|
||||
|
||||
@type detectable :: Geo.Point.t() | Geo.PointZ.t() | {float() | float()}
|
||||
|
||||
@doc """
|
||||
Detect the most appropriate timezone from a value, a geographic set of coordinates and a fallback
|
||||
"""
|
||||
@spec detect(String.t() | nil, detectable(), String.t()) :: String.t()
|
||||
def detect(nil, geo, fallback) do
|
||||
case TzWorld.timezone_at(geo) do
|
||||
{:ok, timezone} ->
|
||||
timezone
|
||||
|
||||
{:error, :time_zone_not_found} ->
|
||||
fallback
|
||||
end
|
||||
end
|
||||
|
||||
def detect(timezone, geo, fallback) do
|
||||
if Tzdata.zone_exists?(timezone) do
|
||||
timezone
|
||||
else
|
||||
detect(nil, geo, fallback)
|
||||
end
|
||||
end
|
||||
|
||||
@spec detect(String.t() | nil, String.t()) :: String.t()
|
||||
def detect(nil, fallback), do: fallback
|
||||
|
||||
def detect(timezone, fallback) do
|
||||
if Tzdata.zone_exists?(timezone) do
|
||||
timezone
|
||||
else
|
||||
fallback
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user