From d5d7648789847b943caf339732aeeed11cd2728d Mon Sep 17 00:00:00 2001 From: kPherox Date: Wed, 1 Feb 2023 20:24:58 +0000 Subject: [PATCH 1/2] feat: build rel me tags with profile fields --- lib/pleroma/web/metadata/providers/rel_me.ex | 18 +++++++++++++++--- .../web/metadata/providers/rel_me_test.exs | 15 ++++++++++++++- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/lib/pleroma/web/metadata/providers/rel_me.ex b/lib/pleroma/web/metadata/providers/rel_me.ex index f0bee85c8..e1890a050 100644 --- a/lib/pleroma/web/metadata/providers/rel_me.ex +++ b/lib/pleroma/web/metadata/providers/rel_me.ex @@ -8,12 +8,24 @@ defmodule Pleroma.Web.Metadata.Providers.RelMe do @impl Provider 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(bio_tree, "a[rel~=me]", "href")) + (Floki.attribute(profile_tree, "link[rel~=me]", "href") ++ + Floki.attribute(profile_tree, "a[rel~=me]", "href")) |> Enum.map(fn link -> {:link, [rel: "me", href: link], []} 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 diff --git a/test/pleroma/web/metadata/providers/rel_me_test.exs b/test/pleroma/web/metadata/providers/rel_me_test.exs index cce4f3607..b1177edb7 100644 --- a/test/pleroma/web/metadata/providers/rel_me_test.exs +++ b/test/pleroma/web/metadata/providers/rel_me_test.exs @@ -11,10 +11,23 @@ defmodule Pleroma.Web.Metadata.Providers.RelMeTest do bio = ~s(https://some-link.com https://another-link.com ) - user = insert(:user, %{bio: bio}) + fields = [ + %{ + "name" => "profile", + "value" => ~S(http://profile.com) + }, + %{ + "name" => "like", + "value" => ~S(http://cofe.io) + }, + %{"name" => "foo", "value" => "bar"} + ] + + user = insert(:user, %{bio: bio, fields: fields}) assert RelMe.build_tags(%{user: user}) == [ {:link, [rel: "me", href: "http://some3.com"], []}, + {:link, [rel: "me", href: "http://profile.com"], []}, {:link, [rel: "me", href: "https://another-link.com"], []} ] end From 83c7415803774a4c56c718f5ebed2b9714ffdfcf Mon Sep 17 00:00:00 2001 From: kPherox Date: Wed, 15 Mar 2023 23:55:24 +0900 Subject: [PATCH 2/2] fix: append field values to bio before parsing --- lib/pleroma/web/metadata/providers/rel_me.ex | 14 +++++--------- .../pleroma/web/metadata/providers/rel_me_test.exs | 4 ++-- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/lib/pleroma/web/metadata/providers/rel_me.ex b/lib/pleroma/web/metadata/providers/rel_me.ex index e1890a050..eabd8cb00 100644 --- a/lib/pleroma/web/metadata/providers/rel_me.ex +++ b/lib/pleroma/web/metadata/providers/rel_me.ex @@ -9,8 +9,9 @@ defmodule Pleroma.Web.Metadata.Providers.RelMe do @impl Provider def build_tags(%{user: user}) do profile_tree = - Floki.parse_fragment!(user.bio) - |> prepend_fields_tag(user.fields) + user.bio + |> append_fields_tag(user.fields) + |> Floki.parse_fragment!() (Floki.attribute(profile_tree, "link[rel~=me]", "href") ++ Floki.attribute(profile_tree, "a[rel~=me]", "href")) @@ -19,13 +20,8 @@ defmodule Pleroma.Web.Metadata.Providers.RelMe do end) end - defp prepend_fields_tag(bio_tree, fields) do + defp append_fields_tag(bio, fields) do fields - |> Enum.reduce(bio_tree, fn %{"value" => v}, tree -> - case Floki.parse_fragment(v) do - {:ok, [a | _]} -> [a | tree] - _ -> tree - end - end) + |> Enum.reduce(bio, fn %{"value" => v}, res -> res <> v end) end end diff --git a/test/pleroma/web/metadata/providers/rel_me_test.exs b/test/pleroma/web/metadata/providers/rel_me_test.exs index b1177edb7..793669037 100644 --- a/test/pleroma/web/metadata/providers/rel_me_test.exs +++ b/test/pleroma/web/metadata/providers/rel_me_test.exs @@ -27,8 +27,8 @@ defmodule Pleroma.Web.Metadata.Providers.RelMeTest do assert RelMe.build_tags(%{user: user}) == [ {: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"], []}, + {:link, [rel: "me", href: "http://profile.com"], []} ] end end