Render multilang in polls

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
tusooa 2023-01-02 00:30:47 -05:00 committed by marcin mikołajczak
parent 6b20df1e2d
commit e11d4b6923
3 changed files with 35 additions and 5 deletions

View file

@ -4,6 +4,7 @@
defmodule Pleroma.Web.ApiSpec.Schemas.Poll do
alias OpenApiSpex.Schema
alias Pleroma.Web.ApiSpec.Helpers
alias Pleroma.Web.ApiSpec.Schemas.Emoji
alias Pleroma.Web.ApiSpec.Schemas.FlakeID
@ -52,6 +53,7 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Poll do
type: :object,
properties: %{
title: %Schema{type: :string},
title_map: Helpers.multilang_map_of(%Schema{type: :string}),
votes_count: %Schema{type: :integer}
}
},
@ -81,10 +83,12 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Poll do
options: [
%{
title: "accept",
title_map: %{"en" => "accept", "cmn" => "接受"},
votes_count: 6
},
%{
title: "deny",
title_map: %{"en" => "deny", "cmn" => "拒绝"},
votes_count: 4
}
],

View file

@ -65,7 +65,8 @@ defp options_and_votes_count(options) do
current_count = option["replies"]["totalItems"] || 0
{%{
title: name,
title: option["nameRendered"] || name,
title_map: option["nameMap"] || %{},
votes_count: current_count
}, current_count + count}
end)

View file

@ -37,10 +37,10 @@ test "renders a poll" do
id: to_string(object.id),
multiple: false,
options: [
%{title: "absolutely!", votes_count: 0},
%{title: "sure", votes_count: 0},
%{title: "yes", votes_count: 0},
%{title: "why are you even asking?", votes_count: 0}
%{title: "absolutely!", title_map: %{}, votes_count: 0},
%{title: "sure", title_map: %{}, votes_count: 0},
%{title: "yes", title_map: %{}, votes_count: 0},
%{title: "why are you even asking?", title_map: %{}, votes_count: 0}
],
votes_count: 0,
voters_count: 0,
@ -167,6 +167,31 @@ test "doesn't strips HTML tags" do
} = PollView.render("show.json", %{object: object})
end
describe "multilang" do
test "renders multilang" do
object = %Object{
id: 123,
data: %{
"oneOf" => [
%{
"name" => "mew",
"nameMap" => %{"en" => "mew", "cmn" => ""},
"nameRendered" => "mew | 喵"
},
%{"name" => "mew mew", "nameMap" => %{"en" => "mew mew", "cmn" => "喵喵"}}
]
}
}
assert %{
options: [
%{title: "mew | 喵", title_map: %{"en" => "mew", "cmn" => ""}},
%{title: "mew mew", title_map: %{"en" => "mew mew", "cmn" => "喵喵"}}
]
} = PollView.render("show.json", %{object: object})
end
end
test "displays correct voters count" do
object = Object.normalize("https://friends.grishka.me/posts/54642", fetch: true)
result = PollView.render("show.json", %{object: object})