AP ingest: normalize object url list
This commit is contained in:
parent
0feb23a308
commit
39817fa547
3 changed files with 106 additions and 1 deletions
|
|
@ -46,7 +46,32 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.ArticleNotePageValidator do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp fix_url(%{"url" => url} = data) when is_bitstring(url), do: data
|
defp fix_url(%{"url" => url} = data) when is_bitstring(url), do: data
|
||||||
defp fix_url(%{"url" => url} = data) when is_map(url), do: Map.put(data, "url", url["href"])
|
|
||||||
|
defp fix_url(%{"url" => url} = data) when is_map(url) do
|
||||||
|
if is_binary(url["href"]) do
|
||||||
|
Map.put(data, "url", url["href"])
|
||||||
|
else
|
||||||
|
Map.delete(data, "url")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp fix_url(%{"url" => url} = data) when is_list(url) do
|
||||||
|
first_element = Enum.at(url, 0)
|
||||||
|
|
||||||
|
url_string =
|
||||||
|
cond do
|
||||||
|
is_bitstring(first_element) -> first_element
|
||||||
|
is_map(first_element) -> first_element["href"]
|
||||||
|
true -> nil
|
||||||
|
end
|
||||||
|
|
||||||
|
if is_binary(url_string) do
|
||||||
|
Map.put(data, "url", url_string)
|
||||||
|
else
|
||||||
|
Map.delete(data, "url")
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
defp fix_url(data), do: data
|
defp fix_url(data), do: data
|
||||||
|
|
||||||
defp fix_tag(%{"tag" => tag} = data) when is_list(tag) do
|
defp fix_tag(%{"tag" => tag} = data) when is_list(tag) do
|
||||||
|
|
|
||||||
27
test/fixtures/fedidev.fun/url_list.json
vendored
Normal file
27
test/fixtures/fedidev.fun/url_list.json
vendored
Normal file
|
|
@ -0,0 +1,27 @@
|
||||||
|
{
|
||||||
|
"@context": [
|
||||||
|
"https://www.w3.org/ns/activitystreams"
|
||||||
|
],
|
||||||
|
"type": "Create",
|
||||||
|
"id": "https://fedidev.fun/activity/url-list",
|
||||||
|
"actor": "https://fedidev.fun/users/alice",
|
||||||
|
"to": [
|
||||||
|
"https://www.w3.org/ns/activitystreams#Public"
|
||||||
|
],
|
||||||
|
"cc": [],
|
||||||
|
"published": "2025-11-21T14:39:06Z",
|
||||||
|
"object": {
|
||||||
|
"type": "Note",
|
||||||
|
"id": "https://fedidev.fun/objects/note-url-list",
|
||||||
|
"attributedTo": "https://fedidev.fun/users/alice",
|
||||||
|
"to": [
|
||||||
|
"https://www.w3.org/ns/activitystreams#Public"
|
||||||
|
],
|
||||||
|
"cc": [],
|
||||||
|
"published": "2025-11-21T14:39:06Z",
|
||||||
|
"content": "text",
|
||||||
|
"url": [
|
||||||
|
"https://fedidev.fun/objects/123"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,53 @@
|
||||||
|
# Pleroma: A lightweight social networking server
|
||||||
|
# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
|
||||||
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
|
defmodule Pleroma.Web.ActivityPub.Transmogrifier.FedidevFunUrlListTest do
|
||||||
|
use Oban.Testing, repo: Pleroma.Repo
|
||||||
|
use Pleroma.DataCase
|
||||||
|
|
||||||
|
alias Pleroma.Object
|
||||||
|
alias Pleroma.User
|
||||||
|
alias Pleroma.Web.ActivityPub.Transmogrifier
|
||||||
|
|
||||||
|
import Pleroma.Factory
|
||||||
|
|
||||||
|
setup_all do
|
||||||
|
Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
|
||||||
|
defp ensure_remote_actor!(ap_id) when is_binary(ap_id) do
|
||||||
|
case User.get_cached_by_ap_id(ap_id) do
|
||||||
|
%User{} ->
|
||||||
|
:ok
|
||||||
|
|
||||||
|
_ ->
|
||||||
|
insert(:user,
|
||||||
|
local: false,
|
||||||
|
ap_id: ap_id,
|
||||||
|
follower_address: ap_id <> "/followers",
|
||||||
|
following_address: ap_id <> "/following",
|
||||||
|
featured_address: ap_id <> "/collections/featured",
|
||||||
|
inbox: ap_id <> "/inbox"
|
||||||
|
)
|
||||||
|
|
||||||
|
:ok
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
defp ingest_fixture!(rel_path) do
|
||||||
|
data = rel_path |> File.read!() |> Jason.decode!()
|
||||||
|
ensure_remote_actor!(data["actor"])
|
||||||
|
|
||||||
|
assert {:ok, activity} = Transmogrifier.handle_incoming(data)
|
||||||
|
object = Object.normalize(activity.data["object"], fetch: false)
|
||||||
|
|
||||||
|
{activity, object}
|
||||||
|
end
|
||||||
|
|
||||||
|
test "normalizes object url list to a string" do
|
||||||
|
{_activity, object} = ingest_fixture!("test/fixtures/fedidev.fun/url_list.json")
|
||||||
|
assert object.data["url"] == "https://fedidev.fun/objects/123"
|
||||||
|
end
|
||||||
|
end
|
||||||
Loading…
Add table
Add a link
Reference in a new issue