Add :reject_deletes option to SimplePolicy
[akkoma] / docs / configuration / mrf.md
1 # Message Rewrite Facility
2
3 The Message Rewrite Facility (MRF) is a subsystem that is implemented as a series of hooks that allows the administrator to rewrite or discard messages.
4
5 Possible uses include:
6
7 * marking incoming messages with media from a given account or instance as sensitive
8 * rejecting messages from a specific instance
9 * rejecting reports (flags) from a specific instance
10 * removing/unlisting messages from the public timelines
11 * removing media from messages
12 * sending only public messages to a specific instance
13
14 The MRF provides user-configurable policies. The default policy is `NoOpPolicy`, which disables the MRF functionality. Pleroma also includes an easy to use policy called `SimplePolicy` which maps messages matching certain pre-defined criterion to actions built into the policy module.
15
16 It is possible to use multiple, active MRF policies at the same time.
17
18 ## Quarantine Instances
19
20 You have the ability to prevent from private / followers-only messages from federating with specific instances. Which means they will only get the public or unlisted messages from your instance.
21
22 If, for example, you're using `MIX_ENV=prod` aka using production mode, you would open your configuration file located in `config/prod.secret.exs` and edit or add the option under your `:instance` config object. Then you would specify the instance within quotes.
23
24 ```elixir
25 config :pleroma, :instance,
26 [...]
27 quarantined_instances: ["instance.example", "other.example"]
28 ```
29
30 ## Using `SimplePolicy`
31
32 `SimplePolicy` is capable of handling most common admin tasks.
33
34 To use `SimplePolicy`, you must enable it. Do so by adding the following to your `:instance` config object, so that it looks like this:
35
36 ```elixir
37 config :pleroma, :instance,
38 [...]
39 rewrite_policy: Pleroma.Web.ActivityPub.MRF.SimplePolicy
40 ```
41
42 Once `SimplePolicy` is enabled, you can configure various groups in the `:mrf_simple` config object. These groups are:
43
44 * `media_removal`: Servers in this group will have media stripped from incoming messages.
45 * `media_nsfw`: Servers in this group will have the #nsfw tag and sensitive setting injected into incoming messages which contain media.
46 * `reject`: Servers in this group will have their messages (except deletions) rejected.
47 * `federated_timeline_removal`: Servers in this group will have their messages unlisted from the public timelines by flipping the `to` and `cc` fields.
48 * `report_removal`: Servers in this group will have their reports (flags) rejected.
49 * `reject_deletes`: Deletion requests will be rejected from these servers.
50
51 Servers should be configured as lists.
52
53 ### Example
54
55 This example will enable `SimplePolicy`, block media from `illegalporn.biz`, mark media as NSFW from `porn.biz` and `porn.business`, reject messages from `spam.com`, remove messages from `spam.university` from the federated timeline and block reports (flags) from `whiny.whiner`:
56
57 ```elixir
58 config :pleroma, :instance,
59 rewrite_policy: [Pleroma.Web.ActivityPub.MRF.SimplePolicy]
60
61 config :pleroma, :mrf_simple,
62 media_removal: ["illegalporn.biz"],
63 media_nsfw: ["porn.biz", "porn.business"],
64 reject: ["spam.com"],
65 federated_timeline_removal: ["spam.university"],
66 report_removal: ["whiny.whiner"]
67 ```
68
69 ### Use with Care
70
71 The effects of MRF policies can be very drastic. It is important to use this functionality carefully. Always try to talk to an admin before writing an MRF policy concerning their instance.
72
73 ## Writing your own MRF Policy
74
75 As discussed above, the MRF system is a modular system that supports pluggable policies. This means that an admin may write a custom MRF policy in Elixir or any other language that runs on the Erlang VM, by specifying the module name in the `rewrite_policy` config setting.
76
77 For example, here is a sample policy module which rewrites all messages to "new message content":
78
79 ```elixir
80 defmodule Pleroma.Web.ActivityPub.MRF.RewritePolicy do
81 @moduledoc "MRF policy which rewrites all Notes to have 'new message content'."
82 @behaviour Pleroma.Web.ActivityPub.MRF
83
84 # Catch messages which contain Note objects with actual data to filter.
85 # Capture the object as `object`, the message content as `content` and the
86 # message itself as `message`.
87 @impl true
88 def filter(
89 %{"type" => "Create", "object" => %{"type" => "Note", "content" => content} = object} =
90 message
91 )
92 when is_binary(content) do
93 # Subject / CW is stored as summary instead of `name` like other AS2 objects
94 # because of Mastodon doing it that way.
95 summary = object["summary"]
96
97 # Message edits go here.
98 content = "new message content"
99
100 # Assemble the mutated object.
101 object =
102 object
103 |> Map.put("content", content)
104 |> Map.put("summary", summary)
105
106 # Assemble the mutated message.
107 message = Map.put(message, "object", object)
108 {:ok, message}
109 end
110
111 # Let all other messages through without modifying them.
112 @impl true
113 def filter(message), do: {:ok, message}
114
115 @impl true
116 def describe do
117 {:ok, %{mrf_sample: %{content: "new message content"}}}`
118 end
119 end
120 ```
121
122 If you save this file as `lib/pleroma/web/activity_pub/mrf/rewrite_policy.ex`, it will be included when you next rebuild Pleroma. You can enable it in the configuration like so:
123
124 ```elixir
125 config :pleroma, :instance,
126 rewrite_policy: [
127 Pleroma.Web.ActivityPub.MRF.SimplePolicy,
128 Pleroma.Web.ActivityPub.MRF.RewritePolicy
129 ]
130 ```
131
132 Please note that the Pleroma developers consider custom MRF policy modules to fall under the purview of the AGPL. As such, you are obligated to release the sources to your custom MRF policy modules upon request.