Merge remote-tracking branch 'pleroma/develop' into admin-api-users-sort
[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 def filter(policies, %{} = message) do
55 policies
56 |> Enum.reduce({:ok, message}, fn
57 policy, {:ok, message} -> policy.filter(message)
58 _, error -> error
59 end)
60 end
61
62 def filter(%{} = object), do: get_policies() |> filter(object)
63
64 @impl true
65 def pipeline_filter(%{} = message, meta) do
66 object = meta[:object_data]
67 ap_id = message["object"]
68
69 if object && ap_id do
70 with {:ok, message} <- filter(Map.put(message, "object", object)) do
71 meta = Keyword.put(meta, :object_data, message["object"])
72 {:ok, Map.put(message, "object", ap_id), meta}
73 else
74 {err, message} -> {err, message, meta}
75 end
76 else
77 {err, message} = filter(message)
78
79 {err, message, meta}
80 end
81 end
82
83 def get_policies do
84 Pleroma.Config.get([:mrf, :policies], [])
85 |> get_policies()
86 |> Enum.concat([Pleroma.Web.ActivityPub.MRF.HashtagPolicy])
87 end
88
89 defp get_policies(policy) when is_atom(policy), do: [policy]
90 defp get_policies(policies) when is_list(policies), do: policies
91 defp get_policies(_), do: []
92
93 @spec subdomains_regex([String.t()]) :: [Regex.t()]
94 def subdomains_regex(domains) when is_list(domains) do
95 for domain <- domains, do: ~r(^#{String.replace(domain, "*.", "(.*\\.)*")}$)i
96 end
97
98 @spec subdomain_match?([Regex.t()], String.t()) :: boolean()
99 def subdomain_match?(domains, host) do
100 Enum.any?(domains, fn domain -> Regex.match?(domain, host) end)
101 end
102
103 def describe(policies) do
104 {:ok, policy_configs} =
105 policies
106 |> Enum.reduce({:ok, %{}}, fn
107 policy, {:ok, data} ->
108 {:ok, policy_data} = policy.describe()
109 {:ok, Map.merge(data, policy_data)}
110
111 _, error ->
112 error
113 end)
114
115 mrf_policies =
116 get_policies()
117 |> Enum.map(fn policy -> to_string(policy) |> String.split(".") |> List.last() end)
118
119 exclusions = Pleroma.Config.get([:mrf, :transparency_exclusions])
120
121 base =
122 %{
123 mrf_policies: mrf_policies,
124 exclusions: length(exclusions) > 0
125 }
126 |> Map.merge(policy_configs)
127
128 {:ok, base}
129 end
130
131 def describe, do: get_policies() |> describe()
132
133 def config_descriptions do
134 Pleroma.Web.ActivityPub.MRF.Policy
135 |> Pleroma.Docs.Generator.list_behaviour_implementations()
136 |> config_descriptions()
137 end
138
139 def config_descriptions(policies) do
140 Enum.reduce(policies, @mrf_config_descriptions, fn policy, acc ->
141 if function_exported?(policy, :config_description, 0) do
142 description =
143 @default_description
144 |> Map.merge(policy.config_description)
145 |> Map.put(:group, :pleroma)
146 |> Map.put(:tab, :mrf)
147 |> Map.put(:type, :group)
148
149 if Enum.all?(@required_description_keys, &Map.has_key?(description, &1)) do
150 [description | acc]
151 else
152 Logger.warn(
153 "#{policy} config description doesn't have one or all required keys #{
154 inspect(@required_description_keys)
155 }"
156 )
157
158 acc
159 end
160 else
161 Logger.debug(
162 "#{policy} is excluded from config descriptions, because does not implement `config_description/0` method."
163 )
164
165 acc
166 end
167 end)
168 end
169 end