Add user setting to provide location and show events near location on

homepage

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel
2021-02-12 18:19:49 +01:00
parent 7d8399f4c8
commit b1cc3868a6
27 changed files with 538 additions and 112 deletions

View File

@@ -40,7 +40,6 @@ defmodule Mobilizon.Service.Geospatial.Addok do
@spec build_url(atom(), map(), list()) :: String.t()
defp build_url(method, args, options) do
limit = Keyword.get(options, :limit, 10)
coords = Keyword.get(options, :coords, nil)
endpoint = Keyword.get(options, :endpoint, @endpoint)
case method do
@@ -48,8 +47,9 @@ defmodule Mobilizon.Service.Geospatial.Addok do
"#{endpoint}/reverse/?lon=#{args.lon}&lat=#{args.lat}&limit=#{limit}"
:search ->
url = "#{endpoint}/search/?q=#{URI.encode(args.q)}&limit=#{limit}"
if is_nil(coords), do: url, else: url <> "&lat=#{coords.lat}&lon=#{coords.lon}"
"#{endpoint}/search/?q=#{URI.encode(args.q)}&limit=#{limit}"
|> add_parameter(options, :country_code)
|> add_parameter(options, :type)
end
end
@@ -89,4 +89,20 @@ defmodule Mobilizon.Service.Geospatial.Addok do
Map.get(properties, "street")
end
end
@spec add_parameter(String.t(), Keyword.t(), atom()) :: String.t()
defp add_parameter(url, options, key) do
value = Keyword.get(options, key)
if is_nil(value), do: url, else: do_add_parameter(url, key, value)
end
@spec do_add_parameter(String.t(), atom(), any()) :: String.t()
defp do_add_parameter(url, :coords, coords),
do: "#{url}&lat=#{coords.lat}&lon=#{coords.lon}"
defp do_add_parameter(url, :type, :administrative),
do: "#{url}&type=municipality"
defp do_add_parameter(url, :type, _type), do: url
end

View File

