Make ensure_re_prepended multilang-aware

Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
This commit is contained in:
tusooa 2023-01-15 14:11:15 -05:00 committed by marcin mikołajczak
parent 792e495180
commit cd0260f309
2 changed files with 74 additions and 6 deletions

View file

@ -12,18 +12,34 @@ defmodule Pleroma.Web.ActivityPub.MRF.EnsureRePrepended do
def history_awareness, do: :auto
def filter_by_summary(
%{data: %{"summaryMap" => %{} = parent_summary_map}} = _in_reply_to,
%{"summaryMap" => %{} = child_summary_map} = child
) do
fixed_summary_map =
Enum.reduce(child_summary_map, %{}, fn {lang, cur}, acc ->
with {:ok, fixed_cur} <- fix_one(cur, parent_summary_map[lang]) do
Map.put(acc, lang, fixed_cur)
else
_ -> Map.put(acc, lang, cur)
end
end)
child
|> Map.put("summaryMap", fixed_summary_map)
|> Map.put("summary", Pleroma.MultiLanguage.map_to_str(fixed_summary_map, multiline: false))
end
def filter_by_summary(
%{data: %{"summary" => parent_summary}} = _in_reply_to,
%{"summary" => child_summary} = child
)
when not is_nil(child_summary) and byte_size(child_summary) > 0 and
not is_nil(parent_summary) and byte_size(parent_summary) > 0 do
if (child_summary == parent_summary and not Regex.match?(@reply_prefix, child_summary)) or
(Regex.match?(@reply_prefix, parent_summary) &&
Regex.replace(@reply_prefix, parent_summary, "") == child_summary) do
Map.put(child, "summary", "re: " <> child_summary)
with {:ok, fixed_child_summary} <- fix_one(child_summary, parent_summary) do
Map.put(child, "summary", fixed_child_summary)
else
child
_ -> child
end
end
@ -44,4 +60,20 @@ def filter(%{"type" => type, "object" => child_object} = object)
def filter(object), do: {:ok, object}
def describe, do: {:ok, %{}}
defp fix_one(child_summary, parent_summary)
when is_binary(child_summary) and child_summary != "" and is_binary(parent_summary) and
parent_summary != "" do
if (child_summary == parent_summary and not Regex.match?(@reply_prefix, child_summary)) or
(Regex.match?(@reply_prefix, parent_summary) &&
Regex.replace(@reply_prefix, parent_summary, "") == child_summary) do
{:ok, "re: " <> child_summary}
else
{:nochange, nil}
end
end
defp fix_one(_, _) do
{:nochange, nil}
end
end

View file

@ -24,8 +24,44 @@ test "it adds `re:` to summary object when child summary and parent summary equa
assert res["object"]["summary"] == "re: object-summary"
end
test "it adds `re:` to summary object when child summary contains re-subject of parent summary " do
test "it adds `re:` to summaryMap object when child summary and parent summary for some language equal" do
message = %{
"type" => "Create",
"object" => %{
"summary" => "object-summary",
"summaryMap" => %{
"a" => "object-summary",
"b" => "some-object-summary",
"c" => "another-object-summary"
},
"inReplyTo" => %Activity{
object: %Object{
data: %{
"summary" => "object-summary",
"summaryMap" => %{
"a" => "unrelated-summary",
"b" => "some-object-summary"
}
}
}
}
}
}
assert {:ok, res} = EnsureRePrepended.filter(message)
assert res["object"]["summaryMap"] == %{
"a" => "object-summary",
"b" => "re: some-object-summary",
"c" => "another-object-summary"
}
assert res["object"]["summary"] ==
Pleroma.MultiLanguage.map_to_str(res["object"]["summaryMap"], multiline: false)
end
test "it adds `re:` to summary object when child summary contains re-subject of parent summary " do
s message = %{
"type" => "Create",
"object" => %{
"summary" => "object-summary",