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 @behaviour Pleroma.Web.ActivityPub.MRF
9 defp delist_message(message, threshold) when threshold > 0 do
10 follower_collection = User.get_cached_by_ap_id(message["actor"]).follower_address
12 follower_collection? = Enum.member?(message["to"] ++ message["cc"], follower_collection)
15 case get_recipient_count(message) do
17 when follower_collection? and recipients > threshold ->
19 |> Map.put("to", [follower_collection])
20 |> Map.put("cc", ["https://www.w3.org/ns/activitystreams#Public"])
22 {:public, recipients} when recipients > threshold ->
25 |> Map.put("cc", ["https://www.w3.org/ns/activitystreams#Public"])
34 defp delist_message(message, _threshold), do: {:ok, message}
36 defp reject_message(message, threshold) when threshold > 0 do
37 with {_, recipients} <- get_recipient_count(message) do
38 if recipients > threshold do
46 defp reject_message(message, _threshold), do: {:ok, message}
48 defp get_recipient_count(message) do
49 recipients = (message["to"] || []) ++ (message["cc"] || [])
50 follower_collection = User.get_cached_by_ap_id(message["actor"]).follower_address
52 if Enum.member?(recipients, "https://www.w3.org/ns/activitystreams#Public") do
55 |> List.delete("https://www.w3.org/ns/activitystreams#Public")
56 |> List.delete(follower_collection)
58 {:public, length(recipients)}
62 |> List.delete(follower_collection)
64 {:not_public, length(recipients)}
69 def filter(%{"type" => "Create"} = message) do
72 [:mrf_hellthread, :reject_threshold],
73 Pleroma.Config.get([:mrf_hellthread, :threshold])
76 delist_threshold = Pleroma.Config.get([:mrf_hellthread, :delist_threshold])
78 with {:ok, message} <- reject_message(message, reject_threshold),
79 {:ok, message} <- delist_message(message, delist_threshold) do
87 def filter(message), do: {:ok, message}