Make keyword_policy multilang-aware

This commit is contained in:
tusooa 2023-01-15 14:49:45 -05:00 committed by marcin mikołajczak
parent 2d66faf9a4
commit 9354ee31d2
2 changed files with 79 additions and 11 deletions

View file

@ -85,18 +85,36 @@ defp check_ftl_removal(message) do
end
defp check_replace(%{"object" => %{} = object} = message) do
replace_kw = fn object ->
["content", "name", "summary"]
|> Enum.filter(fn field -> Map.has_key?(object, field) && object[field] end)
|> Enum.reduce(object, fn field, object ->
data =
Enum.reduce(
Pleroma.Config.get([:mrf_keyword, :replace]),
object[field],
fn {pat, repl}, acc -> String.replace(acc, pat, repl) end
)
config = Pleroma.Config.get([:mrf_keyword, :replace])
Map.put(object, field, data)
replace_kw = fn object ->
[
{"content", [multiline: true]},
{"name", [multiline: false]},
{"summary", [multiline: false]}
]
|> Enum.filter(fn {field, _} ->
is_map(object[field <> "Map"]) or
(Map.has_key?(object, field) && object[field])
end)
|> Enum.reduce(object, fn {field, opts}, object ->
field_name_map = field <> "Map"
with %{} = data_map <- object[field_name_map] do
fixed_data_map =
Enum.reduce(data_map, %{}, fn {lang, content}, acc ->
Map.put(acc, lang, replace_keyword(content, config))
end)
object
|> Map.put(field_name_map, fixed_data_map)
|> Map.put(field, Pleroma.MultiLanguage.map_to_str(fixed_data_map, opts))
else
_ ->
data = replace_keyword(object[field], config)
Map.put(object, field, data)
end
end)
|> (fn object -> {:ok, object} end).()
end
@ -108,6 +126,14 @@ defp check_replace(%{"object" => %{} = object} = message) do
{:ok, message}
end
defp replace_keyword(data, config) do
Enum.reduce(
config,
data,
fn {pat, repl}, acc -> String.replace(acc, pat, repl) end
)
end
@impl true
def filter(%{"type" => type, "object" => %{"content" => _content}} = message)
when type in ["Create", "Update"] do

View file

@ -295,6 +295,48 @@ test "replaces keyword if regex matches in summary" do
end)
end
test "replaces keyword in *Map" do
clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}])
message = %{
"type" => "Create",
"to" => ["https://www.w3.org/ns/activitystreams#Public"],
"object" => %{
"content" => "unrelevant",
"contentMap" => %{
"a" => "ZFS is opensource",
"b" => "mew mew is also opensource"
},
"summary" => "unrelevant",
"summaryMap" => %{
"a" => "ZFS is very opensource",
"b" => "mew mew is also very opensource"
}
}
}
{:ok,
%{
"object" => %{
"content" => content,
"contentMap" =>
%{
"a" => "ZFS is free software",
"b" => "mew mew is also free software"
} = content_map,
"summary" => summary,
"summaryMap" =>
%{
"a" => "ZFS is very free software",
"b" => "mew mew is also very free software"
} = summary_map
}
}} = 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
test "replaces keyword if string matches in history" do
clear_config([:mrf_keyword, :replace], [{"opensource", "free software"}])