Merge branch 'nodeinfo-fixes' into 'main'

fix(nodeinfo): fix getting application actor information from NodeInfo response

See merge request framasoft/mobilizon!1509
This commit is contained in:
Thomas Citharel 2023-12-19 09:40:41 +00:00
commit 8544278c9a
5 changed files with 84 additions and 4 deletions

View file

@ -16,7 +16,6 @@ defmodule Mobilizon.Federation.ActivityPub.Relay do
alias Mobilizon.Federation.{NodeInfo, WebFinger} alias Mobilizon.Federation.{NodeInfo, WebFinger}
alias Mobilizon.GraphQL.API.Follows alias Mobilizon.GraphQL.API.Follows
alias Mobilizon.Service.Workers.Background alias Mobilizon.Service.Workers.Background
import Mobilizon.Federation.ActivityPub.Utils, only: [create_full_domain_string: 1]
require Logger require Logger
@ -185,7 +184,7 @@ defmodule Mobilizon.Federation.ActivityPub.Relay do
defp fetch_actor("http://" <> address), do: fetch_actor(address) defp fetch_actor("http://" <> address), do: fetch_actor(address)
defp fetch_actor(address) do defp fetch_actor(address) do
%URI{host: host} = uri = URI.parse("http://" <> address) %URI{host: host} = URI.parse("http://" <> address)
cond do cond do
String.contains?(address, "@") -> String.contains?(address, "@") ->

View file

@ -25,9 +25,10 @@ defmodule Mobilizon.Federation.NodeInfo do
defp extract_application_actor(body) do defp extract_application_actor(body) do
body body
|> Enum.find(%{rel: @application_uri, href: nil}, fn %{rel: rel, href: href} -> |> Map.get("links", [])
|> Enum.find(%{"rel" => @application_uri, "href" => nil}, fn %{"rel" => rel, "href" => href} ->
rel == @application_uri and is_binary(href) rel == @application_uri and is_binary(href)
end) end)
|> Map.get(:href) |> Map.get("href")
end end
end end

View file

@ -0,0 +1,60 @@
defmodule Mobilizon.Federation.NodeInfoTest do
use Mobilizon.DataCase
import Mox
alias Mobilizon.Federation.NodeInfo
alias Mobilizon.Service.HTTP.WebfingerClient.Mock, as: WebfingerClientMock
@instance_domain "event-federation.eu"
@nodeinfo_fixture_path "test/fixtures/nodeinfo/wp-event-federation.json"
@nodeinfo_regular_fixture_path "test/fixtures/nodeinfo/regular.json"
describe "getting application actor" do
test "from wordpress event federation" do
nodeinfo_data = @nodeinfo_fixture_path |> File.read!() |> Jason.decode!()
WebfingerClientMock
|> expect(:call, fn
%{
method: :get,
url: "https://event-federation.eu/.well-known/nodeinfo"
},
_opts ->
{:ok, %Tesla.Env{status: 200, body: nodeinfo_data}}
end)
assert "https://event-federation.eu/actor-relay" ==
NodeInfo.application_actor(@instance_domain)
end
test "with no FEP-2677 information" do
nodeinfo_data = @nodeinfo_regular_fixture_path |> File.read!() |> Jason.decode!()
WebfingerClientMock
|> expect(:call, fn
%{
method: :get,
url: "https://event-federation.eu/.well-known/nodeinfo"
},
_opts ->
{:ok, %Tesla.Env{status: 200, body: nodeinfo_data}}
end)
assert nil == NodeInfo.application_actor(@instance_domain)
end
test "with no result" do
WebfingerClientMock
|> expect(:call, fn
%{
method: :get,
url: "https://event-federation.eu/.well-known/nodeinfo"
},
_opts ->
{:ok, %Tesla.Env{status: 404, body: ""}}
end)
assert nil == NodeInfo.application_actor(@instance_domain)
end
end
end

8
test/fixtures/nodeinfo/regular.json vendored Normal file
View file

@ -0,0 +1,8 @@
{
"links": [
{
"rel": "http:\/\/nodeinfo.diaspora.software\/ns\/schema\/2.0",
"href": "https:\/\/event-federation.eu\/wp-json\/activitypub\/1.0\/nodeinfo"
}
]
}

View file

@ -0,0 +1,12 @@
{
"links": [
{
"rel": "http:\/\/nodeinfo.diaspora.software\/ns\/schema\/2.0",
"href": "https:\/\/event-federation.eu\/wp-json\/activitypub\/1.0\/nodeinfo"
},
{
"rel": "https://www.w3.org/ns/activitystreams#Application",
"href": "https://event-federation.eu/actor-relay"
}
]
}