1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicy do
7 @moduledoc "Block messages with too much mentions (configurable)"
9 @behaviour Pleroma.Web.ActivityPub.MRF
11 defp delist_message(message, threshold) when threshold > 0 do
12 follower_collection = User.get_cached_by_ap_id(message["actor"]).follower_address
14 follower_collection? = Enum.member?(message["to"] ++ message["cc"], follower_collection)
17 case get_recipient_count(message) do
19 when follower_collection? and recipients > threshold ->
21 |> Map.put("to", [follower_collection])
22 |> Map.put("cc", ["https://www.w3.org/ns/activitystreams#Public"])
24 {:public, recipients} when recipients > threshold ->
27 |> Map.put("cc", ["https://www.w3.org/ns/activitystreams#Public"])
36 defp delist_message(message, _threshold), do: {:ok, message}
38 defp reject_message(message, threshold) when threshold > 0 do
39 with {_, recipients} <- get_recipient_count(message) do
40 if recipients > threshold do
48 defp reject_message(message, _threshold), do: {:ok, message}
50 defp get_recipient_count(message) do
51 recipients = (message["to"] || []) ++ (message["cc"] || [])
52 follower_collection = User.get_cached_by_ap_id(message["actor"]).follower_address
54 if Enum.member?(recipients, "https://www.w3.org/ns/activitystreams#Public") do
57 |> List.delete("https://www.w3.org/ns/activitystreams#Public")
58 |> List.delete(follower_collection)
60 {:public, length(recipients)}
64 |> List.delete(follower_collection)
66 {:not_public, length(recipients)}
71 def filter(%{"type" => "Create"} = message) do
74 [:mrf_hellthread, :reject_threshold],
75 Pleroma.Config.get([:mrf_hellthread, :threshold])
78 delist_threshold = Pleroma.Config.get([:mrf_hellthread, :delist_threshold])
80 with {:ok, message} <- reject_message(message, reject_threshold),
81 {:ok, message} <- delist_message(message, delist_threshold) do
89 def filter(message), do: {:ok, message}