1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicy do
6 @behaviour Pleroma.Web.ActivityPub.MRF
7 defp string_matches?(string, pattern) when is_binary(pattern) do
8 String.contains?(string, pattern)
11 defp string_matches?(string, pattern) do
12 String.match?(string, pattern)
15 defp check_reject(%{"object" => %{"content" => content}} = message) do
16 if Enum.any?(Pleroma.Config.get([:mrf_keyword, :reject]), fn pattern ->
17 string_matches?(content, pattern)
25 defp check_ftl_removal(%{"to" => to, "object" => %{"content" => content}} = message) do
26 if "https://www.w3.org/ns/activitystreams#Public" in to and
27 Enum.any?(Pleroma.Config.get([:mrf_keyword, :ftl_removal]), fn pattern ->
28 string_matches?(content, pattern)
30 to = List.delete(to, "https://www.w3.org/ns/activitystreams#Public")
31 cc = ["https://www.w3.org/ns/activitystreams#Public" | [message["cc"] || []]]
45 defp check_replace(%{"object" => %{"content" => content}} = message) do
47 Enum.reduce(Pleroma.Config.get([:mrf_keyword, :replace]), content, fn {pattern, replacement},
49 String.replace(acc, pattern, replacement)
52 {:ok, put_in(message["object"]["content"], content)}
56 def filter(%{"object" => %{"content" => nil}} = message) do
61 def filter(%{"type" => "Create", "object" => %{"content" => _content}} = message) do
62 with {:ok, message} <- check_reject(message),
63 {:ok, message} <- check_ftl_removal(message),
64 {:ok, message} <- check_replace(message) do
73 def filter(message), do: {:ok, message}