Fix edge cases

This commit is contained in:
tusooa 2023-06-20 10:14:01 -04:00
parent 0d914e17be
commit ba3aa4f86d
No known key found for this signature in database
GPG Key ID: 42AEC43D48433C51
3 changed files with 87 additions and 37 deletions

View File

@ -42,6 +42,18 @@ defmodule Pleroma.Constants do
] ]
) )
const(status_object_types,
do: [
"Note",
"Question",
"Audio",
"Video",
"Event",
"Article",
"Page"
]
)
const(updatable_object_types, const(updatable_object_types,
do: [ do: [
"Note", "Note",

View File

@ -31,8 +31,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
def history_awareness, do: :manual def history_awareness, do: :manual
@impl Pleroma.Web.ActivityPub.MRF.Policy @impl Pleroma.Web.ActivityPub.MRF.Policy
def filter(%{"type" => type, "object" => %{} = object} = message) def filter(%{"type" => type, "object" => %{"type" => objtype} = object} = message)
when type in ["Create", "Update"] do when type in ["Create", "Update"] and objtype in Pleroma.Constants.status_object_types() do
with {:ok, object} <- with {:ok, object} <-
Updater.do_with_history(object, fn object -> Updater.do_with_history(object, fn object ->
{:ok, process_remove(object, :url, config_remove_url())} {:ok, process_remove(object, :url, config_remove_url())}
@ -102,7 +102,11 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
end end
defp process_remove_impl(object, extract_from_tag, extract_from_emoji, patterns) do defp process_remove_impl(object, extract_from_tag, extract_from_emoji, patterns) do
processed_tag = object =
if object["tag"] do
Map.put(
object,
"tag",
Enum.filter( Enum.filter(
object["tag"], object["tag"],
fn fn
@ -119,9 +123,16 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
true true
end end
) )
)
else
object
end
processed_emoji = object =
if object["emoji"] do if object["emoji"] do
Map.put(
object,
"emoji",
object["emoji"] object["emoji"]
|> Enum.reduce(%{}, fn {name, url} = emoji, acc -> |> Enum.reduce(%{}, fn {name, url} = emoji, acc ->
if not match_any?(extract_from_emoji.(emoji), patterns) do if not match_any?(extract_from_emoji.(emoji), patterns) do
@ -130,18 +141,12 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
acc acc
end end
end) end)
)
else else
nil object
end end
if processed_emoji do
object object
|> Map.put("tag", processed_tag)
|> Map.put("emoji", processed_emoji)
else
object
|> Map.put("tag", processed_tag)
end
end end
defp matched_emoji_checker(urls, shortcodes) do defp matched_emoji_checker(urls, shortcodes) do

View File

@ -389,4 +389,37 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicyTest do
} = filtered } = filtered
end end
end end
describe "edge cases" do
setup do
clear_config([:mrf_emoji, :remove_url], [
"https://example.org/test.png",
~r{/biribiri/mikoto_smile[23]\.png},
"nekomimi_girl_emoji"
])
:ok
end
test "non-statuses" do
answer = @status_data |> put_in(["object", "type"], "Answer")
{:ok, filtered} = MRF.filter_one(EmojiPolicy, answer)
assert filtered == answer
end
test "without tag" do
status = @status_data |> Map.put("object", Map.drop(@status_data["object"], ["tag"]))
{:ok, filtered} = MRF.filter_one(EmojiPolicy, status)
refute Map.has_key?(filtered["object"], "tag")
end
test "without emoji" do
status = @status_data |> Map.put("object", Map.drop(@status_data["object"], ["emoji"]))
{:ok, filtered} = MRF.filter_one(EmojiPolicy, status)
refute Map.has_key?(filtered["object"], "emoji")
end
end
end end