diff --git a/lib/pleroma/user.ex b/lib/pleroma/user.ex index 71ed7e744..541e5a559 100644 --- a/lib/pleroma/user.ex +++ b/lib/pleroma/user.ex @@ -2397,24 +2397,32 @@ defp is_url(uri) do @spec maybe_validate_rel_me_field(Changeset.t(), User.t()) :: Changeset.t() defp maybe_validate_rel_me_field(changeset, %User{ap_id: _ap_id} = struct) do fields = get_change(changeset, :fields) + raw_fields = get_change(changeset, :raw_fields) if is_nil(fields) do changeset else - validate_rel_me_field(changeset, fields, struct) + validate_rel_me_field(changeset, fields, raw_fields, struct) end end defp maybe_validate_rel_me_field(changeset, _), do: changeset - @spec validate_rel_me_field(Changeset.t(), [Map.t()], User.t()) :: Changeset.t() - defp validate_rel_me_field(changeset, fields, %User{ap_id: ap_id}) do + @spec validate_rel_me_field(Changeset.t(), [Map.t()], [Map.t()], User.t()) :: Changeset.t() + defp validate_rel_me_field(changeset, fields, raw_fields, %User{ap_id: ap_id}) do fields = fields - |> Enum.map(fn %{"name" => name, "value" => value} -> - if is_url(value) do - with "me" <- RelMe.maybe_put_rel_me(value, [ap_id]) do - %{"name" => name, "value" => value, "verified_at" => DateTime.to_iso8601(DateTime.utc_now())} + |> Enum.with_index() + |> Enum.map(fn {%{"name" => name, "value" => value}, index} -> + raw_value = Enum.at(raw_fields, index)["value"] + + if is_url(raw_value) do + with "me" <- RelMe.maybe_put_rel_me(raw_value, [ap_id]) do + %{ + "name" => name, + "value" => value, + "verified_at" => DateTime.to_iso8601(DateTime.utc_now()) + } else e -> Logger.error("Could not check for rel=me, #{inspect(e)}") diff --git a/lib/pleroma/web/rel_me.ex b/lib/pleroma/web/rel_me.ex index 1826031dd..a10a13fd2 100644 --- a/lib/pleroma/web/rel_me.ex +++ b/lib/pleroma/web/rel_me.ex @@ -38,15 +38,14 @@ defp parse_url(url) do def maybe_put_rel_me("http" <> _ = target_page, profile_urls) when is_list(profile_urls) do {:ok, rel_me_hrefs} = parse(target_page) - true = Enum.any?(rel_me_hrefs, fn x -> x in profile_urls end) "me" rescue - _ -> nil + e -> nil end - def maybe_put_rel_me(_, _) do + def maybe_put_rel_me(a, b) do nil end end diff --git a/test/pleroma/web/mastodon_api/update_credentials_test.exs b/test/pleroma/web/mastodon_api/update_credentials_test.exs index 2ba909dad..c5b105cba 100644 --- a/test/pleroma/web/mastodon_api/update_credentials_test.exs +++ b/test/pleroma/web/mastodon_api/update_credentials_test.exs @@ -465,6 +465,32 @@ test "update fields", %{conn: conn} do ] end + test "update fields with a link to content with rel=me", %{user: user, conn: conn} do + Tesla.Mock.mock(fn + %{url: "http://example.com/rel_me/ap_id"} -> + %Tesla.Env{ + status: 200, + body: ~s[
] + } + end) + + field = %{name: "Website", value: "http://example.com/rel_me/ap_id"} + + account_data = + conn + |> patch("/api/v1/accounts/update_credentials", %{fields_attributes: [field]}) + |> json_response_and_validate_schema(200) + + assert [%{ + "name" => "Website", + "value" => + ~s[http://example.com/rel_me/ap_id], + "verified_at" => verified_at + }] = account_data["fields"] + {:ok, verified_at, _} = DateTime.from_iso8601(verified_at) + assert DateTime.diff(DateTime.utc_now(), verified_at) < 10 + end + test "emojis in fields labels", %{conn: conn} do fields = [ %{name: ":firefox:", value: "is best 2hu"},