Merge branch 'bugfix/mrf-reject-logging' into 'develop'
[akkoma] / lib / pleroma / web / activity_pub / mrf / vocabulary_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
9
10 def filter(%{"type" => "Undo", "object" => child_message} = message) do
11 with {:ok, _} <- filter(child_message) do
12 {:ok, message}
13 else
14 {:reject, _} = e -> e
15 end
16 end
17
18 def filter(%{"type" => message_type} = message) do
19 with accepted_vocabulary <- Pleroma.Config.get([:mrf_vocabulary, :accept]),
20 rejected_vocabulary <- Pleroma.Config.get([:mrf_vocabulary, :reject]),
21 {_, true} <-
22 {:accepted,
23 Enum.empty?(accepted_vocabulary) || Enum.member?(accepted_vocabulary, message_type)},
24 {_, false} <-
25 {:rejected,
26 length(rejected_vocabulary) > 0 && Enum.member?(rejected_vocabulary, message_type)},
27 {:ok, _} <- filter(message["object"]) do
28 {:ok, message}
29 else
30 {:reject, _} = e -> e
31 {:accepted, _} -> {:reject, "[VocabularyPolicy] #{message_type} not in accept list"}
32 {:rejected, _} -> {:reject, "[VocabularyPolicy] #{message_type} in reject list"}
33 _ -> {:reject, "[VocabularyPolicy]"}
34 end
35 end
36
37 def filter(message), do: {:ok, message}
38
39 def describe,
40 do: {:ok, %{mrf_vocabulary: Pleroma.Config.get(:mrf_vocabulary) |> Enum.into(%{})}}
41 end