2020-01-22 22:40:40 +01:00
|
|
|
defmodule Mobilizon.Federation.ActivityStream.Converter.Address do
|
2019-07-30 10:35:29 +02:00
|
|
|
@moduledoc """
|
2019-09-22 16:26:23 +02:00
|
|
|
Address converter.
|
2019-07-30 10:35:29 +02:00
|
|
|
|
2019-09-22 16:26:23 +02:00
|
|
|
This module allows to convert reports from ActivityStream format to our own
|
|
|
|
internal one, and back.
|
2019-07-30 10:35:29 +02:00
|
|
|
"""
|
2019-09-22 16:26:23 +02:00
|
|
|
|
2019-07-30 10:35:29 +02:00
|
|
|
alias Mobilizon.Addresses.Address, as: AddressModel
|
2020-01-22 02:14:42 +01:00
|
|
|
|
2020-01-22 22:40:40 +01:00
|
|
|
alias Mobilizon.Federation.ActivityStream.Converter
|
2019-07-30 10:35:29 +02:00
|
|
|
|
|
|
|
@behaviour Converter
|
|
|
|
|
|
|
|
@doc """
|
2019-09-22 18:29:13 +02:00
|
|
|
Converts an AP object data to our internal data structure.
|
2019-07-30 10:35:29 +02:00
|
|
|
"""
|
|
|
|
@impl Converter
|
2019-09-22 18:29:13 +02:00
|
|
|
@spec as_to_model_data(map) :: map
|
2019-07-30 10:35:29 +02:00
|
|
|
def as_to_model_data(object) do
|
|
|
|
res = %{
|
|
|
|
"description" => object["name"],
|
|
|
|
"url" => object["url"]
|
|
|
|
}
|
|
|
|
|
|
|
|
res =
|
|
|
|
if is_nil(object["address"]) do
|
|
|
|
res
|
|
|
|
else
|
|
|
|
Map.merge(res, %{
|
|
|
|
"country" => object["address"]["addressCountry"],
|
|
|
|
"postal_code" => object["address"]["postalCode"],
|
|
|
|
"region" => object["address"]["addressRegion"],
|
|
|
|
"street" => object["address"]["streetAddress"],
|
|
|
|
"locality" => object["address"]["addressLocality"]
|
|
|
|
})
|
|
|
|
end
|
|
|
|
|
2021-09-06 12:46:29 +02:00
|
|
|
latitude = Map.get(object, "latitude")
|
|
|
|
longitude = Map.get(object, "longitude")
|
2019-07-30 10:35:29 +02:00
|
|
|
|
2021-09-06 12:46:29 +02:00
|
|
|
if is_float(latitude) and is_float(longitude) do
|
|
|
|
Map.put(res, "geom", %Geo.Point{
|
|
|
|
coordinates: {longitude, latitude},
|
|
|
|
srid: 4326
|
|
|
|
})
|
|
|
|
else
|
|
|
|
res
|
2019-07-30 10:35:29 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
@doc """
|
2019-09-22 18:29:13 +02:00
|
|
|
Convert an event struct to an ActivityStream representation.
|
2019-07-30 10:35:29 +02:00
|
|
|
"""
|
|
|
|
@impl Converter
|
2019-09-22 18:29:13 +02:00
|
|
|
@spec model_to_as(AddressModel.t()) :: map
|
2019-07-30 16:40:59 +02:00
|
|
|
def model_to_as(%AddressModel{} = address) do
|
|
|
|
res = %{
|
|
|
|
"type" => "Place",
|
|
|
|
"name" => address.description,
|
|
|
|
"id" => address.url,
|
|
|
|
"address" => %{
|
|
|
|
"type" => "PostalAddress",
|
|
|
|
"streetAddress" => address.street,
|
|
|
|
"postalCode" => address.postal_code,
|
|
|
|
"addressLocality" => address.locality,
|
|
|
|
"addressRegion" => address.region,
|
|
|
|
"addressCountry" => address.country
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if is_nil(address.geom) do
|
|
|
|
res
|
|
|
|
else
|
2019-12-18 11:03:04 +01:00
|
|
|
res
|
2021-09-06 12:46:29 +02:00
|
|
|
|> Map.put("longitude", address.geom.coordinates |> elem(0))
|
|
|
|
|> Map.put("latitude", address.geom.coordinates |> elem(1))
|
2019-07-30 16:40:59 +02:00
|
|
|
end
|
2019-07-30 10:35:29 +02:00
|
|
|
end
|
|
|
|
end
|