Merge branch 'address-text-only' into 'main'
feat(activitypub): allow simple text for address field Closes #1387 See merge request framasoft/mobilizon!1510
This commit is contained in:
commit
f4e51c2a90
|
@ -24,9 +24,13 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Address do
|
||||||
}
|
}
|
||||||
|
|
||||||
res =
|
res =
|
||||||
if is_nil(object["address"]) or not is_map(object["address"]) do
|
cond do
|
||||||
res
|
is_binary(object["address"]) ->
|
||||||
else
|
Map.merge(res, %{
|
||||||
|
"street" => object["address"]
|
||||||
|
})
|
||||||
|
|
||||||
|
is_map(object["address"]) ->
|
||||||
Map.merge(res, %{
|
Map.merge(res, %{
|
||||||
"country" => object["address"]["addressCountry"],
|
"country" => object["address"]["addressCountry"],
|
||||||
"postal_code" => object["address"]["postalCode"],
|
"postal_code" => object["address"]["postalCode"],
|
||||||
|
@ -34,6 +38,9 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Address do
|
||||||
"street" => object["address"]["streetAddress"],
|
"street" => object["address"]["streetAddress"],
|
||||||
"locality" => object["address"]["addressLocality"]
|
"locality" => object["address"]["addressLocality"]
|
||||||
})
|
})
|
||||||
|
|
||||||
|
is_nil(object["address"]) ->
|
||||||
|
res
|
||||||
end
|
end
|
||||||
|
|
||||||
latitude = Map.get(object, "latitude")
|
latitude = Map.get(object, "latitude")
|
||||||
|
|
|
@ -198,6 +198,8 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Event do
|
||||||
|
|
||||||
defp calculate_timezone(_object, nil), do: nil
|
defp calculate_timezone(_object, nil), do: nil
|
||||||
|
|
||||||
|
defp calculate_timezone(_object, %Address{geom: nil}), do: nil
|
||||||
|
|
||||||
defp calculate_timezone(_object, %Address{geom: geom}) do
|
defp calculate_timezone(_object, %Address{geom: geom}) do
|
||||||
TimezoneDetector.detect(
|
TimezoneDetector.detect(
|
||||||
nil,
|
nil,
|
||||||
|
|
|
@ -278,8 +278,8 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Utils do
|
||||||
end
|
end
|
||||||
|
|
||||||
@spec get_address(map | binary | nil) :: Address.t() | nil
|
@spec get_address(map | binary | nil) :: Address.t() | nil
|
||||||
def get_address(address_url) when is_binary(address_url) do
|
def get_address(text_address) when is_binary(text_address) do
|
||||||
get_address(%{"id" => address_url})
|
get_address(%{"type" => "Place", "name" => text_address})
|
||||||
end
|
end
|
||||||
|
|
||||||
def get_address(%{"id" => url} = map) when is_map(map) and is_binary(url) do
|
def get_address(%{"id" => url} = map) when is_map(map) and is_binary(url) do
|
||||||
|
|
74
test/federation/activity_stream/converter/address_test.exs
Normal file
74
test/federation/activity_stream/converter/address_test.exs
Normal file
|
@ -0,0 +1,74 @@
|
||||||
|
defmodule Mobilizon.Federation.ActivityStream.Converter.AddressTest do
|
||||||
|
use Mobilizon.DataCase
|
||||||
|
|
||||||
|
alias Mobilizon.Addresses.Address
|
||||||
|
|
||||||
|
alias Mobilizon.Federation.ActivityStream.Converter.Address, as: AddressConverter
|
||||||
|
|
||||||
|
describe "address to AS" do
|
||||||
|
test "valid address to as" do
|
||||||
|
data =
|
||||||
|
AddressConverter.model_to_as(%Address{
|
||||||
|
country: "France",
|
||||||
|
locality: "Lyon",
|
||||||
|
description: "Locaux'Motiv",
|
||||||
|
postal_code: "69007",
|
||||||
|
street: "10Bis Rue Jangot"
|
||||||
|
})
|
||||||
|
|
||||||
|
assert is_map(data)
|
||||||
|
assert data["type"] == "Place"
|
||||||
|
assert data["name"] == "Locaux'Motiv"
|
||||||
|
assert data["address"]["type"] == "PostalAddress"
|
||||||
|
assert data["address"]["addressLocality"] == "Lyon"
|
||||||
|
assert data["address"]["postalCode"] == "69007"
|
||||||
|
assert data["address"]["addressCountry"] == "France"
|
||||||
|
assert data["address"]["streetAddress"] == "10Bis Rue Jangot"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
describe "AS to Address" do
|
||||||
|
test "basic AS data to model" do
|
||||||
|
address =
|
||||||
|
AddressConverter.as_to_model_data(%{
|
||||||
|
"type" => "Place",
|
||||||
|
"name" => "Federazione Anarchica Torinese",
|
||||||
|
"address" => "corso Palermo 46",
|
||||||
|
"latitude" => nil,
|
||||||
|
"longitude" => nil
|
||||||
|
})
|
||||||
|
|
||||||
|
assert address["description"] == "Federazione Anarchica Torinese"
|
||||||
|
assert address["street"] == "corso Palermo 46"
|
||||||
|
assert address["locality"] == nil
|
||||||
|
assert address["geom"] == nil
|
||||||
|
end
|
||||||
|
|
||||||
|
test "AS data with PostalAddress to model" do
|
||||||
|
address =
|
||||||
|
AddressConverter.as_to_model_data(%{
|
||||||
|
"type" => "Place",
|
||||||
|
"name" => "Federazione Anarchica Torinese",
|
||||||
|
"address" => %{
|
||||||
|
"streetAddress" => "Corso Palermo, 46",
|
||||||
|
"addressCountry" => "Italia",
|
||||||
|
"postalCode" => "10152",
|
||||||
|
"addressLocality" => "Torino"
|
||||||
|
},
|
||||||
|
"latitude" => 45.08281,
|
||||||
|
"longitude" => 7.69311
|
||||||
|
})
|
||||||
|
|
||||||
|
assert address["description"] == "Federazione Anarchica Torinese"
|
||||||
|
assert address["street"] == "Corso Palermo, 46"
|
||||||
|
assert address["locality"] == "Torino"
|
||||||
|
assert address["postal_code"] == "10152"
|
||||||
|
assert address["country"] == "Italia"
|
||||||
|
|
||||||
|
assert address["geom"] == %Geo.Point{
|
||||||
|
coordinates: {7.69311, 45.08281},
|
||||||
|
srid: 4326
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in a new issue