1f09b4e667605996ed143255d4cee723551fbb67
[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 delist_threshold = Pleroma.Config.get([:mrf_hellthread, :delist_threshold])
11 follower_collection = User.get_cached_by_ap_id(message["actor"]).follower_address
12
13 message =
14 with {:public, recipients} <- get_recipient_count(message) do
15 if recipients > delist_threshold and delist_threshold > 0 do
16 message
17 |> Map.put("to", [follower_collection])
18 |> Map.put("cc", ["https://www.w3.org/ns/activitystreams#Public"])
19 else
20 message
21 end
22 else
23 _ -> message
24 end
25
26 {:ok, message}
27 end
28
29 defp reject_message(message) do
30 reject_threshold =
31 Pleroma.Config.get(
32 [:mrf_hellthread, :reject_threshold],
33 Pleroma.Config.get([:mrf_hellthread, :threshold])
34 )
35
36 with {_, recipients} <- get_recipient_count(message) do
37 if recipients > reject_threshold and reject_threshold > 0 do
38 {:reject, nil}
39 else
40 {:ok, message}
41 end
42 end
43 end
44
45 defp get_recipient_count(message) do
46 recipients = (message["to"] || []) ++ (message["cc"] || [])
47 follower_collection = User.get_cached_by_ap_id(message["actor"]).follower_address
48
49 if Enum.member?(recipients, "https://www.w3.org/ns/activitystreams#Public") do
50 recipients
51 |> List.delete("https://www.w3.org/ns/activitystreams#Public")
52 |> List.delete(follower_collection)
53
54 {:public, length(recipients)}
55 else
56 recipients
57 |> List.delete(follower_collection)
58
59 {:not_public, length(recipients)}
60 end
61 end
62
63 @impl true
64 def filter(%{"type" => "Create"} = message) do
65 with {:ok, message} <- reject_message(message),
66 {:ok, message} <- delist_message(message) do
67 {:ok, message}
68 else
69 _e -> {:reject, nil}
70 end
71 end
72
73 @impl true
74 def filter(message), do: {:ok, message}
75 end