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