extend reject MRF to check if originating instance is blocked
[akkoma] / lib / pleroma / web / activity_pub / mrf / reject_non_public.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.RejectNonPublic do
6 @moduledoc "Rejects non-public (followers-only, direct) activities"
7
8 alias Pleroma.Config
9 alias Pleroma.User
10
11 @behaviour Pleroma.Web.ActivityPub.MRF.Policy
12
13 require Pleroma.Constants
14
15 @impl true
16 def filter(%{"type" => "Create"} = object) do
17 user = User.get_cached_by_ap_id(object["actor"])
18
19 # Determine visibility
20 visibility =
21 cond do
22 Pleroma.Constants.as_public() in object["to"] -> "public"
23 Pleroma.Constants.as_public() in object["cc"] -> "unlisted"
24 user.follower_address in object["to"] -> "followers"
25 true -> "direct"
26 end
27
28 policy = Config.get(:mrf_rejectnonpublic)
29
30 cond do
31 visibility in ["public", "unlisted"] ->
32 {:ok, object}
33
34 visibility == "followers" and Keyword.get(policy, :allow_followersonly) ->
35 {:ok, object}
36
37 visibility == "direct" and Keyword.get(policy, :allow_direct) ->
38 {:ok, object}
39
40 true ->
41 {:reject, "[RejectNonPublic] visibility: #{visibility}"}
42 end
43 end
44
45 @impl true
46 def filter(object), do: {:ok, object}
47
48 @impl true
49 def describe,
50 do: {:ok, %{mrf_rejectnonpublic: Config.get(:mrf_rejectnonpublic) |> Map.new()}}
51
52 @impl true
53 def config_description do
54 %{
55 key: :mrf_rejectnonpublic,
56 related_policy: "Pleroma.Web.ActivityPub.MRF.RejectNonPublic",
57 description: "RejectNonPublic drops posts with non-public visibility settings.",
58 label: "MRF Reject Non Public",
59 children: [
60 %{
61 key: :allow_followersonly,
62 label: "Allow followers-only",
63 type: :boolean,
64 description: "Whether to allow followers-only posts"
65 },
66 %{
67 key: :allow_direct,
68 type: :boolean,
69 description: "Whether to allow direct messages"
70 }
71 ]
72 }
73 end
74 end