MultiLanguage: Remove map_to_str
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
parent
9b0c05f70c
commit
48be78b375
26 changed files with 115 additions and 192 deletions
|
@ -940,12 +940,6 @@
|
||||||
|
|
||||||
config :pleroma, Pleroma.Web.WebFinger, domain: nil, update_nickname_on_user_fetch: true
|
config :pleroma, Pleroma.Web.WebFinger, domain: nil, update_nickname_on_user_fetch: true
|
||||||
|
|
||||||
config :pleroma, Pleroma.MultiLanguage,
|
|
||||||
template: "<div lang=\"{code}\">{content}</div>",
|
|
||||||
separator: "<br><hr><br>",
|
|
||||||
single_line_template: "[{code}] {content}",
|
|
||||||
single_line_separator: " | "
|
|
||||||
|
|
||||||
config :pleroma, Pleroma.Language.Translation, allow_unauthenticated: false, allow_remote: true
|
config :pleroma, Pleroma.Language.Translation, allow_unauthenticated: false, allow_remote: true
|
||||||
|
|
||||||
config :geospatial, Geospatial.Service, service: Geospatial.Providers.Nominatim
|
config :geospatial, Geospatial.Service, service: Geospatial.Providers.Nominatim
|
||||||
|
|
|
@ -3,12 +3,6 @@
|
||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
defmodule Pleroma.MultiLanguage do
|
defmodule Pleroma.MultiLanguage do
|
||||||
defp template(:multi), do: Pleroma.Config.get([__MODULE__, :template])
|
|
||||||
defp template(:single), do: Pleroma.Config.get([__MODULE__, :single_line_template])
|
|
||||||
|
|
||||||
defp sep(:multi), do: Pleroma.Config.get([__MODULE__, :separator])
|
|
||||||
defp sep(:single), do: Pleroma.Config.get([__MODULE__, :single_line_separator])
|
|
||||||
|
|
||||||
def good_locale_code?(code) when is_binary(code), do: code =~ ~r<^[a-zA-Z0-9\-]+$>
|
def good_locale_code?(code) when is_binary(code), do: code =~ ~r<^[a-zA-Z0-9\-]+$>
|
||||||
|
|
||||||
def good_locale_code?(_code), do: false
|
def good_locale_code?(_code), do: false
|
||||||
|
@ -37,27 +31,6 @@ def validate_map(%{} = object) do
|
||||||
|
|
||||||
def validate_map(_), do: {:error, nil}
|
def validate_map(_), do: {:error, nil}
|
||||||
|
|
||||||
def map_to_str(data, opts \\ []) do
|
|
||||||
map_to_str_impl(data, if(opts[:multiline], do: :multi, else: :single))
|
|
||||||
end
|
|
||||||
|
|
||||||
defp map_to_str_impl(data, mode) do
|
|
||||||
with ks <- Map.keys(data),
|
|
||||||
[_, _ | _] <- ks,
|
|
||||||
ks <- Enum.sort(ks) do
|
|
||||||
template = template(mode)
|
|
||||||
|
|
||||||
ks
|
|
||||||
|> Enum.map(fn lang ->
|
|
||||||
format_template(template, %{code: lang, content: data[lang]})
|
|
||||||
end)
|
|
||||||
|> Enum.join(sep(mode))
|
|
||||||
else
|
|
||||||
[lang] -> data[lang]
|
|
||||||
_ -> nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def str_to_map(data, opts \\ []) do
|
def str_to_map(data, opts \\ []) do
|
||||||
with lang when is_binary(lang) <- opts[:lang],
|
with lang when is_binary(lang) <- opts[:lang],
|
||||||
true <- good_locale_code?(lang) do
|
true <- good_locale_code?(lang) do
|
||||||
|
@ -67,16 +40,4 @@ def str_to_map(data, opts \\ []) do
|
||||||
%{"und" => data}
|
%{"und" => data}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def format_template(template, %{code: code, content: content}) do
|
|
||||||
template
|
|
||||||
|> String.replace(
|
|
||||||
["{code}", "{content}"],
|
|
||||||
fn
|
|
||||||
"{code}" -> code
|
|
||||||
"{content}" -> content
|
|
||||||
end,
|
|
||||||
global: true
|
|
||||||
)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -91,23 +91,23 @@ defp get_description(upload) do
|
||||||
end
|
end
|
||||||
|
|
||||||
defp validate_description_limit(%{} = description) do
|
defp validate_description_limit(%{} = description) do
|
||||||
len = Enum.reduce(description, 0, fn {_, content}, acc -> String.length(content) + acc end)
|
Enum.each(description, fn content ->
|
||||||
|
String.length(content) <= Pleroma.Config.get([:instance, :description_limit])
|
||||||
len <= Pleroma.Config.get([:instance, :description_limit])
|
end)
|
||||||
end
|
end
|
||||||
|
|
||||||
defp validate_description_limit(description) when is_binary(description) do
|
defp validate_description_limit(description) when is_binary(description) do
|
||||||
String.length(description) <= Pleroma.Config.get([:instance, :description_limit])
|
String.length(description) <= Pleroma.Config.get([:instance, :description_limit])
|
||||||
end
|
end
|
||||||
|
|
||||||
defp description_fields(%{} = description) do
|
defp description_fields(%{} = description, language) do
|
||||||
%{
|
%{
|
||||||
"name" => Pleroma.MultiLanguage.map_to_str(description, multiline: false),
|
"name" => Map.get(description, language),
|
||||||
"nameMap" => description
|
"nameMap" => description
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
defp description_fields(description) when is_binary(description) do
|
defp description_fields(description, _) when is_binary(description) do
|
||||||
%{"name" => description}
|
%{"name" => description}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -121,6 +121,9 @@ def store(upload, opts \\ []) do
|
||||||
{:ok, upload} <- Pleroma.Upload.Filter.filter(opts.filters, upload),
|
{:ok, upload} <- Pleroma.Upload.Filter.filter(opts.filters, upload),
|
||||||
description = get_description(upload),
|
description = get_description(upload),
|
||||||
{_, true} <- {:description_limit, validate_description_limit(description)},
|
{_, true} <- {:description_limit, validate_description_limit(description)},
|
||||||
|
{_, true} <-
|
||||||
|
{:valid_locale,
|
||||||
|
opts[:language] == nil or Pleroma.MultiLanguage.good_locale_code?(opts[:language])},
|
||||||
{:ok, url_spec} <- Pleroma.Uploaders.Uploader.put_file(opts.uploader, upload) do
|
{:ok, url_spec} <- Pleroma.Uploaders.Uploader.put_file(opts.uploader, upload) do
|
||||||
{:ok,
|
{:ok,
|
||||||
%{
|
%{
|
||||||
|
@ -137,8 +140,9 @@ def store(upload, opts \\ []) do
|
||||||
|> Maps.put_if_present("height", upload.height)
|
|> Maps.put_if_present("height", upload.height)
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|> Map.merge(description_fields(description))
|
|> Map.merge(description_fields(description, opts[:language]))
|
||||||
|> Maps.put_if_present("blurhash", upload.blurhash)}
|
|> Maps.put_if_present("blurhash", upload.blurhash)
|
||||||
|
|> Maps.put_if_present("language", opts[:language])}
|
||||||
else
|
else
|
||||||
{:description_limit, _} ->
|
{:description_limit, _} ->
|
||||||
{:error, :description_too_long}
|
{:error, :description_too_long}
|
||||||
|
|
|
@ -1593,27 +1593,37 @@ def fetch_activities_bounded(
|
||||||
|> Enum.reverse()
|
|> Enum.reverse()
|
||||||
end
|
end
|
||||||
|
|
||||||
defp validate_media_description_map(%{} = map) do
|
defp validate_media_description_map(%{} = map, language) do
|
||||||
with {:ok, %{}} <- Pleroma.MultiLanguage.validate_map(map) do
|
with {:ok, %{}} <- Pleroma.MultiLanguage.validate_map(map),
|
||||||
|
true <- Pleroma.MultiLanguage.good_locale_code?(language) do
|
||||||
:ok
|
:ok
|
||||||
else
|
else
|
||||||
|
false -> :invalid_language
|
||||||
_ -> :error
|
_ -> :error
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
defp validate_media_description_map(nil), do: :ok
|
defp validate_media_description_map(nil, _), do: :ok
|
||||||
defp validate_media_description_map(_), do: :error
|
defp validate_media_description_map(_, _), do: :error
|
||||||
|
|
||||||
@spec upload(Upload.source(), keyword()) :: {:ok, Object.t()} | {:error, any()}
|
@spec upload(Upload.source(), keyword()) :: {:ok, Object.t()} | {:error, any()}
|
||||||
def upload(file, opts \\ []) do
|
def upload(file, opts \\ []) do
|
||||||
with {_, :ok} <- {:description_map, validate_media_description_map(opts[:description_map])},
|
with {_, :ok} <-
|
||||||
|
{:description_map,
|
||||||
|
validate_media_description_map(opts[:description_map], opts[:language])},
|
||||||
{:ok, data} <- Upload.store(sanitize_upload_file(file), opts) do
|
{:ok, data} <- Upload.store(sanitize_upload_file(file), opts) do
|
||||||
obj_data = Maps.put_if_present(data, "actor", opts[:actor])
|
obj_data = Maps.put_if_present(data, "actor", opts[:actor])
|
||||||
|
|
||||||
Repo.insert(%Object{data: obj_data})
|
Repo.insert(%Object{data: obj_data})
|
||||||
else
|
else
|
||||||
{:description_map, _} -> {:error, dgettext("errors", "description_map invalid")}
|
{:description_map, :invalid_language} ->
|
||||||
e -> e
|
{:error, dgettext("errors", "valid language must be provided with description_map")}
|
||||||
|
|
||||||
|
{:description_map, _} ->
|
||||||
|
{:error, dgettext("errors", "description_map invalid")}
|
||||||
|
|
||||||
|
e ->
|
||||||
|
e
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -11,7 +11,6 @@ defmodule Pleroma.Web.ActivityPub.Builder do
|
||||||
|
|
||||||
alias Pleroma.Activity
|
alias Pleroma.Activity
|
||||||
alias Pleroma.Emoji
|
alias Pleroma.Emoji
|
||||||
alias Pleroma.MultiLanguage
|
|
||||||
alias Pleroma.Object
|
alias Pleroma.Object
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
alias Pleroma.Web.ActivityPub.Relay
|
alias Pleroma.Web.ActivityPub.Relay
|
||||||
|
|
|
@ -13,7 +13,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.EnsureRePrepended do
|
||||||
def history_awareness, do: :auto
|
def history_awareness, do: :auto
|
||||||
|
|
||||||
def filter_by_summary(
|
def filter_by_summary(
|
||||||
%{data: %{"summaryMap" => %{} = parent_summary_map}} = _in_reply_to,
|
%{data: %{"summaryMap" => %{} = parent_summary_map} = parent},
|
||||||
%{"summaryMap" => %{} = child_summary_map} = child
|
%{"summaryMap" => %{} = child_summary_map} = child
|
||||||
) do
|
) do
|
||||||
fixed_summary_map =
|
fixed_summary_map =
|
||||||
|
@ -25,9 +25,16 @@ def filter_by_summary(
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
fixed_summary =
|
||||||
|
with {:ok, fixed} <- fix_one(child["summary"], parent["summary"]) do
|
||||||
|
fixed
|
||||||
|
else
|
||||||
|
_ -> child["summary"]
|
||||||
|
end
|
||||||
|
|
||||||
child
|
child
|
||||||
|> Map.put("summaryMap", fixed_summary_map)
|
|> Map.put("summaryMap", fixed_summary_map)
|
||||||
|> Map.put("summary", Pleroma.MultiLanguage.map_to_str(fixed_summary_map, multiline: false))
|
|> Map.put("summary", fixed_summary)
|
||||||
end
|
end
|
||||||
|
|
||||||
def filter_by_summary(
|
def filter_by_summary(
|
||||||
|
|
|
@ -103,10 +103,7 @@ def filter(
|
||||||
|
|
||||||
object
|
object
|
||||||
|> Map.put("contentMap", fixed_content_map)
|
|> Map.put("contentMap", fixed_content_map)
|
||||||
|> Map.put(
|
|> Map.put("content", fix_content(object["content"] || "", mention_users))
|
||||||
"content",
|
|
||||||
Pleroma.MultiLanguage.map_to_str(fixed_content_map, multiline: true)
|
|
||||||
)
|
|
||||||
else
|
else
|
||||||
_ ->
|
_ ->
|
||||||
# image-only posts from pleroma apparently reach this MRF without the content field
|
# image-only posts from pleroma apparently reach this MRF without the content field
|
||||||
|
|
|
@ -88,16 +88,12 @@ defp check_replace(%{"object" => %{} = object} = message) do
|
||||||
config = Pleroma.Config.get([:mrf_keyword, :replace])
|
config = Pleroma.Config.get([:mrf_keyword, :replace])
|
||||||
|
|
||||||
replace_kw = fn object ->
|
replace_kw = fn object ->
|
||||||
[
|
["content", "name", "summary"]
|
||||||
{"content", [multiline: true]},
|
|> Enum.filter(fn field ->
|
||||||
{"name", [multiline: false]},
|
|
||||||
{"summary", [multiline: false]}
|
|
||||||
]
|
|
||||||
|> Enum.filter(fn {field, _} ->
|
|
||||||
is_map(object[field <> "Map"]) or
|
is_map(object[field <> "Map"]) or
|
||||||
(Map.has_key?(object, field) && object[field])
|
(Map.has_key?(object, field) && object[field])
|
||||||
end)
|
end)
|
||||||
|> Enum.reduce(object, fn {field, opts}, object ->
|
|> Enum.reduce(object, fn field, object ->
|
||||||
field_name_map = field <> "Map"
|
field_name_map = field <> "Map"
|
||||||
|
|
||||||
with %{} = data_map <- object[field_name_map] do
|
with %{} = data_map <- object[field_name_map] do
|
||||||
|
@ -108,7 +104,7 @@ defp check_replace(%{"object" => %{} = object} = message) do
|
||||||
|
|
||||||
object
|
object
|
||||||
|> Map.put(field_name_map, fixed_data_map)
|
|> Map.put(field_name_map, fixed_data_map)
|
||||||
|> Map.put(field, Pleroma.MultiLanguage.map_to_str(fixed_data_map, opts))
|
|> Map.put(field, replace_keyword(object[field], config))
|
||||||
else
|
else
|
||||||
_ ->
|
_ ->
|
||||||
data = replace_keyword(object[field], config)
|
data = replace_keyword(object[field], config)
|
||||||
|
|
|
@ -15,7 +15,7 @@ def history_awareness, do: :auto
|
||||||
def filter(
|
def filter(
|
||||||
%{
|
%{
|
||||||
"type" => type,
|
"type" => type,
|
||||||
"object" => %{"contentMap" => %{} = content_map, "attachment" => _} = _child_object
|
"object" => %{"contentMap" => %{} = content_map, "attachment" => _} = child_object
|
||||||
} = object
|
} = object
|
||||||
)
|
)
|
||||||
when type in ["Create", "Update"] do
|
when type in ["Create", "Update"] do
|
||||||
|
@ -28,6 +28,13 @@ def filter(
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
fixed_content =
|
||||||
|
if child_object["content"] in @placeholders do
|
||||||
|
""
|
||||||
|
else
|
||||||
|
child_object["content"]
|
||||||
|
end
|
||||||
|
|
||||||
fixed_object =
|
fixed_object =
|
||||||
if fixed_content_map == %{} do
|
if fixed_content_map == %{} do
|
||||||
Map.put(
|
Map.put(
|
||||||
|
@ -40,10 +47,7 @@ def filter(
|
||||||
else
|
else
|
||||||
object
|
object
|
||||||
|> put_in(["object", "contentMap"], fixed_content_map)
|
|> put_in(["object", "contentMap"], fixed_content_map)
|
||||||
|> put_in(
|
|> put_in(["object", "content"], fixed_content)
|
||||||
["object", "content"],
|
|
||||||
Pleroma.MultiLanguage.map_to_str(fixed_content_map, multiline: true)
|
|
||||||
)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
{:ok, fixed_object}
|
{:ok, fixed_object}
|
||||||
|
|
|
@ -25,10 +25,7 @@ def filter(%{"type" => type, "object" => child_object} = object)
|
||||||
|
|
||||||
object
|
object
|
||||||
|> put_in(["object", "contentMap"], fixed_content_map)
|
|> put_in(["object", "contentMap"], fixed_content_map)
|
||||||
|> put_in(
|
|> put_in(["object", "content"], HTML.filter_tags(child_object["content"], scrub_policy))
|
||||||
["object", "content"],
|
|
||||||
Pleroma.MultiLanguage.map_to_str(fixed_content_map, multiline: true)
|
|
||||||
)
|
|
||||||
else
|
else
|
||||||
_ ->
|
_ ->
|
||||||
content =
|
content =
|
||||||
|
|
|
@ -86,9 +86,6 @@ defp fix(data) do
|
||||||
|> fix_attachments()
|
|> fix_attachments()
|
||||||
|> CommonFixes.fix_quote_url()
|
|> CommonFixes.fix_quote_url()
|
||||||
|> Transmogrifier.fix_emoji()
|
|> Transmogrifier.fix_emoji()
|
||||||
|> CommonFixes.fix_multilang_field("content", "contentMap", multiline: true)
|
|
||||||
|> CommonFixes.fix_multilang_field("summary", "summaryMap", multiline: false)
|
|
||||||
|> CommonFixes.fix_multilang_field("name", "nameMap", multiline: false)
|
|
||||||
|> CommonFixes.maybe_add_language()
|
|> CommonFixes.maybe_add_language()
|
||||||
|> CommonFixes.maybe_add_content_map()
|
|> CommonFixes.maybe_add_content_map()
|
||||||
end
|
end
|
||||||
|
|
|
@ -102,9 +102,6 @@ defp fix(data) do
|
||||||
|> CommonFixes.fix_quote_url()
|
|> CommonFixes.fix_quote_url()
|
||||||
|> Transmogrifier.fix_emoji()
|
|> Transmogrifier.fix_emoji()
|
||||||
|> fix_url()
|
|> fix_url()
|
||||||
|> CommonFixes.fix_multilang_field("content", "contentMap", multiline: true)
|
|
||||||
|> CommonFixes.fix_multilang_field("summary", "summaryMap", multiline: false)
|
|
||||||
|> CommonFixes.fix_multilang_field("name", "nameMap", multiline: false)
|
|
||||||
|> fix_content()
|
|> fix_content()
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -194,13 +194,4 @@ def maybe_add_content_map(%{"language" => language, "content" => content} = obje
|
||||||
end
|
end
|
||||||
|
|
||||||
def maybe_add_content_map(object), do: object
|
def maybe_add_content_map(object), do: object
|
||||||
|
|
||||||
def fix_multilang_field(data, str_field, map_field, opts \\ []) do
|
|
||||||
with %{} = map <- data[map_field],
|
|
||||||
str when is_binary(str) <- Pleroma.MultiLanguage.map_to_str(map, opts) do
|
|
||||||
Map.put(data, str_field, str)
|
|
||||||
else
|
|
||||||
_ -> data
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
|
@ -8,7 +8,6 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsValidator do
|
||||||
import Ecto.Changeset
|
import Ecto.Changeset
|
||||||
|
|
||||||
alias Pleroma.EctoType.ActivityPub.ObjectValidators
|
alias Pleroma.EctoType.ActivityPub.ObjectValidators
|
||||||
alias Pleroma.Web.ActivityPub.ObjectValidators.CommonFixes
|
|
||||||
|
|
||||||
@primary_key false
|
@primary_key false
|
||||||
|
|
||||||
|
@ -25,15 +24,7 @@ defmodule Pleroma.Web.ActivityPub.ObjectValidators.QuestionOptionsValidator do
|
||||||
field(:type, :string, default: "Note")
|
field(:type, :string, default: "Note")
|
||||||
end
|
end
|
||||||
|
|
||||||
defp fix(data) do
|
|
||||||
data
|
|
||||||
# name is used in Answers, so better not change it
|
|
||||||
|> CommonFixes.fix_multilang_field("nameRendered", "nameMap", multiline: false)
|
|
||||||
end
|
|
||||||
|
|
||||||
def changeset(struct, data) do
|
def changeset(struct, data) do
|
||||||
data = fix(data)
|
|
||||||
|
|
||||||
struct
|
struct
|
||||||
|> cast(data, [:name, :nameRendered, :nameMap, :type])
|
|> cast(data, [:name, :nameRendered, :nameMap, :type])
|
||||||
|> cast_embed(:replies, with: &replies_changeset/2)
|
|> cast_embed(:replies, with: &replies_changeset/2)
|
||||||
|
|
|
@ -67,9 +67,6 @@ defp fix(data) do
|
||||||
|> CommonFixes.fix_quote_url()
|
|> CommonFixes.fix_quote_url()
|
||||||
|> Transmogrifier.fix_emoji()
|
|> Transmogrifier.fix_emoji()
|
||||||
|> fix_closed()
|
|> fix_closed()
|
||||||
|> CommonFixes.fix_multilang_field("content", "contentMap", multiline: true)
|
|
||||||
|> CommonFixes.fix_multilang_field("summary", "summaryMap", multiline: false)
|
|
||||||
|> CommonFixes.fix_multilang_field("name", "nameMap", multiline: false)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def changeset(struct, data) do
|
def changeset(struct, data) do
|
||||||
|
|
|
@ -52,6 +52,11 @@ defp create_request do
|
||||||
type: :string,
|
type: :string,
|
||||||
description: "A plain-text description of the media, for accessibility purposes."
|
description: "A plain-text description of the media, for accessibility purposes."
|
||||||
}),
|
}),
|
||||||
|
language: %Schema{
|
||||||
|
type: :string,
|
||||||
|
nullable: true,
|
||||||
|
description: "ISO 639 language code for this status."
|
||||||
|
},
|
||||||
focus: %Schema{
|
focus: %Schema{
|
||||||
type: :string,
|
type: :string,
|
||||||
description: "Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0."
|
description: "Two floating points (x,y), comma-delimited, ranging from -1.0 to 1.0."
|
||||||
|
|
|
@ -166,7 +166,11 @@ defp language(%{status: status} = draft) when is_binary(status) do
|
||||||
LanguageDetector.detect(draft.status <> " " <> (draft.summary || draft.params[:summary]))
|
LanguageDetector.detect(draft.status <> " " <> (draft.summary || draft.params[:summary]))
|
||||||
|
|
||||||
if MultiLanguage.good_locale_code?(detected_language) do
|
if MultiLanguage.good_locale_code?(detected_language) do
|
||||||
%__MODULE__{draft | language: detected_language}
|
%__MODULE__{
|
||||||
|
draft
|
||||||
|
| params: Map.put(draft.params, :language, detected_language),
|
||||||
|
language: detected_language
|
||||||
|
}
|
||||||
else
|
else
|
||||||
draft
|
draft
|
||||||
end
|
end
|
||||||
|
@ -529,9 +533,9 @@ defp validate(%{errors: [message | _]}), do: {:error, message}
|
||||||
defp differentiate_string_map(%{} = map), do: {nil, map}
|
defp differentiate_string_map(%{} = map), do: {nil, map}
|
||||||
defp differentiate_string_map(str) when is_binary(str), do: {str, nil}
|
defp differentiate_string_map(str) when is_binary(str), do: {str, nil}
|
||||||
|
|
||||||
defp get_source_map(%{status_map: %{} = status_map} = _draft) do
|
defp get_source_map(%{status_map: %{} = status_map} = draft) do
|
||||||
%{
|
%{
|
||||||
"content" => Pleroma.MultiLanguage.map_to_str(status_map, mutiline: true),
|
"content" => Map.get(status_map, draft.language),
|
||||||
"contentMap" => status_map
|
"contentMap" => status_map
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
|
@ -167,7 +167,7 @@ def make_poll_data(%{poll: %{options_map: options_map, expires_in: expires_in}}
|
||||||
%{"name" => option["und"]}
|
%{"name" => option["und"]}
|
||||||
else
|
else
|
||||||
%{
|
%{
|
||||||
"name" => MultiLanguage.map_to_str(option, multiline: false),
|
"name" => Map.get(option, data.language),
|
||||||
"nameMap" => option
|
"nameMap" => option
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
|
@ -5,6 +5,7 @@
|
||||||
defmodule Pleroma.Web.MastodonAPI.MediaController do
|
defmodule Pleroma.Web.MastodonAPI.MediaController do
|
||||||
use Pleroma.Web, :controller
|
use Pleroma.Web, :controller
|
||||||
|
|
||||||
|
alias Pleroma.MultiLanguage
|
||||||
alias Pleroma.Object
|
alias Pleroma.Object
|
||||||
alias Pleroma.Web.ActivityPub.ActivityPub
|
alias Pleroma.Web.ActivityPub.ActivityPub
|
||||||
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
alias Pleroma.Web.Plugs.OAuthScopesPlug
|
||||||
|
@ -24,17 +25,25 @@ def create(
|
||||||
conn,
|
conn,
|
||||||
_
|
_
|
||||||
) do
|
) do
|
||||||
with {:ok, object} <-
|
with language <- Map.get(data, :language),
|
||||||
|
{_, true} <-
|
||||||
|
{:valid_locale,
|
||||||
|
Map.get(data, :description_map) == nil or MultiLanguage.good_locale_code?(language)},
|
||||||
|
{:ok, object} <-
|
||||||
ActivityPub.upload(
|
ActivityPub.upload(
|
||||||
file,
|
file,
|
||||||
actor: user.ap_id,
|
actor: user.ap_id,
|
||||||
description: Map.get(data, :description),
|
description: Map.get(data, :description),
|
||||||
description_map: Map.get(data, :description_map)
|
description_map: Map.get(data, :description_map),
|
||||||
|
language: language
|
||||||
) do
|
) do
|
||||||
attachment_data = Map.put(object.data, "id", object.id)
|
attachment_data = Map.put(object.data, "id", object.id)
|
||||||
|
|
||||||
render(conn, "attachment.json", %{attachment: attachment_data})
|
render(conn, "attachment.json", %{attachment: attachment_data})
|
||||||
else
|
else
|
||||||
|
{:valid_locale, _} ->
|
||||||
|
render_error(conn, 422, "valid language must be provided with description_map")
|
||||||
|
|
||||||
{:error, e} ->
|
{:error, e} ->
|
||||||
conn
|
conn
|
||||||
|> put_status(:unprocessable_entity)
|
|> put_status(:unprocessable_entity)
|
||||||
|
@ -50,12 +59,17 @@ def create2(
|
||||||
conn,
|
conn,
|
||||||
_
|
_
|
||||||
) do
|
) do
|
||||||
with {:ok, object} <-
|
with language <- Map.get(data, :language),
|
||||||
|
{_, true} <-
|
||||||
|
{:valid_locale,
|
||||||
|
Map.get(data, :description_map) == nil or MultiLanguage.good_locale_code?(language)},
|
||||||
|
{:ok, object} <-
|
||||||
ActivityPub.upload(
|
ActivityPub.upload(
|
||||||
file,
|
file,
|
||||||
actor: user.ap_id,
|
actor: user.ap_id,
|
||||||
description: Map.get(data, :description),
|
description: Map.get(data, :description),
|
||||||
description_map: Map.get(data, :description_map)
|
description_map: Map.get(data, :description_map),
|
||||||
|
language: language
|
||||||
) do
|
) do
|
||||||
attachment_data = Map.put(object.data, "id", object.id)
|
attachment_data = Map.put(object.data, "id", object.id)
|
||||||
|
|
||||||
|
@ -63,6 +77,9 @@ def create2(
|
||||||
|> put_status(202)
|
|> put_status(202)
|
||||||
|> render("attachment.json", %{attachment: attachment_data})
|
|> render("attachment.json", %{attachment: attachment_data})
|
||||||
else
|
else
|
||||||
|
{:valid_locale, _} ->
|
||||||
|
render_error(conn, 422, "valid language must be provided with description_map")
|
||||||
|
|
||||||
{:error, e} ->
|
{:error, e} ->
|
||||||
conn
|
conn
|
||||||
|> put_status(:unprocessable_entity)
|
|> put_status(:unprocessable_entity)
|
||||||
|
@ -78,7 +95,7 @@ def update(
|
||||||
assigns: %{user: user},
|
assigns: %{user: user},
|
||||||
private: %{
|
private: %{
|
||||||
open_api_spex: %{
|
open_api_spex: %{
|
||||||
body_params: %{description_map: %{} = description_map},
|
body_params: %{description_map: %{} = description_map} = body_params,
|
||||||
params: %{id: id}
|
params: %{id: id}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,18 +104,24 @@ def update(
|
||||||
) do
|
) do
|
||||||
with %Object{} = object <- Object.get_by_id(id),
|
with %Object{} = object <- Object.get_by_id(id),
|
||||||
:ok <- Object.authorize_access(object, user),
|
:ok <- Object.authorize_access(object, user),
|
||||||
{_, {:ok, %{}}} <-
|
language = Map.get(body_params, :language, object["language"]),
|
||||||
{:description_map, Pleroma.MultiLanguage.validate_map(description_map)},
|
{_, true} <-
|
||||||
|
{:valid_locale, description_map == nil or MultiLanguage.good_locale_code?(language)},
|
||||||
|
{_, {:ok, %{}}} <- {:description_map, MultiLanguage.validate_map(description_map)},
|
||||||
{:ok, %Object{data: data}} <-
|
{:ok, %Object{data: data}} <-
|
||||||
Object.update_data(object, %{
|
Object.update_data(object, %{
|
||||||
"name" => Pleroma.MultiLanguage.map_to_str(description_map),
|
"name" => Map.get(description_map, language),
|
||||||
"nameMap" => description_map
|
"nameMap" => description_map
|
||||||
}) do
|
}) do
|
||||||
attachment_data = Map.put(data, "id", object.id)
|
attachment_data = Map.put(data, "id", object.id)
|
||||||
|
|
||||||
render(conn, "attachment.json", %{attachment: attachment_data})
|
render(conn, "attachment.json", %{attachment: attachment_data})
|
||||||
else
|
else
|
||||||
{:description_map, _} -> render_error(conn, 422, "description_map not valid")
|
{:valid_locale, _} ->
|
||||||
|
render_error(conn, 422, "valid language must be provided with description_map")
|
||||||
|
|
||||||
|
{:description_map, _} ->
|
||||||
|
render_error(conn, 422, "description_map not valid")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -7,42 +7,6 @@ defmodule Pleroma.MultiLanguageTest do
|
||||||
|
|
||||||
alias Pleroma.MultiLanguage
|
alias Pleroma.MultiLanguage
|
||||||
|
|
||||||
describe "map_to_str" do
|
|
||||||
setup do
|
|
||||||
%{
|
|
||||||
data: %{
|
|
||||||
"en-US" => "mew",
|
|
||||||
"en-GB" => "meow"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
test "single line", %{data: data} do
|
|
||||||
assert MultiLanguage.map_to_str(data) == "[en-GB] meow | [en-US] mew"
|
|
||||||
end
|
|
||||||
|
|
||||||
test "multi line", %{data: data} do
|
|
||||||
assert MultiLanguage.map_to_str(data, multiline: true) ==
|
|
||||||
"<div lang=\"en-GB\">meow</div><br><hr><br><div lang=\"en-US\">mew</div>"
|
|
||||||
end
|
|
||||||
|
|
||||||
test "only one language" do
|
|
||||||
data = %{"some" => "foo"}
|
|
||||||
assert MultiLanguage.map_to_str(data) == "foo"
|
|
||||||
assert MultiLanguage.map_to_str(data, multiline: true) == "foo"
|
|
||||||
end
|
|
||||||
|
|
||||||
test "resistent to tampering" do
|
|
||||||
data = %{
|
|
||||||
"en-US" => "mew {code} {content}",
|
|
||||||
"en-GB" => "meow {code} {content}"
|
|
||||||
}
|
|
||||||
|
|
||||||
assert MultiLanguage.map_to_str(data) ==
|
|
||||||
"[en-GB] meow {code} {content} | [en-US] mew {code} {content}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
describe "str_to_map" do
|
describe "str_to_map" do
|
||||||
test "" do
|
test "" do
|
||||||
assert MultiLanguage.str_to_map("foo") == %{"und" => "foo"}
|
assert MultiLanguage.str_to_map("foo") == %{"und" => "foo"}
|
||||||
|
|
|
@ -56,12 +56,11 @@ test "it adds `re:` to summaryMap object when child summary and parent summary f
|
||||||
"c" => "another-object-summary"
|
"c" => "another-object-summary"
|
||||||
}
|
}
|
||||||
|
|
||||||
assert res["object"]["summary"] ==
|
assert res["object"]["summary"] == "re: object-summary"
|
||||||
Pleroma.MultiLanguage.map_to_str(res["object"]["summaryMap"], multiline: false)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "it adds `re:` to summary object when child summary contains re-subject of parent summary " do
|
test "it adds `re:` to summary object when child summary contains re-subject of parent summary " do
|
||||||
s message = %{
|
message = %{
|
||||||
"type" => "Create",
|
"type" => "Create",
|
||||||
"object" => %{
|
"object" => %{
|
||||||
"summary" => "object-summary",
|
"summary" => "object-summary",
|
||||||
|
|
|
@ -123,11 +123,10 @@ test "supports mulitlang" do
|
||||||
%{
|
%{
|
||||||
"object" => %{
|
"object" => %{
|
||||||
"content" => content,
|
"content" => content,
|
||||||
"contentMap" =>
|
"contentMap" => %{
|
||||||
%{
|
"a" => content_a,
|
||||||
"a" => content_a,
|
"b" => content_b
|
||||||
"b" => content_b
|
}
|
||||||
} = content_map
|
|
||||||
}
|
}
|
||||||
}} = ForceMentionsInContent.filter(activity)
|
}} = ForceMentionsInContent.filter(activity)
|
||||||
|
|
||||||
|
@ -136,7 +135,7 @@ test "supports mulitlang" do
|
||||||
|
|
||||||
assert content_a == mentions_part <> "mew mew"
|
assert content_a == mentions_part <> "mew mew"
|
||||||
assert content_b == mentions_part <> "lol lol"
|
assert content_b == mentions_part <> "lol lol"
|
||||||
assert content == Pleroma.MultiLanguage.map_to_str(content_map, multiline: true)
|
assert content == mentions_part <> "WHA-HA!"
|
||||||
end
|
end
|
||||||
|
|
||||||
test "don't mention self" do
|
test "don't mention self" do
|
||||||
|
|
|
@ -318,23 +318,16 @@ test "replaces keyword in *Map" do
|
||||||
{:ok,
|
{:ok,
|
||||||
%{
|
%{
|
||||||
"object" => %{
|
"object" => %{
|
||||||
"content" => content,
|
"contentMap" => %{
|
||||||
"contentMap" =>
|
"a" => "ZFS is free software",
|
||||||
%{
|
"b" => "mew mew is also free software"
|
||||||
"a" => "ZFS is free software",
|
},
|
||||||
"b" => "mew mew is also free software"
|
"summaryMap" => %{
|
||||||
} = content_map,
|
"a" => "ZFS is very free software",
|
||||||
"summary" => summary,
|
"b" => "mew mew is also very free software"
|
||||||
"summaryMap" =>
|
}
|
||||||
%{
|
|
||||||
"a" => "ZFS is very free software",
|
|
||||||
"b" => "mew mew is also very free software"
|
|
||||||
} = summary_map
|
|
||||||
}
|
}
|
||||||
}} = KeywordPolicy.filter(message)
|
}} = KeywordPolicy.filter(message)
|
||||||
|
|
||||||
assert content == Pleroma.MultiLanguage.map_to_str(content_map, multiline: true)
|
|
||||||
assert summary == Pleroma.MultiLanguage.map_to_str(summary_map, multiline: false)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "replaces keyword if string matches in history" do
|
test "replaces keyword if string matches in history" do
|
||||||
|
|
|
@ -32,7 +32,7 @@ test "multilang aware" do
|
||||||
}
|
}
|
||||||
|
|
||||||
assert {:ok, res} = NoPlaceholderTextPolicy.filter(message)
|
assert {:ok, res} = NoPlaceholderTextPolicy.filter(message)
|
||||||
assert res["object"]["content"] == "lol"
|
assert res["object"]["content"] == ""
|
||||||
assert res["object"]["contentMap"] == %{"b" => "lol"}
|
assert res["object"]["contentMap"] == %{"b" => "lol"}
|
||||||
|
|
||||||
message = %{
|
message = %{
|
||||||
|
|
|
@ -53,8 +53,7 @@ test "multilang-aware" do
|
||||||
assert {:ok, res} = NormalizeMarkup.filter(message)
|
assert {:ok, res} = NormalizeMarkup.filter(message)
|
||||||
assert res["object"]["contentMap"] == %{"a" => @expected, "b" => @expected}
|
assert res["object"]["contentMap"] == %{"a" => @expected, "b" => @expected}
|
||||||
|
|
||||||
assert res["object"]["content"] ==
|
assert res["object"]["content"] == "some"
|
||||||
Pleroma.MultiLanguage.map_to_str(res["object"]["contentMap"], multiline: true)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
test "history-aware" do
|
test "history-aware" do
|
||||||
|
|
|
@ -54,14 +54,9 @@ test "Note with contentMap and summaryMap", %{note: note} do
|
||||||
|> Map.put("summaryMap", summary_map)
|
|> Map.put("summaryMap", summary_map)
|
||||||
|> Map.put("contentMap", content_map)
|
|> Map.put("contentMap", content_map)
|
||||||
|
|
||||||
expected_summary = Pleroma.MultiLanguage.map_to_str(summary_map, multiline: false)
|
|
||||||
expected_content = Pleroma.MultiLanguage.map_to_str(content_map, multiline: true)
|
|
||||||
|
|
||||||
assert %{
|
assert %{
|
||||||
valid?: true,
|
valid?: true,
|
||||||
changes: %{
|
changes: %{
|
||||||
summary: ^expected_summary,
|
|
||||||
content: ^expected_content,
|
|
||||||
summaryMap: ^summary_map,
|
summaryMap: ^summary_map,
|
||||||
contentMap: ^content_map
|
contentMap: ^content_map
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue