cd9f9b1c4dfeb1dc5721338e67a0f4f5e6892c5f
[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(object) do
10 follower_collection = User.get_by_ap_id(object["actor"].follower_address)
11
12 object
13 |> Kernel.update_in(["to"], [follower_collection])
14 |> Kernel.update_in(["cc"], ["https://www.w3.org/ns/activitystreams#Public"])
15 end
16
17 @impl true
18 def filter(%{"type" => "Create"} = object) do
19 delist_threshold = Pleroma.Config.get([:mrf_hellthread, :delist_threshold])
20
21 reject_threshold =
22 Pleroma.Config.get(
23 [:mrf_hellthread, :reject_threshold],
24 Pleroma.Config.get([:mrf_hellthread, :threshold])
25 )
26
27 recipients = (object["to"] || []) ++ (object["cc"] || [])
28
29 cond do
30 length(recipients) > reject_threshold and reject_threshold > 0 ->
31 {:reject, nil}
32
33 length(recipients) > delist_threshold and delist_threshold > 0 ->
34 if Enum.member?(object["to"], "https://www.w3.org/ns/activitystreams#Public") or
35 Enum.member?(object["to"], "https://www.w3.org/ns/activitystreams#Public") do
36 delist_message(object)
37 {:ok, object}
38 else
39 {:ok, object}
40 end
41
42 true ->
43 {:ok, object}
44 end
45 end
46
47 @impl true
48 def filter(object), do: {:ok, object}
49 end