Merge branch 'feature/twitter_api/fields' into 'develop'

[Pleroma.Web.TwitterAPI.UserView]: Add mastodon-fields in "fields"

See merge request pleroma/pleroma!360
This commit is contained in:
Haelwenn 2018-09-28 09:25:27 +00:00
commit 34b6d444d6
2 changed files with 43 additions and 5 deletions

View File

@ -37,6 +37,13 @@ def render("user.json", %{user: user = %User{}} = assigns) do
{String.trim(name, ":"), url} {String.trim(name, ":"), url}
end) 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 = %{ data = %{
"created_at" => user.inserted_at |> Utils.format_naive_asctime(), "created_at" => user.inserted_at |> Utils.format_naive_asctime(),
"description" => HTML.strip_tags((user.bio || "") |> String.replace("<br>", "\n")), "description" => HTML.strip_tags((user.bio || "") |> String.replace("<br>", "\n")),
@ -65,7 +72,8 @@ def render("user.json", %{user: user = %User{}} = assigns) do
"is_local" => user.local, "is_local" => user.local,
"locked" => !!user.info["locked"], "locked" => !!user.info["locked"],
"default_scope" => user.info["default_scope"] || "public", "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 if assigns[:token] do

View File

@ -88,7 +88,8 @@ test "A user" do
"is_local" => true, "is_local" => true,
"locked" => false, "locked" => false,
"default_scope" => "public", "default_scope" => "public",
"no_rich_text" => false "no_rich_text" => false,
"fields" => []
} }
assert represented == UserView.render("show.json", %{user: user}) 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, "is_local" => true,
"locked" => false, "locked" => false,
"default_scope" => "public", "default_scope" => "public",
"no_rich_text" => false "no_rich_text" => false,
"fields" => []
} }
assert represented == UserView.render("show.json", %{user: user, for: follower}) 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, "is_local" => true,
"locked" => false, "locked" => false,
"default_scope" => "public", "default_scope" => "public",
"no_rich_text" => false "no_rich_text" => false,
"fields" => []
} }
assert represented == UserView.render("show.json", %{user: follower, for: user}) assert represented == UserView.render("show.json", %{user: follower, for: user})
@ -217,10 +220,37 @@ test "A blocked user for the blocker" do
"is_local" => true, "is_local" => true,
"locked" => false, "locked" => false,
"default_scope" => "public", "default_scope" => "public",
"no_rich_text" => false "no_rich_text" => false,
"fields" => []
} }
blocker = Repo.get(User, blocker.id) blocker = Repo.get(User, blocker.id)
assert represented == UserView.render("show.json", %{user: user, for: blocker}) assert represented == UserView.render("show.json", %{user: user, for: blocker})
end 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 end