From 82b57ebad1b5de0fc4614441e8838cbdb4fe32b3 Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Thu, 27 Sep 2018 20:17:44 +0200 Subject: [PATCH 1/2] [Pleroma.Web.TwitterAPI.UserView]: Add mastodon-fields in "fields" --- lib/pleroma/web/twitter_api/views/user_view.ex | 10 +++++++++- test/web/twitter_api/views/user_view_test.exs | 12 ++++++++---- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/pleroma/web/twitter_api/views/user_view.ex b/lib/pleroma/web/twitter_api/views/user_view.ex index cfbaef4c9..a662f83b6 100644 --- a/lib/pleroma/web/twitter_api/views/user_view.ex +++ b/lib/pleroma/web/twitter_api/views/user_view.ex @@ -37,6 +37,13 @@ def render("user.json", %{user: user = %User{}} = assigns) do {String.trim(name, ":"), url} end) + # ``fields`` is an array of mastodon profile field, containing ``{"name": "…", "value": "…"}``. + # For example: [{"name": "Pronoun", "value": "she/her"}, …] + fields = + (user.info["source_data"]["attachment"] || []) + |> Enum.filter(fn %{"type" => t} -> t == "PropertyValue" end) + |> Enum.map(fn fields -> Map.take(fields, ["name", "value"]) end) + data = %{ "created_at" => user.inserted_at |> Utils.format_naive_asctime(), "description" => HTML.strip_tags((user.bio || "") |> String.replace("
", "\n")), @@ -65,7 +72,8 @@ def render("user.json", %{user: user = %User{}} = assigns) do "is_local" => user.local, "locked" => !!user.info["locked"], "default_scope" => user.info["default_scope"] || "public", - "no_rich_text" => user.info["no_rich_text"] || false + "no_rich_text" => user.info["no_rich_text"] || false, + "fields" => fields } if assigns[:token] do diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index 50687f450..383d7d34c 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -88,7 +88,8 @@ test "A user" do "is_local" => true, "locked" => false, "default_scope" => "public", - "no_rich_text" => false + "no_rich_text" => false, + "fields" => [] } assert represented == UserView.render("show.json", %{user: user}) @@ -128,7 +129,8 @@ test "A user for a given other follower", %{user: user} do "is_local" => true, "locked" => false, "default_scope" => "public", - "no_rich_text" => false + "no_rich_text" => false, + "fields" => [] } assert represented == UserView.render("show.json", %{user: user, for: follower}) @@ -169,7 +171,8 @@ test "A user that follows you", %{user: user} do "is_local" => true, "locked" => false, "default_scope" => "public", - "no_rich_text" => false + "no_rich_text" => false, + "fields" => [] } assert represented == UserView.render("show.json", %{user: follower, for: user}) @@ -217,7 +220,8 @@ test "A blocked user for the blocker" do "is_local" => true, "locked" => false, "default_scope" => "public", - "no_rich_text" => false + "no_rich_text" => false, + "fields" => [] } blocker = Repo.get(User, blocker.id) From a74daabfae5ed149942b5afc64982d3e2ca95b11 Mon Sep 17 00:00:00 2001 From: "Haelwenn (lanodan) Monnier" Date: Fri, 28 Sep 2018 11:17:33 +0200 Subject: [PATCH 2/2] [Pleroma.Web.TwitterAPI.UserViewTest]: Add test for mastodon-fields --- test/web/twitter_api/views/user_view_test.exs | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/test/web/twitter_api/views/user_view_test.exs b/test/web/twitter_api/views/user_view_test.exs index 383d7d34c..2deb22fb1 100644 --- a/test/web/twitter_api/views/user_view_test.exs +++ b/test/web/twitter_api/views/user_view_test.exs @@ -227,4 +227,30 @@ test "A blocked user for the blocker" do blocker = Repo.get(User, blocker.id) assert represented == UserView.render("show.json", %{user: user, for: blocker}) end + + test "a user with mastodon fields" do + fields = [ + %{ + "name" => "Pronouns", + "value" => "she/her" + }, + %{ + "name" => "Website", + "value" => "https://example.org/" + } + ] + + user = + insert(:user, %{ + info: %{ + "source_data" => %{ + "attachment" => + Enum.map(fields, fn field -> Map.put(field, "type", "PropertyValue") end) + } + } + }) + + userview = UserView.render("show.json", %{user: user}) + assert userview["fields"] == fields + end end