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) do
10 delist_threshold = Pleroma.Config.get([:mrf_hellthread, :delist_threshold])
11 follower_collection = User.get_cached_by_ap_id(message["actor"]).follower_address
13 follower_collection? = Enum.member?(message["to"] ++ message["cc"], follower_collection)
16 case recipients = get_recipient_count(message) do
17 {:public, _} when follower_collection? and recipients > delist_threshold ->
19 |> Map.put("to", [follower_collection])
20 |> Map.put("cc", ["https://www.w3.org/ns/activitystreams#Public"])
22 {:public, _} when recipients > delist_threshold ->
25 |> Map.put("cc", ["https://www.w3.org/ns/activitystreams#Public"])
34 defp reject_message(message) do
37 [:mrf_hellthread, :reject_threshold],
38 Pleroma.Config.get([:mrf_hellthread, :threshold])
41 with {_, recipients} <- get_recipient_count(message) do
42 if recipients > reject_threshold and reject_threshold > 0 do
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
72 with {:ok, message} <- reject_message(message),
73 {:ok, message} <- delist_message(message) do
81 def filter(message), do: {:ok, message}