9be3829724b1123ba09d815271141cb96ad16484
[akkoma] / lib / pleroma / web / activity_pub / mrf / hellthread_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicy do
6 alias Pleroma.User
7 @behaviour Pleroma.Web.ActivityPub.MRF
8
9 defp delist_message(message) do
10 follower_collection = User.get_cached_by_ap_id(message["actor"]).follower_address
11
12 message
13 |> Map.put("to", [follower_collection])
14 |> Map.put("cc", ["https://www.w3.org/ns/activitystreams#Public"])
15 end
16
17 defp get_recipient_count(message) do
18 recipients = (message["to"] || []) ++ (message["cc"] || [])
19
20 cond do
21 Enum.member?(recipients, "https://www.w3.org/ns/activitystreams#Public") &&
22 Enum.find(recipients, &String.ends_with?(&1, "/followers")) ->
23 length(recipients) - 2
24
25 Enum.member?(recipients, "https://www.w3.org/ns/activitystreams#Public") ||
26 Enum.find(recipients, &String.ends_with?(&1, "/followers")) ->
27 length(recipients) - 1
28
29 true ->
30 length(recipients)
31 end
32 end
33
34 @impl true
35 def filter(%{"type" => "Create"} = message) do
36 delist_threshold = Pleroma.Config.get([:mrf_hellthread, :delist_threshold])
37
38 reject_threshold =
39 Pleroma.Config.get(
40 [:mrf_hellthread, :reject_threshold],
41 Pleroma.Config.get([:mrf_hellthread, :threshold])
42 )
43
44 recipients = get_recipient_count(message)
45
46 cond do
47 recipients > reject_threshold and reject_threshold > 0 ->
48 {:reject, nil}
49
50 recipients > delist_threshold and delist_threshold > 0 ->
51 if Enum.member?(message["to"], "https://www.w3.org/ns/activitystreams#Public") or
52 Enum.member?(message["cc"], "https://www.w3.org/ns/activitystreams#Public") do
53 {:ok, delist_message(message)}
54 else
55 {:ok, message}
56 end
57
58 true ->
59 {:ok, message}
60 end
61 end
62
63 @impl true
64 def filter(message), do: {:ok, message}
65 end