6e73b2f22f7e685d1ba12d1b89f2240df6c421d4
[akkoma] / lib / pleroma / web / activity_pub / mrf.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 do
6 require Logger
7
8 @mrf_config_descriptions [
9 %{
10 group: :pleroma,
11 key: :mrf,
12 tab: :mrf,
13 label: "MRF",
14 type: :group,
15 description: "General MRF settings",
16 children: [
17 %{
18 key: :policies,
19 type: [:module, {:list, :module}],
20 description:
21 "A list of MRF policies enabled. Module names are shortened (removed leading `Pleroma.Web.ActivityPub.MRF.` part), but on adding custom module you need to use full name.",
22 suggestions: {:list_behaviour_implementations, Pleroma.Web.ActivityPub.MRF}
23 },
24 %{
25 key: :transparency,
26 label: "MRF transparency",
27 type: :boolean,
28 description:
29 "Make the content of your Message Rewrite Facility settings public (via nodeinfo)"
30 },
31 %{
32 key: :transparency_exclusions,
33 label: "MRF transparency exclusions",
34 type: {:list, :string},
35 description:
36 "Exclude specific instance names from MRF transparency. The use of the exclusions feature will be disclosed in nodeinfo as a boolean value.",
37 suggestions: [
38 "exclusion.com"
39 ]
40 }
41 ]
42 }
43 ]
44
45 @default_description %{
46 label: "",
47 description: ""
48 }
49
50 @required_description_keys [:key, :related_policy]
51
52 @callback filter(Map.t()) :: {:ok | :reject, Map.t()}
53 @callback describe() :: {:ok | :error, Map.t()}
54 @callback config_description() :: %{
55 optional(:children) => [map()],
56 key: atom(),
57 related_policy: String.t(),
58 label: String.t(),
59 description: String.t()
60 }
61 @optional_callbacks config_description: 0
62
63 def filter(policies, %{} = message) do
64 policies
65 |> Enum.reduce({:ok, message}, fn
66 policy, {:ok, message} -> policy.filter(message)
67 _, error -> error
68 end)
69 end
70
71 def filter(%{} = object), do: get_policies() |> filter(object)
72
73 def pipeline_filter(%{} = message, meta) do
74 object = meta[:object_data]
75 ap_id = message["object"]
76
77 if object && ap_id do
78 with {:ok, message} <- filter(Map.put(message, "object", object)) do
79 meta = Keyword.put(meta, :object_data, message["object"])
80 {:ok, Map.put(message, "object", ap_id), meta}
81 else
82 {err, message} -> {err, message, meta}
83 end
84 else
85 {err, message} = filter(message)
86
87 {err, message, meta}
88 end
89 end
90
91 def get_policies do
92 Pleroma.Config.get([:mrf, :policies], []) |> get_policies()
93 end
94
95 defp get_policies(policy) when is_atom(policy), do: [policy]
96 defp get_policies(policies) when is_list(policies), do: policies
97 defp get_policies(_), do: []
98
99 @spec subdomains_regex([String.t()]) :: [Regex.t()]
100 def subdomains_regex(domains) when is_list(domains) do
101 for domain <- domains, do: ~r(^#{String.replace(domain, "*.", "(.*\\.)*")}$)i
102 end
103
104 @spec subdomain_match?([Regex.t()], String.t()) :: boolean()
105 def subdomain_match?(domains, host) do
106 Enum.any?(domains, fn domain -> Regex.match?(domain, host) end)
107 end
108
109 def describe(policies) do
110 {:ok, policy_configs} =
111 policies
112 |> Enum.reduce({:ok, %{}}, fn
113 policy, {:ok, data} ->
114 {:ok, policy_data} = policy.describe()
115 {:ok, Map.merge(data, policy_data)}
116
117 _, error ->
118 error
119 end)
120
121 mrf_policies =
122 get_policies()
123 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
124
125 exclusions = Pleroma.Config.get([:mrf, :transparency_exclusions])
126
127 base =
128 %{
129 mrf_policies: mrf_policies,
130 exclusions: length(exclusions) > 0
131 }
132 |> Map.merge(policy_configs)
133
134 {:ok, base}
135 end
136
137 def describe, do: get_policies() |> describe()
138
139 def config_descriptions do
140 Pleroma.Web.ActivityPub.MRF
141 |> Pleroma.Docs.Generator.list_behaviour_implementations()
142 |> config_descriptions()
143 end
144
145 def config_descriptions(policies) do
146 Enum.reduce(policies, @mrf_config_descriptions, fn policy, acc ->
147 if function_exported?(policy, :config_description, 0) do
148 description =
149 @default_description
150 |> Map.merge(policy.config_description)
151 |> Map.put(:group, :pleroma)
152 |> Map.put(:tab, :mrf)
153 |> Map.put(:type, :group)
154
155 if Enum.all?(@required_description_keys, &Map.has_key?(description, &1)) do
156 [description | acc]
157 else
158 Logger.warn(
159 "#{policy} config description doesn't have one or all required keys #{
160 inspect(@required_description_keys)
161 }"
162 )
163
164 acc
165 end
166 else
167 Logger.debug(
168 "#{policy} is excluded from config descriptions, because does not implement `config_description/0` method."
169 )
170
171 acc
172 end
173 end)
174 end
175 end