e4ee8fe827fae7fdf827a517cb247fa708c1e054
[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.Policy}
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, :tuple},
37 key_placeholder: "instance",
38 value_placeholder: "reason",
39 description:
40 "Exclude specific instance names from MRF transparency. The use of the exclusions feature will be disclosed in nodeinfo as a boolean value. You can also provide a reason for excluding these instance names. The instances and reasons won't be publicly disclosed.",
41 suggestions: [
42 "exclusion.com"
43 ]
44 }
45 ]
46 }
47 ]
48
49 @default_description %{
50 label: "",
51 description: ""
52 }
53
54 @required_description_keys [:key, :related_policy]
55
56 def filter(policies, %{} = message) do
57 policies
58 |> Enum.reduce({:ok, message}, fn
59 policy, {:ok, message} -> policy.filter(message)
60 _, error -> error
61 end)
62 end
63
64 def filter(%{} = object), do: get_policies() |> filter(object)
65
66 @impl true
67 def pipeline_filter(%{} = message, meta) do
68 object = meta[:object_data]
69 ap_id = message["object"]
70
71 if object && ap_id do
72 with {:ok, message} <- filter(Map.put(message, "object", object)) do
73 meta = Keyword.put(meta, :object_data, message["object"])
74 {:ok, Map.put(message, "object", ap_id), meta}
75 else
76 {err, message} -> {err, message, meta}
77 end
78 else
79 {err, message} = filter(message)
80
81 {err, message, meta}
82 end
83 end
84
85 def get_policies do
86 Pleroma.Config.get([:mrf, :policies], [])
87 |> get_policies()
88 |> Enum.concat([Pleroma.Web.ActivityPub.MRF.HashtagPolicy])
89 end
90
91 defp get_policies(policy) when is_atom(policy), do: [policy]
92 defp get_policies(policies) when is_list(policies), do: policies
93 defp get_policies(_), do: []
94
95 @spec subdomains_regex([String.t()]) :: [Regex.t()]
96 def subdomains_regex(domains) when is_list(domains) do
97 for domain <- domains, do: ~r(^#{String.replace(domain, "*.", "(.*\\.)*")}$)i
98 end
99
100 @spec subdomain_match?([Regex.t()], String.t()) :: boolean()
101 def subdomain_match?(domains, host) do
102 Enum.any?(domains, fn domain -> Regex.match?(domain, host) end)
103 end
104
105 @spec instance_list_from_tuples([{String.t(), String.t()}]) :: [String.t()]
106 def instance_list_from_tuples(list) do
107 Enum.map(list, fn {instance, _} -> instance end)
108 end
109
110 def describe(policies) do
111 {:ok, policy_configs} =
112 policies
113 |> Enum.reduce({:ok, %{}}, fn
114 policy, {:ok, data} ->
115 {:ok, policy_data} = policy.describe()
116 {:ok, Map.merge(data, policy_data)}
117
118 _, error ->
119 error
120 end)
121
122 mrf_policies =
123 get_policies()
124 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
125
126 exclusions = Pleroma.Config.get([:mrf, :transparency_exclusions])
127
128 base =
129 %{
130 mrf_policies: mrf_policies,
131 exclusions: length(exclusions) > 0
132 }
133 |> Map.merge(policy_configs)
134
135 {:ok, base}
136 end
137
138 def describe, do: get_policies() |> describe()
139
140 def config_descriptions do
141 Pleroma.Web.ActivityPub.MRF.Policy
142 |> Pleroma.Docs.Generator.list_behaviour_implementations()
143 |> config_descriptions()
144 end
145
146 def config_descriptions(policies) do
147 Enum.reduce(policies, @mrf_config_descriptions, fn policy, acc ->
148 if function_exported?(policy, :config_description, 0) do
149 description =
150 @default_description
151 |> Map.merge(policy.config_description)
152 |> Map.put(:group, :pleroma)
153 |> Map.put(:tab, :mrf)
154 |> Map.put(:type, :group)
155
156 if Enum.all?(@required_description_keys, &Map.has_key?(description, &1)) do
157 [description | acc]
158 else
159 Logger.warn(
160 "#{policy} config description doesn't have one or all required keys #{
161 inspect(@required_description_keys)
162 }"
163 )
164
165 acc
166 end
167 else
168 Logger.debug(
169 "#{policy} is excluded from config descriptions, because does not implement `config_description/0` method."
170 )
171
172 acc
173 end
174 end)
175 end
176 end