1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicy do
8 require Pleroma.Constants
10 @moduledoc "Block messages with too much mentions (configurable)"
12 @behaviour Pleroma.Web.ActivityPub.MRF
14 defp delist_message(message, threshold) when threshold > 0 do
15 follower_collection = User.get_cached_by_ap_id(message["actor"]).follower_address
17 follower_collection? = Enum.member?(message["to"] ++ message["cc"], follower_collection)
20 case get_recipient_count(message) do
22 when follower_collection? and recipients > threshold ->
24 |> Map.put("to", [follower_collection])
25 |> Map.put("cc", [Pleroma.Constants.as_public()])
27 {:public, recipients} when recipients > threshold ->
30 |> Map.put("cc", [Pleroma.Constants.as_public()])
39 defp delist_message(message, _threshold), do: {:ok, message}
41 defp reject_message(message, threshold) when threshold > 0 do
42 with {_, recipients} <- get_recipient_count(message) do
43 if recipients > threshold do
51 defp reject_message(message, _threshold), do: {:ok, message}
53 defp get_recipient_count(message) do
54 recipients = (message["to"] || []) ++ (message["cc"] || [])
55 follower_collection = User.get_cached_by_ap_id(message["actor"]).follower_address
57 if Enum.member?(recipients, Pleroma.Constants.as_public()) do
60 |> List.delete(Pleroma.Constants.as_public())
61 |> List.delete(follower_collection)
63 {:public, length(recipients)}
67 |> List.delete(follower_collection)
69 {:not_public, length(recipients)}
74 def filter(%{"type" => "Create"} = message) do
77 [:mrf_hellthread, :reject_threshold],
78 Pleroma.Config.get([:mrf_hellthread, :threshold])
81 delist_threshold = Pleroma.Config.get([:mrf_hellthread, :delist_threshold])
83 with {:ok, message} <- reject_message(message, reject_threshold),
84 {:ok, message} <- delist_message(message, delist_threshold) do
92 def filter(message), do: {:ok, message}
96 do: {:ok, %{mrf_hellthread: Pleroma.Config.get(:mrf_hellthread) |> Enum.into(%{})}}