Merge branch 'release/2.4.0' into 'stable'
[akkoma] / lib / pleroma / web / activity_pub / mrf / vocabulary_policy.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.VocabularyPolicy do
6 @moduledoc "Filter messages which belong to certain activity vocabularies"
7
8 @behaviour Pleroma.Web.ActivityPub.MRF.Policy
9
10 @impl true
11 def filter(%{"type" => "Undo", "object" => child_message} = message) do
12 with {:ok, _} <- filter(child_message) do
13 {:ok, message}
14 else
15 {:reject, _} = e -> e
16 end
17 end
18
19 def filter(%{"type" => message_type} = message) do
20 with accepted_vocabulary <- Pleroma.Config.get([:mrf_vocabulary, :accept]),
21 rejected_vocabulary <- Pleroma.Config.get([:mrf_vocabulary, :reject]),
22 {_, true} <-
23 {:accepted,
24 Enum.empty?(accepted_vocabulary) || Enum.member?(accepted_vocabulary, message_type)},
25 {_, false} <-
26 {:rejected,
27 length(rejected_vocabulary) > 0 && Enum.member?(rejected_vocabulary, message_type)},
28 {:ok, _} <- filter(message["object"]) do
29 {:ok, message}
30 else
31 {:reject, _} = e -> e
32 {:accepted, _} -> {:reject, "[VocabularyPolicy] #{message_type} not in accept list"}
33 {:rejected, _} -> {:reject, "[VocabularyPolicy] #{message_type} in reject list"}
34 _ -> {:reject, "[VocabularyPolicy]"}
35 end
36 end
37
38 def filter(message), do: {:ok, message}
39
40 @impl true
41 def describe,
42 do: {:ok, %{mrf_vocabulary: Pleroma.Config.get(:mrf_vocabulary) |> Enum.into(%{})}}
43
44 @impl true
45 def config_description do
46 %{
47 key: :mrf_vocabulary,
48 related_policy: "Pleroma.Web.ActivityPub.MRF.VocabularyPolicy",
49 label: "MRF Vocabulary",
50 description: "Filter messages which belong to certain activity vocabularies",
51 children: [
52 %{
53 key: :accept,
54 type: {:list, :string},
55 description:
56 "A list of ActivityStreams terms to accept. If empty, all supported messages are accepted.",
57 suggestions: ["Create", "Follow", "Mention", "Announce", "Like"]
58 },
59 %{
60 key: :reject,
61 type: {:list, :string},
62 description:
63 "A list of ActivityStreams terms to reject. If empty, no messages are rejected.",
64 suggestions: ["Create", "Follow", "Mention", "Announce", "Like"]
65 }
66 ]
67 }
68 end
69 end