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