2020-01-26 20:34:25 +01:00
|
|
|
defmodule Mobilizon.GraphQL.Resolvers.Address do
|
2019-03-14 18:31:14 +01:00
|
|
|
@moduledoc """
|
|
|
|
Handles the comment-related GraphQL calls
|
|
|
|
"""
|
2020-01-22 02:14:42 +01:00
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
alias Mobilizon.Addresses.Address
|
2022-07-12 10:55:28 +02:00
|
|
|
alias Mobilizon.Service.{Geospatial, Pictures}
|
2019-03-14 18:31:14 +01:00
|
|
|
|
2020-01-22 02:14:42 +01:00
|
|
|
require Logger
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Search an address
|
|
|
|
"""
|
2021-10-10 16:25:50 +02:00
|
|
|
@spec search(map, map, map) :: {:ok, [map()]}
|
2020-01-26 20:34:25 +01:00
|
|
|
def search(
|
|
|
|
_parent,
|
2021-02-12 18:19:49 +01:00
|
|
|
%{query: query, locale: locale, page: _page, limit: _limit} = args,
|
2020-01-26 20:34:25 +01:00
|
|
|
%{context: %{ip: ip}}
|
|
|
|
) do
|
2019-11-08 19:37:14 +01:00
|
|
|
geolix = Geolix.lookup(ip)
|
2019-03-14 18:31:14 +01:00
|
|
|
|
2019-11-08 19:37:14 +01:00
|
|
|
country_code =
|
|
|
|
case geolix do
|
|
|
|
%{country: %{iso_code: country_code}} -> String.downcase(country_code)
|
|
|
|
_ -> nil
|
|
|
|
end
|
2019-03-14 18:31:14 +01:00
|
|
|
|
2021-02-12 18:19:49 +01:00
|
|
|
addresses =
|
|
|
|
Geospatial.service().search(query,
|
|
|
|
lang: locale,
|
|
|
|
country_code: country_code,
|
|
|
|
type: Map.get(args, :type)
|
|
|
|
)
|
2019-08-22 15:57:44 +02:00
|
|
|
|
2019-03-14 18:31:14 +01:00
|
|
|
{:ok, addresses}
|
|
|
|
end
|
|
|
|
|
2019-03-19 11:16:03 +01:00
|
|
|
@doc """
|
|
|
|
Reverse geocode some coordinates
|
|
|
|
"""
|
2020-01-26 20:34:25 +01:00
|
|
|
@spec reverse_geocode(map, map, map) :: {:ok, [Address.t()]}
|
2019-11-08 19:37:14 +01:00
|
|
|
def reverse_geocode(
|
|
|
|
_parent,
|
|
|
|
%{longitude: longitude, latitude: latitude, zoom: zoom, locale: locale},
|
|
|
|
_context
|
|
|
|
) do
|
2022-07-12 10:55:28 +02:00
|
|
|
addresses =
|
|
|
|
Geospatial.service().geocode(longitude, latitude, lang: locale, zoom: zoom)
|
|
|
|
|> Enum.map(fn address ->
|
|
|
|
picture_info =
|
|
|
|
Pictures.service().search(address.locality || address.region || address.country)
|
|
|
|
|
|
|
|
Map.put(address, :picture_info, picture_info)
|
|
|
|
end)
|
2019-03-14 18:31:14 +01:00
|
|
|
|
|
|
|
{:ok, addresses}
|
|
|
|
end
|
|
|
|
end
|