Fix filtering out incorrect addresses

This commit is contained in:
FloatingGhost 2023-05-23 13:46:25 +01:00
parent 037f881187
commit 8c208f751d
2 changed files with 29 additions and 10 deletions

View file

@ -2,7 +2,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.DirectMessageDisabledPolicy do
@behaviour Pleroma.Web.ActivityPub.MRF.Policy @behaviour Pleroma.Web.ActivityPub.MRF.Policy
alias Pleroma.User alias Pleroma.User
alias Pleroma.Web.ActivityPub.Visibility require Pleroma.Constants
@moduledoc """ @moduledoc """
Removes entries from the "To" field from direct messages if the user has requested to not Removes entries from the "To" field from direct messages if the user has requested to not
@ -13,15 +13,19 @@ defmodule Pleroma.Web.ActivityPub.MRF.DirectMessageDisabledPolicy do
def filter( def filter(
%{ %{
"type" => "Create", "type" => "Create",
"actor" => actor "actor" => actor,
"object" => %{
"type" => "Note"
}
} = activity } = activity
) do ) do
with true <- Visibility.is_direct?(%{data: activity}), with recipients <- Map.get(activity, "to", []),
recipients <- Map.get(activity, "to"), cc <- Map.get(activity, "cc", []),
true <- is_direct?(recipients, cc),
sender <- User.get_cached_by_ap_id(actor) do sender <- User.get_cached_by_ap_id(actor) do
new_to = new_to =
Enum.filter(recipients, fn recv -> Enum.filter(recipients, fn recv ->
should_filter?(sender, recv) should_include?(sender, recv)
end) end)
{:ok, {:ok,
@ -40,11 +44,11 @@ defmodule Pleroma.Web.ActivityPub.MRF.DirectMessageDisabledPolicy do
@impl true @impl true
def describe, do: {:ok, %{}} def describe, do: {:ok, %{}}
defp should_filter?(sender, receiver_ap_id) do defp should_include?(sender, receiver_ap_id) do
with %User{local: true} = receiver <- User.get_cached_by_ap_id(receiver_ap_id) do with %User{local: true} = receiver <- User.get_cached_by_ap_id(receiver_ap_id) do
User.accepts_direct_messages?(receiver, sender) User.accepts_direct_messages?(receiver, sender)
else else
_ -> false _ -> true
end end
end end
@ -53,4 +57,9 @@ defmodule Pleroma.Web.ActivityPub.MRF.DirectMessageDisabledPolicy do
end end
defp maybe_replace_object_to(other, _), do: other defp maybe_replace_object_to(other, _), do: other
defp is_direct?(to, cc) do
!(Enum.member?(to, Pleroma.Constants.as_public()) ||
Enum.member?(cc, Pleroma.Constants.as_public()))
end
end end

View file

@ -16,10 +16,15 @@ defmodule Pleroma.Web.ActivityPub.MRF.DirectMessageDisabledPolicyTest do
"actor" => sender.ap_id, "actor" => sender.ap_id,
"to" => [recipient.ap_id], "to" => [recipient.ap_id],
"cc" => [], "cc" => [],
"type" => "Create" "type" => "Create",
"object" => %{
"type" => "Note",
"to" => [recipient.ap_id]
}
} }
assert {:ok, %{"to" => []}} = DirectMessageDisabledPolicy.filter(message) assert {:ok, %{"to" => [], "object" => %{"to" => []}}} =
DirectMessageDisabledPolicy.filter(message)
end end
test "when the user does not deny the direct message" do test "when the user does not deny the direct message" do
@ -32,11 +37,16 @@ defmodule Pleroma.Web.ActivityPub.MRF.DirectMessageDisabledPolicyTest do
"actor" => sender.ap_id, "actor" => sender.ap_id,
"to" => [recipient.ap_id], "to" => [recipient.ap_id],
"cc" => [], "cc" => [],
"type" => "Create" "type" => "Create",
"object" => %{
"type" => "Note",
"to" => [recipient.ap_id]
}
} }
assert {:ok, message} = DirectMessageDisabledPolicy.filter(message) assert {:ok, message} = DirectMessageDisabledPolicy.filter(message)
assert message["to"] == [recipient.ap_id] assert message["to"] == [recipient.ap_id]
assert message["object"]["to"] == [recipient.ap_id]
end end
end end
end end