MRF Policies: Return a {:reject, reason} instead of {:reject, nil}
[akkoma] / lib / pleroma / web / activity_pub / mrf / user_allow_list_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.UserAllowListPolicy do
6 alias Pleroma.Config
7
8 @moduledoc "Accept-list of users from specified instances"
9 @behaviour Pleroma.Web.ActivityPub.MRF
10
11 defp filter_by_list(object, []), do: {:ok, object}
12
13 defp filter_by_list(%{"actor" => actor} = object, allow_list) do
14 if actor in allow_list do
15 {:ok, object}
16 else
17 {:reject, "[UserAllowListPolicy] #{actor} not in the list"}
18 end
19 end
20
21 @impl true
22 def filter(%{"actor" => actor} = object) do
23 actor_info = URI.parse(actor)
24
25 allow_list =
26 Config.get(
27 [:mrf_user_allowlist, actor_info.host],
28 []
29 )
30
31 filter_by_list(object, allow_list)
32 end
33
34 def filter(object), do: {:ok, object}
35
36 @impl true
37 def describe do
38 mrf_user_allowlist =
39 Config.get([:mrf_user_allowlist], [])
40 |> Enum.into(%{}, fn {k, v} -> {k, length(v)} end)
41
42 {:ok, %{mrf_user_allowlist: mrf_user_allowlist}}
43 end
44 end