First
[anni] / lib / pleroma / web / activity_pub / mrf / user_allow_list_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 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.Policy
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       |> Map.new(fn {k, v} -> {k, length(v)} end)
41
42     {:ok, %{mrf_user_allowlist: mrf_user_allowlist}}
43   end
44
45   # TODO: change way of getting settings on `lib/pleroma/web/activity_pub/mrf/user_allow_list_policy.ex:18` to use `hosts` subkey
46   # @impl true
47   # def config_description do
48   #   %{
49   #     key: :mrf_user_allowlist,
50   #     related_policy: "Pleroma.Web.ActivityPub.MRF.UserAllowListPolicy",
51   #     description: "Accept-list of users from specified instances",
52   #     children: [
53   #       %{
54   #         key: :hosts,
55   #         type: :map,
56   #         description:
57   #           "The keys in this section are the domain names that the policy should apply to." <>
58   #             " Each key should be assigned a list of users that should be allowed " <>
59   #             "through by their ActivityPub ID",
60   #         suggestions: [%{"example.org" => ["https://example.org/users/admin"]}]
61   #       }
62   #     ]
63   #   }
64   # end
65 end