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