Notices where this attachment appears
-
Embed this notice
@SamBox9029
defmodule Pleroma.Web.ActivityPub.MRF.SpamPolicy do
@moduledoc "Spam policy"
@behaviour Pleroma.Web.ActivityPub.MRF.Policy
require Logger
defp string_matches?(string, _) when not is_binary(string) do
false
end
defp string_matches?(string, pattern) when is_binary(pattern) do
String.contains?(string, pattern)
end
defp string_matches?(string, pattern) do
String.match?(string, pattern)
end
@impl true
def filter(
%{
"type" => "Note",
"actor" => actor
} = message
) do
mentions =
message["tag"]
|> Enum.filter(fn m -> m["type"] == "Mention" end)
|> Enum.map(fn m -> m["href"] end)
recipients =
(((message["to"] || []) ++ (message["cc"] || [])) ++ (mentions || []))
|> Enum.reject(&is_nil/1)
|> Enum.uniq()
|> Enum.reject(fn apid ->
apid == "https://www.w3.org/ns/activitystreams#Public" ||
String.ends_with?(apid, "/followers")
end)
actor_patterns = Pleroma.Config.get([:mrf_spam, :actor_patterns], [~r/\/[a-zA-Z0-9]{10}$/])
actor_age = Pleroma.Config.get([:mrf_spam, :actor_age], 86400)
with true <- Enum.any?(actor_patterns, fn pattern -> string_matches?(actor, pattern) end),
true <- length(recipients) >= Pleroma.Config.get([:mrf_spam, :mentions], 5),
user <- Pleroma.User.get_cached_by_ap_id(actor),
true <- user.follower_count == 0,
true <- user.following_count == 0,
true <- System.system_time(:second) - Timex.to_unix(user.inserted_at) < actor_age do
Logger.info("Reject SPAM message from #{inspect(message)}")
{:reject, message}
else
_ -> {:ok, message}
end
end
@impl true
def filter(object), do: {:ok, object}
@impl true
def describe, do: {:ok, %{}}
@impl true
def config_description do
%{
key: :mrf_spam,
related_policy: "Pleroma.Web.ActivityPub.MRF.SpamPolicy",
label: "MRF SPAM",
description:
"Reject or Word-Replace SPAM messages matching a keyword or [Regex](https://hexdocs.pm/elixir/Regex.html).",
children: [
%{
key: :actor_patterns,
type: {:list, :string},
description: """
A list of actor patterns which result in message being rejected.
Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
""",
suggestions: ["foo", ~r/\/[a-zA-Z0-9]{10}$/]
},
%{
key: :mentions,
type: :integer,
description: "Minimum mentions to trigger policy, excluding public/followers scopes",
suggestions: [5]
},
%{
key: :actor_age,
type: :integer,
description: "User account age, as seen by local instance, in seconds",
suggestions: [86400]
}
]
}
end
end