Render multilang of sources
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
ad8f47fe8f
commit
64db6ce871
5 changed files with 77 additions and 12 deletions
|
@ -83,17 +83,20 @@ def no_content_response do
|
|||
Operation.response("No Content", "application/json", %Schema{type: :string, example: ""})
|
||||
end
|
||||
|
||||
def multilang_map_of(embedded_schema) do
|
||||
%Schema{
|
||||
type: :object,
|
||||
title:
|
||||
if embedded_schema.title do
|
||||
"MultiLang map of #{embedded_schema.title}"
|
||||
else
|
||||
"MultiLang map"
|
||||
end,
|
||||
additionalProperties: embedded_schema,
|
||||
description: "Map from a BCP47 language tag to a string in that language."
|
||||
}
|
||||
def multilang_map_of(embedded_schema, opts \\ []) do
|
||||
struct(
|
||||
%Schema{
|
||||
type: :object,
|
||||
title:
|
||||
if embedded_schema.title do
|
||||
"MultiLang map of #{embedded_schema.title}"
|
||||
else
|
||||
"MultiLang map"
|
||||
end,
|
||||
additionalProperties: embedded_schema,
|
||||
description: "Map from a BCP47 language tag to a string in that language."
|
||||
},
|
||||
opts
|
||||
)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -829,11 +829,22 @@ defp status_source_response do
|
|||
type: :string,
|
||||
description: "Raw source of status content"
|
||||
},
|
||||
text_map:
|
||||
Helpers.multilang_map_of(%Schema{
|
||||
type: :string,
|
||||
description: "Raw source of status content"
|
||||
}),
|
||||
spoiler_text: %Schema{
|
||||
type: :string,
|
||||
description:
|
||||
"Subject or summary line, below which status content is collapsed until expanded"
|
||||
},
|
||||
spoiler_text_map:
|
||||
Helpers.multilang_map_of(%Schema{
|
||||
type: :string,
|
||||
description:
|
||||
"Subject or summary line, below which status content is collapsed until expanded"
|
||||
}),
|
||||
content_type: %Schema{
|
||||
type: :string,
|
||||
description: "The content type of the source"
|
||||
|
|
|
@ -80,6 +80,14 @@ defmodule Pleroma.Web.ApiSpec.Schemas.Status do
|
|||
description: "Original unformatted content in plain text",
|
||||
nullable: true
|
||||
},
|
||||
text_map:
|
||||
Helpers.multilang_map_of(
|
||||
%Schema{
|
||||
type: :string,
|
||||
description: "Original unformatted content in plain text"
|
||||
},
|
||||
nullable: true
|
||||
),
|
||||
created_at: %Schema{
|
||||
type: :string,
|
||||
format: "date-time",
|
||||
|
|
|
@ -429,6 +429,7 @@ def render("show.json", %{activity: %{data: %{"object" => _object}} = activity}
|
|||
content: content_html,
|
||||
content_map: content_html_map,
|
||||
text: opts[:with_source] && get_source_text(object.data["source"]),
|
||||
text_map: opts[:with_source] && get_source_text_map(Map.get(object.data, "source", "")),
|
||||
created_at: created_at,
|
||||
edited_at: edited_at,
|
||||
reblogs_count: announcement_count,
|
||||
|
@ -570,7 +571,9 @@ def render("source.json", %{activity: %{data: %{"object" => _object}} = activity
|
|||
%{
|
||||
id: activity.id,
|
||||
text: get_source_text(Map.get(object.data, "source", "")),
|
||||
text_map: get_source_text_map(Map.get(object.data, "source", "")),
|
||||
spoiler_text: Map.get(object.data, "summary", ""),
|
||||
spoiler_text_map: Map.get(object.data, "summaryMap", %{}),
|
||||
content_type: get_source_content_type(object.data["source"]),
|
||||
location: build_source_location(object.data)
|
||||
}
|
||||
|
@ -896,6 +899,12 @@ defp get_source_text(_) do
|
|||
""
|
||||
end
|
||||
|
||||
defp get_source_text_map(%{"contentMap" => %{} = content_map} = _source) do
|
||||
content_map
|
||||
end
|
||||
|
||||
defp get_source_text_map(_), do: %{}
|
||||
|
||||
defp get_source_content_type(%{"mediaType" => type} = _source) do
|
||||
type
|
||||
end
|
||||
|
|
|
@ -294,6 +294,7 @@ test "a note activity" do
|
|||
content: HTML.filter_tags(object_data["content"]),
|
||||
content_map: %{},
|
||||
text: nil,
|
||||
text_map: nil,
|
||||
created_at: created_at,
|
||||
edited_at: nil,
|
||||
reblogs_count: 0,
|
||||
|
@ -1057,4 +1058,37 @@ test "renders multilang" do
|
|||
} = status
|
||||
end
|
||||
end
|
||||
|
||||
describe "source" do
|
||||
test "renders multilang" do
|
||||
user = insert(:user)
|
||||
|
||||
note_obj =
|
||||
insert(:note,
|
||||
data: %{
|
||||
"source" => %{
|
||||
"content" => "mew mew",
|
||||
"contentMap" => %{"en" => "mew mew", "cmn" => "喵喵"},
|
||||
"mediaType" => "text/plain"
|
||||
},
|
||||
"summary" => "mew",
|
||||
"summaryMap" => %{"en" => "mew", "cmn" => "喵"}
|
||||
}
|
||||
)
|
||||
|
||||
note = insert(:note_activity, note: note_obj, user: user)
|
||||
|
||||
status =
|
||||
StatusView.render("source.json", %{
|
||||
activity: note
|
||||
})
|
||||
|
||||
assert %{
|
||||
text: "mew mew",
|
||||
text_map: %{"en" => "mew mew", "cmn" => "喵喵"},
|
||||
spoiler_text: "mew",
|
||||
spoiler_text_map: %{"en" => "mew", "cmn" => "喵"}
|
||||
} = status
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue