Skip to content

Commit

Permalink
Merge branch 'mrf-cleanup' into 'develop'
Browse files Browse the repository at this point in the history
MRF cleanup

See merge request pleroma/pleroma!4219
  • Loading branch information
feld committed Aug 13, 2024
2 parents 7388c4b + 2ba5ad8 commit ccf476a
Show file tree
Hide file tree
Showing 33 changed files with 376 additions and 375 deletions.
Empty file added changelog.d/mrf-cleanup.skip
Empty file.
8 changes: 4 additions & 4 deletions lib/pleroma/web/activity_pub/mrf/anti_followbot_policy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,20 +63,20 @@ defmodule Pleroma.Web.ActivityPub.MRF.AntiFollowbotPolicy do
end

@impl true
def filter(%{"type" => "Follow", "actor" => actor_id} = message) do
def filter(%{"type" => "Follow", "actor" => actor_id} = activity) do
%User{} = actor = normalize_by_ap_id(actor_id)

score = determine_if_followbot(actor)

if score < 0.8 || bot_allowed?(message, actor) do
{:ok, message}
if score < 0.8 || bot_allowed?(activity, actor) do
{:ok, activity}
else
{:reject, "[AntiFollowbotPolicy] Scored #{actor_id} as #{score}"}
end
end

@impl true
def filter(message), do: {:ok, message}
def filter(activity), do: {:ok, activity}

@impl true
def describe, do: {:ok, %{}}
Expand Down
10 changes: 5 additions & 5 deletions lib/pleroma/web/activity_pub/mrf/anti_link_spam_policy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ defmodule Pleroma.Web.ActivityPub.MRF.AntiLinkSpamPolicy do
defp contains_links?(_), do: false

@impl true
def filter(%{"type" => "Create", "actor" => actor, "object" => object} = message) do
def filter(%{"type" => "Create", "actor" => actor, "object" => object} = activity) do
with {:ok, %User{local: false} = u} <- User.get_or_fetch_by_ap_id(actor),
{:contains_links, true} <- {:contains_links, contains_links?(object)},
{:old_user, true} <- {:old_user, old_user?(u)} do
{:ok, message}
{:ok, activity}
else
{:ok, %User{local: true}} ->
{:ok, message}
{:ok, activity}

{:contains_links, false} ->
{:ok, message}
{:ok, activity}

{:old_user, false} ->
{:reject, "[AntiLinkSpamPolicy] User has no posts nor followers"}
Expand All @@ -53,7 +53,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.AntiLinkSpamPolicy do
end

# in all other cases, pass through
def filter(message), do: {:ok, message}
def filter(activity), do: {:ok, activity}

@impl true
def describe, do: {:ok, %{}}
Expand Down
8 changes: 4 additions & 4 deletions lib/pleroma/web/activity_pub/mrf/anti_mention_spam_policy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ defmodule Pleroma.Web.ActivityPub.MRF.AntiMentionSpamPolicy do
end

# copied from HellthreadPolicy
defp get_recipient_count(message) do
recipients = (message["to"] || []) ++ (message["cc"] || [])
defp get_recipient_count(activity) do
recipients = (activity["to"] || []) ++ (activity["cc"] || [])

follower_collection =
User.get_cached_by_ap_id(message["actor"] || message["attributedTo"]).follower_address
User.get_cached_by_ap_id(activity["actor"] || activity["attributedTo"]).follower_address

if Enum.member?(recipients, Pleroma.Constants.as_public()) do
recipients =
Expand Down Expand Up @@ -80,7 +80,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.AntiMentionSpamPolicy do
end

# in all other cases, pass through
def filter(message), do: {:ok, message}
def filter(activity), do: {:ok, activity}

@impl true
def describe, do: {:ok, %{}}
Expand Down
14 changes: 7 additions & 7 deletions lib/pleroma/web/activity_pub/mrf/dnsrbl_policy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,18 @@ defmodule Pleroma.Web.ActivityPub.MRF.DNSRBLPolicy do
@query_timeout 500

@impl true
def filter(%{"actor" => actor} = object) do
def filter(%{"actor" => actor} = activity) do
actor_info = URI.parse(actor)

with {:ok, object} <- check_rbl(actor_info, object) do
{:ok, object}
with {:ok, activity} <- check_rbl(actor_info, activity) do
{:ok, activity}
else
_ -> {:reject, "[DNSRBLPolicy]"}
end
end

@impl true
def filter(object), do: {:ok, object}
def filter(activity), do: {:ok, activity}

@impl true
def describe do
Expand Down Expand Up @@ -90,7 +90,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.DNSRBLPolicy do
}
end

defp check_rbl(%{host: actor_host}, object) do
defp check_rbl(%{host: actor_host}, activity) do
with false <- match?(^actor_host, Pleroma.Web.Endpoint.host()),
zone when not is_nil(zone) <- Keyword.get(Config.get([:mrf_dnsrbl]), :zone) do
query =
Expand All @@ -100,7 +100,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.DNSRBLPolicy do
rbl_response = rblquery(query)

if Enum.empty?(rbl_response) do
{:ok, object}
{:ok, activity}
else
Task.start(fn ->
reason =
Expand All @@ -117,7 +117,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.DNSRBLPolicy do
:error
end
else
_ -> {:ok, object}
_ -> {:ok, activity}
end
end

Expand Down
6 changes: 3 additions & 3 deletions lib/pleroma/web/activity_pub/mrf/drop_policy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ defmodule Pleroma.Web.ActivityPub.MRF.DropPolicy do
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

@impl true
def filter(object) do
Logger.debug("REJECTING #{inspect(object)}")
{:reject, object}
def filter(activity) do
Logger.debug("REJECTING #{inspect(activity)}")
{:reject, activity}
end

@impl true
Expand Down
30 changes: 15 additions & 15 deletions lib/pleroma/web/activity_pub/mrf/emoji_policy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
Pleroma.Config.get([:mrf_emoji, :federated_timeline_removal_shortcode], [])
end

@impl Pleroma.Web.ActivityPub.MRF.Policy
@impl true
def history_awareness, do: :manual

@impl Pleroma.Web.ActivityPub.MRF.Policy
def filter(%{"type" => type, "object" => %{"type" => objtype} = object} = message)
@impl true
def filter(%{"type" => type, "object" => %{"type" => objtype} = object} = activity)
when type in ["Create", "Update"] and objtype in Pleroma.Constants.status_object_types() do
with {:ok, object} <-
Updater.do_with_history(object, fn object ->
Expand All @@ -42,21 +42,21 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
Updater.do_with_history(object, fn object ->
{:ok, process_remove(object, :shortcode, config_remove_shortcode())}
end),
activity <- Map.put(message, "object", object),
activity <- Map.put(activity, "object", object),
activity <- maybe_delist(activity) do
{:ok, activity}
end
end

@impl Pleroma.Web.ActivityPub.MRF.Policy
@impl true
def filter(%{"type" => type} = object) when type in Pleroma.Constants.actor_types() do
with object <- process_remove(object, :url, config_remove_url()),
object <- process_remove(object, :shortcode, config_remove_shortcode()) do
{:ok, object}
end
end

@impl Pleroma.Web.ActivityPub.MRF.Policy
@impl true
def filter(%{"type" => "EmojiReact"} = object) do
with {:ok, _} <-
matched_emoji_checker(config_remove_url(), config_remove_shortcode()).(object) do
Expand All @@ -67,9 +67,9 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
end
end

@impl Pleroma.Web.ActivityPub.MRF.Policy
def filter(message) do
{:ok, message}
@impl true
def filter(activity) do
{:ok, activity}
end

defp match_string?(string, pattern) when is_binary(pattern) do
Expand Down Expand Up @@ -214,7 +214,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
)
end

@impl Pleroma.Web.ActivityPub.MRF.Policy
@impl true
def describe do
mrf_emoji =
Pleroma.Config.get(:mrf_emoji, [])
Expand All @@ -226,7 +226,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
{:ok, %{mrf_emoji: mrf_emoji}}
end

@impl Pleroma.Web.ActivityPub.MRF.Policy
@impl true
def config_description do
%{
key: :mrf_emoji,
Expand All @@ -239,7 +239,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
key: :remove_url,
type: {:list, :string},
description: """
A list of patterns which result in emoji whose URL matches being removed from the message. This will apply to statuses, emoji reactions, and user profiles.
A list of patterns which result in emoji whose URL matches being removed from the activity. This will apply to statuses, emoji reactions, and user profiles.
Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
""",
Expand All @@ -249,7 +249,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
key: :remove_shortcode,
type: {:list, :string},
description: """
A list of patterns which result in emoji whose shortcode matches being removed from the message. This will apply to statuses, emoji reactions, and user profiles.
A list of patterns which result in emoji whose shortcode matches being removed from the activity. This will apply to statuses, emoji reactions, and user profiles.
Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
""",
Expand All @@ -259,7 +259,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
key: :federated_timeline_removal_url,
type: {:list, :string},
description: """
A list of patterns which result in message with emojis whose URLs match being removed from federated timelines (a.k.a unlisted). This will apply only to statuses.
A list of patterns which result in activity with emojis whose URLs match being removed from federated timelines (a.k.a unlisted). This will apply only to statuses.
Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
""",
Expand All @@ -269,7 +269,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
key: :federated_timeline_removal_shortcode,
type: {:list, :string},
description: """
A list of patterns which result in message with emojis whose shortcodes match being removed from federated timelines (a.k.a unlisted). This will apply only to statuses.
A list of patterns which result in activities with emojis whose shortcodes match being removed from federated timelines (a.k.a unlisted). This will apply only to statuses.
Each pattern can be a string or [Regex](https://hexdocs.pm/elixir/Regex.html) in the format of `~r/PATTERN/`.
""",
Expand Down
14 changes: 7 additions & 7 deletions lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,19 @@ defmodule Pleroma.Web.ActivityPub.MRF.EnsureRePrepended do

def filter_by_summary(_in_reply_to, child), do: child

def filter(%{"type" => type, "object" => child_object} = object)
when type in ["Create", "Update"] and is_map(child_object) do
def filter(%{"type" => type, "object" => object} = activity)
when type in ["Create", "Update"] and is_map(object) do
child =
child_object["inReplyTo"]
object["inReplyTo"]
|> Object.normalize(fetch: false)
|> filter_by_summary(child_object)
|> filter_by_summary(object)

object = Map.put(object, "object", child)
activity = Map.put(activity, "object", child)

{:ok, object}
{:ok, activity}
end

def filter(object), do: {:ok, object}
def filter(activity), do: {:ok, activity}

def describe, do: {:ok, %{}}
end
20 changes: 10 additions & 10 deletions lib/pleroma/web/activity_pub/mrf/follow_bot_policy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,30 @@ defmodule Pleroma.Web.ActivityPub.MRF.FollowBotPolicy do
require Logger

@impl true
def filter(message) do
def filter(activity) do
with follower_nickname <- Config.get([:mrf_follow_bot, :follower_nickname]),
%User{actor_type: "Service"} = follower <-
User.get_cached_by_nickname(follower_nickname),
%{"type" => "Create", "object" => %{"type" => "Note"}} <- message do
try_follow(follower, message)
%{"type" => "Create", "object" => %{"type" => "Note"}} <- activity do
try_follow(follower, activity)
else
nil ->
Logger.warning(
"#{__MODULE__} skipped because of missing `:mrf_follow_bot, :follower_nickname` configuration, the :follower_nickname
account does not exist, or the account is not correctly configured as a bot."
)

{:ok, message}
{:ok, activity}

_ ->
{:ok, message}
{:ok, activity}
end
end

defp try_follow(follower, message) do
to = Map.get(message, "to", [])
cc = Map.get(message, "cc", [])
actor = [message["actor"]]
defp try_follow(follower, activity) do
to = Map.get(activity, "to", [])
cc = Map.get(activity, "cc", [])
actor = [activity["actor"]]

Enum.concat([to, cc, actor])
|> List.flatten()
Expand All @@ -53,7 +53,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.FollowBotPolicy do
end
end)

{:ok, message}
{:ok, activity}
end

@impl true
Expand Down
12 changes: 6 additions & 6 deletions lib/pleroma/web/activity_pub/mrf/force_bot_unlisted_policy.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceBotUnlistedPolicy do
"cc" => cc,
"actor" => actor,
"object" => object
} = message
} = activity
) do
user = User.get_cached_by_ap_id(actor)
isbot = check_if_bot(user)
Expand All @@ -36,20 +36,20 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceBotUnlistedPolicy do
|> Map.put("to", to)
|> Map.put("cc", cc)

message =
message
activity =
activity
|> Map.put("to", to)
|> Map.put("cc", cc)
|> Map.put("object", object)

{:ok, message}
{:ok, activity}
else
{:ok, message}
{:ok, activity}
end
end

@impl true
def filter(message), do: {:ok, message}
def filter(activity), do: {:ok, activity}

@impl true
def describe, do: {:ok, %{}}
Expand Down
2 changes: 1 addition & 1 deletion lib/pleroma/web/activity_pub/mrf/force_mention.ex
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMention do
end

@impl true
def filter(object), do: {:ok, object}
def filter(activity), do: {:ok, activity}

@impl true
def describe, do: {:ok, %{}}
Expand Down
Loading

0 comments on commit ccf476a

Please sign in to comment.