Improve the AP representation and move endpoints to property

Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
Thomas Citharel 2021-11-12 16:29:22 +01:00
parent 6eba531c89
commit f287b9126e
No known key found for this signature in database
GPG Key ID: A061B9DDE0CA0773
2 changed files with 62 additions and 13 deletions

View File

@ -118,6 +118,34 @@ defmodule Mobilizon.Federation.ActivityPub.Utils do
"timezone" => %{ "timezone" => %{
"@id" => "mz:timezone", "@id" => "mz:timezone",
"@type" => "sc:Text" "@type" => "sc:Text"
},
"discussions" => %{
"@id" => "mz:discussions",
"@type" => "@id"
},
"events" => %{
"@id" => "mz:events",
"@type" => "@id"
},
"members" => %{
"@id" => "mz:members",
"@type" => "@id"
},
"openness" => %{
"@id" => "mz:openness",
"@type" => "@id"
},
"posts" => %{
"@id" => "mz:posts",
"@type" => "@id"
},
"resources" => %{
"@id" => "mz:resources",
"@type" => "@id"
},
"todos" => %{
"@id" => "mz:todos",
"@type" => "@id"
} }
} }
] ]

View File

@ -54,13 +54,6 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Actor do
outbox_url: data["outbox"], outbox_url: data["outbox"],
following_url: data["following"], following_url: data["following"],
followers_url: data["followers"], followers_url: data["followers"],
members_url: data["members"],
resources_url: data["resources"],
todos_url: data["todos"],
events_url: data["events"],
posts_url: data["posts"],
discussions_url: data["discussions"],
shared_inbox_url: data["endpoints"]["sharedInbox"],
domain: URI.parse(data["id"]).host, domain: URI.parse(data["id"]).host,
manually_approves_followers: data["manuallyApprovesFollowers"], manually_approves_followers: data["manuallyApprovesFollowers"],
type: data["type"], type: data["type"],
@ -68,10 +61,26 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Actor do
openness: data["openness"], openness: data["openness"],
physical_address_id: if(address, do: address.id, else: nil) physical_address_id: if(address, do: address.id, else: nil)
} }
|> add_endpoints_to_model(data)
end end
def as_to_model_data(_), do: {:error, :actor_not_allowed_type} def as_to_model_data(_), do: {:error, :actor_not_allowed_type}
defp add_endpoints_to_model(actor, data) do
# TODO: Remove fallbacks in 3.0
endpoints = %{
members_url: get_in(data, ["endpoints", "members"]) || data["members"],
resources_url: get_in(data, ["endpoints", "resources"]) || data["resources"],
todos_url: get_in(data, ["endpoints", "todos"]) || data["todos"],
events_url: get_in(data, ["endpoints", "events"]) || data["events"],
posts_url: get_in(data, ["endpoints", "posts"]) || data["posts"],
discussions_url: get_in(data, ["endpoints", "discussions"]) || data["discussions"],
shared_inbox_url: data["endpoints"]["sharedInbox"]
}
Map.merge(actor, endpoints)
end
@doc """ @doc """
Convert an actor struct to an ActivityStream representation. Convert an actor struct to an ActivityStream representation.
""" """
@ -86,12 +95,6 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Actor do
"summary" => actor.summary || "", "summary" => actor.summary || "",
"following" => actor.following_url, "following" => actor.following_url,
"followers" => actor.followers_url, "followers" => actor.followers_url,
"members" => actor.members_url,
"resources" => actor.resources_url,
"todos" => actor.todos_url,
"posts" => actor.posts_url,
"events" => actor.events_url,
"discussions" => actor.discussions_url,
"inbox" => actor.inbox_url, "inbox" => actor.inbox_url,
"outbox" => actor.outbox_url, "outbox" => actor.outbox_url,
"url" => actor.url, "url" => actor.url,
@ -113,12 +116,30 @@ defmodule Mobilizon.Federation.ActivityStream.Converter.Actor do
} }
actor_data actor_data
|> add_endpoints(actor)
|> maybe_add_members(actor) |> maybe_add_members(actor)
|> maybe_add_avatar_picture(actor) |> maybe_add_avatar_picture(actor)
|> maybe_add_banner_picture(actor) |> maybe_add_banner_picture(actor)
|> maybe_add_physical_address(actor) |> maybe_add_physical_address(actor)
end end
defp add_endpoints(%{"endpoints" => endpoints} = actor_data, %ActorModel{} = actor) do
new_endpoints = %{
"members" => actor.members_url,
"resources" => actor.resources_url,
"todos" => actor.todos_url,
"posts" => actor.posts_url,
"events" => actor.events_url,
"discussions" => actor.discussions_url
}
endpoints = Map.merge(endpoints, new_endpoints)
actor_data
|> Map.merge(new_endpoints)
|> Map.put("endpoints", endpoints)
end
@spec download_picture(String.t() | nil, String.t(), String.t()) :: map() | nil @spec download_picture(String.t() | nil, String.t(), String.t()) :: map() | nil
defp download_picture(nil, _name, _default_name), do: nil defp download_picture(nil, _name, _default_name), do: nil