@@ -29,6 +29,9 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
@api_key_missing_message "API Key required to use Google Maps"
@geocode_endpoint "https://maps.googleapis.com/maps/api/geocode/json"
@details_endpoint "https://maps.googleapis.com/maps/api/place/details/json"
@impl Provider
@doc """
Google Maps implementation for `c:Mobilizon.Service.Geospatial.Provider.geocode/3`.
@@ -77,15 +80,13 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
api_key = Keyword.get(options, :api_key, @api_key)
if is_nil(api_key), do: raise(ArgumentError, message: @api_key_missing_message)
url =
"https://maps.googleapis.com/maps/api/geocode/json?limit=#{limit}&key=#{api_key}&language=#{
lang
}"
url = "#{@geocode_endpoint}?limit=#{limit}&key=#{api_key}&language=#{lang}"
uri =
case method do
:search ->
url <> "&address=#{args.q}"
"#{url}&address=#{args.q}"
|> add_parameter(options, :type)
:geocode ->
zoom = Keyword.get(options, :zoom, 15)
@@ -95,9 +96,7 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
url <> "&latlng=#{args.lat},#{args.lon}&result_type=#{result_type}"
:place_details ->
"https://maps.googleapis.com/maps/api/place/details/json?key=#{api_key}&placeid=#{
args.place_id
}"
"#{@details_endpoint}?key=#{api_key}&placeid=#{args.place_id}"
end
URI.encode(uri)
@@ -173,4 +172,17 @@ defmodule Mobilizon.Service.Geospatial.GoogleMaps do
nil
end
end
@spec add_parameter(String.t(), Keyword.t(), atom()) :: String.t()
defp add_parameter(url, options, key, default \\ nil) do
value = Keyword.get(options, key, default)
if is_nil(value), do: url, else: do_add_parameter(url, key, value)
end
@spec do_add_parameter(String.t(), atom(), any()) :: String.t()
defp do_add_parameter(url, :type, :administrative),
do: "#{url}&components=administrative_area"
defp do_add_parameter(url, :type, _), do: url
end

View File

@@ -43,13 +43,13 @@ defmodule Mobilizon.Service.Geospatial.Mimirsbrunn do
defp build_url(method, args, options) do
limit = Keyword.get(options, :limit, 10)
lang = Keyword.get(options, :lang, "en")
coords = Keyword.get(options, :coords, nil)
endpoint = Keyword.get(options, :endpoint, @endpoint)
case method do
:search ->
url = "#{endpoint}/autocomplete?q=#{URI.encode(args.q)}&lang=#{lang}&limit=#{limit}"
if is_nil(coords), do: url, else: url <> "&lat=#{coords.lat}&lon=#{coords.lon}"
"#{endpoint}/autocomplete?q=#{URI.encode(args.q)}&lang=#{lang}&limit=#{limit}"
|> add_parameter(options, :coords)
|> add_parameter(options, :type)
:geocode ->
"#{endpoint}/reverse?lon=#{args.lon}&lat=#{args.lat}"
@@ -143,4 +143,20 @@ defmodule Mobilizon.Service.Geospatial.Mimirsbrunn do
defp get_postal_code(%{"postcode" => nil}), do: nil
defp get_postal_code(%{"postcode" => postcode}), do: postcode |> String.split(";") |> hd()
@spec add_parameter(String.t(), Keyword.t(), atom()) :: String.t()
defp add_parameter(url, options, key) do
value = Keyword.get(options, key)
if is_nil(value), do: url, else: do_add_parameter(url, key, value)
end
@spec do_add_parameter(String.t(), atom(), any()) :: String.t()
defp do_add_parameter(url, :coords, coords),
do: "#{url}&lat=#{coords.lat}&lon=#{coords.lon}"
defp do_add_parameter(url, :type, :administrative),
do: "#{url}&type=zone"
defp do_add_parameter(url, :type, _type), do: url
end

View File

@@ -41,9 +41,6 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
limit = Keyword.get(options, :limit, 10)
lang = Keyword.get(options, :lang, "en")
endpoint = Keyword.get(options, :endpoint, @endpoint)
country_code = Keyword.get(options, :country_code)
zoom = Keyword.get(options, :zoom)
api_key = Keyword.get(options, :api_key, @api_key)
url =
case method do
@@ -53,16 +50,15 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
}&addressdetails=1&namedetails=1"
:geocode ->
url =
"#{endpoint}/reverse?format=geocodejson&lat=#{args.lat}&lon=#{args.lon}&accept-language=#{
lang
}&addressdetails=1&namedetails=1"
if is_nil(zoom), do: url, else: url <> "&zoom=#{zoom}"
"#{endpoint}/reverse?format=geocodejson&lat=#{args.lat}&lon=#{args.lon}&accept-language=#{
lang
}&addressdetails=1&namedetails=1"
|> add_parameter(options, :zoom)
end
url = if is_nil(country_code), do: url, else: "#{url}&countrycodes=#{country_code}"
if is_nil(api_key), do: url, else: url <> "&key=#{api_key}"
url
|> add_parameter(options, :country_code)
|> add_parameter(options, :api_key, @api_key)
end
@spec fetch_features(String.t()) :: list(Address.t())
@@ -146,4 +142,23 @@ defmodule Mobilizon.Service.Geospatial.Nominatim do
description
end
end
@spec add_parameter(String.t(), Keyword.t(), atom()) :: String.t()
defp add_parameter(url, options, key, default \\ nil) do
value = Keyword.get(options, key, default)
if is_nil(value), do: url, else: do_add_parameter(url, key, value)
end
@spec do_add_parameter(String.t(), atom(), any()) :: String.t()
defp do_add_parameter(url, :zoom, zoom),
do: "#{url}&zoom=#{zoom}"
@spec do_add_parameter(String.t(), atom(), any()) :: String.t()
defp do_add_parameter(url, :country_code, country_code),
do: "#{url}&countrycodes=#{country_code}"
@spec do_add_parameter(String.t(), atom(), any()) :: String.t()
defp do_add_parameter(url, :api_key, api_key),
do: "#{url}&key=#{api_key}"
end

View File

@@ -8,7 +8,6 @@ defmodule Mobilizon.Service.Geospatial.Pelias do
alias Mobilizon.Addresses.Address
alias Mobilizon.Service.Geospatial.Provider
alias Mobilizon.Service.HTTP.GeospatialClient
require Logger
@behaviour Provider
@@ -41,25 +40,20 @@ defmodule Mobilizon.Service.Geospatial.Pelias do
defp build_url(method, args, options) do
limit = Keyword.get(options, :limit, 10)
lang = Keyword.get(options, :lang, "en")
coords = Keyword.get(options, :coords, nil)
endpoint = Keyword.get(options, :endpoint, @endpoint)
country_code = Keyword.get(options, :country_code)
url =
case method do
:search ->
url =
"#{endpoint}/v1/autocomplete?text=#{URI.encode(args.q)}&lang=#{lang}&size=#{limit}"
if is_nil(coords),
do: url,
else: url <> "&focus.point.lat=#{coords.lat}&focus.point.lon=#{coords.lon}"
"#{endpoint}/v1/autocomplete?text=#{URI.encode(args.q)}&lang=#{lang}&size=#{limit}"
|> add_parameter(options, :coords)
|> add_parameter(options, :type)
:geocode ->
"#{endpoint}/v1/reverse?point.lon=#{args.lon}&point.lat=#{args.lat}"
end
if is_nil(country_code), do: url, else: "#{url}&boundary.country=#{country_code}"
add_parameter(url, options, :country_code)
end
@spec fetch_features(String.t()) :: list(Address.t())
@@ -120,9 +114,31 @@ defmodule Mobilizon.Service.Geospatial.Pelias do
"dependency"
]
@spec get_type(map()) :: String.t() | nil
defp get_type(%{"layer" => layer}) when layer in @administrative_layers, do: "administrative"
defp get_type(%{"layer" => "address"}), do: "house"
defp get_type(%{"layer" => "street"}), do: "street"
defp get_type(%{"layer" => "venue"}), do: "venue"
defp get_type(%{"layer" => _}), do: nil
@spec add_parameter(String.t(), Keyword.t(), atom()) :: String.t()
def add_parameter(url, options, key) do
value = Keyword.get(options, key)
if is_nil(value), do: url, else: do_add_parameter(url, key, value)
end
@spec do_add_parameter(String.t(), atom(), any()) :: String.t()
defp do_add_parameter(url, :coords, value),
do: "#{url}&focus.point.lat=#{value.lat}&focus.point.lon=#{value.lon}"
defp do_add_parameter(url, :type, :administrative),
do: "#{url}&layers=coarse"
defp do_add_parameter(url, :type, _type), do: url
defp do_add_parameter(url, :country_code, nil), do: url
defp do_add_parameter(url, :country_code, country_code),
do: "#{url}&boundary.country=#{country_code}"
end

View File

@@ -54,6 +54,8 @@ defmodule Mobilizon.Service.Geospatial.Provider do
* `coords` Map of coordinates (ex: `%{lon: 48.11, lat: -1.77}`) allowing to
give a geographic priority to the search. Defaults to `nil`.
* `type` Filter by type of results. Allowed values:
* `:administrative` (cities, regions)
## Examples