feat: build rel me tags with profile fields

This commit is contained in:
kPherox 2023-02-01 20:24:58 +00:00 committed by XxXCertifiedForkliftDriverXxX
parent 5adce547d0
commit d6271e7613
2 changed files with 29 additions and 4 deletions

View file

@ -8,12 +8,24 @@ defmodule Pleroma.Web.Metadata.Providers.RelMe do
@impl Provider @impl Provider
def build_tags(%{user: user}) do def build_tags(%{user: user}) do
bio_tree = Floki.parse_fragment!(user.bio) profile_tree =
Floki.parse_fragment!(user.bio)
|> prepend_fields_tag(user.fields)
(Floki.attribute(bio_tree, "link[rel~=me]", "href") ++ (Floki.attribute(profile_tree, "link[rel~=me]", "href") ++
Floki.attribute(bio_tree, "a[rel~=me]", "href")) Floki.attribute(profile_tree, "a[rel~=me]", "href"))
|> Enum.map(fn link -> |> Enum.map(fn link ->
{:link, [rel: "me", href: link], []} {:link, [rel: "me", href: link], []}
end) end)
end end
defp prepend_fields_tag(bio_tree, fields) do
fields
|> Enum.reduce(bio_tree, fn %{"value" => v}, tree ->
case Floki.parse_fragment(v) do
{:ok, [a | _]} -> [a | tree]
_ -> tree
end
end)
end
end end

View file

@ -11,10 +11,23 @@ test "it renders all links with rel='me' from user bio" do
bio = bio =
~s(<a href="https://some-link.com">https://some-link.com</a> <a rel="me" href="https://another-link.com">https://another-link.com</a> <link href="http://some.com"> <link rel="me" href="http://some3.com">) ~s(<a href="https://some-link.com">https://some-link.com</a> <a rel="me" href="https://another-link.com">https://another-link.com</a> <link href="http://some.com"> <link rel="me" href="http://some3.com">)
user = insert(:user, %{bio: bio}) fields = [
%{
"name" => "profile",
"value" => ~S(<a rel="me" href="http://profile.com">http://profile.com</a>)
},
%{
"name" => "like",
"value" => ~S(<a href="http://cofe.io">http://cofe.io</a>)
},
%{"name" => "foo", "value" => "bar"}
]
user = insert(:user, %{bio: bio, fields: fields})
assert RelMe.build_tags(%{user: user}) == [ assert RelMe.build_tags(%{user: user}) == [
{:link, [rel: "me", href: "http://some3.com"], []}, {:link, [rel: "me", href: "http://some3.com"], []},
{:link, [rel: "me", href: "http://profile.com"], []},
{:link, [rel: "me", href: "https://another-link.com"], []} {:link, [rel: "me", href: "https://another-link.com"], []}
] ]
end end