Browse Source

MRF: create MRF.Policy behaviour separate from MRF module

Speeds up recompilation by reducing compile-time deps
cycles-validator
Alex Gleason 2 years ago
parent
commit
fe4c4a7178
No known key found for this signature in database GPG Key ID: 7211D1F99744FBB7
29 changed files with 43 additions and 38 deletions
  1. +1
    -1
      docs/configuration/mrf.md
  2. +0
    -11
      lib/pleroma/web/activity_pub/mrf.ex
  3. +1
    -1
      lib/pleroma/web/activity_pub/mrf/activity_expiration_policy.ex
  4. +1
    -1
      lib/pleroma/web/activity_pub/mrf/anti_followbot_policy.ex
  5. +1
    -1
      lib/pleroma/web/activity_pub/mrf/anti_link_spam_policy.ex
  6. +1
    -1
      lib/pleroma/web/activity_pub/mrf/drop_policy.ex
  7. +1
    -1
      lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex
  8. +1
    -1
      lib/pleroma/web/activity_pub/mrf/follow_bot_policy.ex
  9. +1
    -1
      lib/pleroma/web/activity_pub/mrf/force_bot_unlisted_policy.ex
  10. +1
    -1
      lib/pleroma/web/activity_pub/mrf/hashtag_policy.ex
  11. +1
    -1
      lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex
  12. +1
    -1
      lib/pleroma/web/activity_pub/mrf/keyword_policy.ex
  13. +1
    -1
      lib/pleroma/web/activity_pub/mrf/media_proxy_warming_policy.ex
  14. +1
    -1
      lib/pleroma/web/activity_pub/mrf/mention_policy.ex
  15. +1
    -1
      lib/pleroma/web/activity_pub/mrf/no_empty_policy.ex
  16. +1
    -1
      lib/pleroma/web/activity_pub/mrf/no_op_policy.ex
  17. +1
    -1
      lib/pleroma/web/activity_pub/mrf/no_placeholder_text_policy.ex
  18. +1
    -1
      lib/pleroma/web/activity_pub/mrf/normalize_markup.ex
  19. +1
    -1
      lib/pleroma/web/activity_pub/mrf/object_age_policy.ex
  20. +16
    -0
      lib/pleroma/web/activity_pub/mrf/policy.ex
  21. +1
    -1
      lib/pleroma/web/activity_pub/mrf/reject_non_public.ex
  22. +1
    -1
      lib/pleroma/web/activity_pub/mrf/simple_policy.ex
  23. +1
    -1
      lib/pleroma/web/activity_pub/mrf/steal_emoji_policy.ex
  24. +1
    -1
      lib/pleroma/web/activity_pub/mrf/subchain_policy.ex
  25. +1
    -1
      lib/pleroma/web/activity_pub/mrf/tag_policy.ex
  26. +1
    -1
      lib/pleroma/web/activity_pub/mrf/user_allow_list_policy.ex
  27. +1
    -1
      lib/pleroma/web/activity_pub/mrf/vocabulary_policy.ex
  28. +1
    -1
      test/fixtures/modules/good_mrf.ex
  29. +1
    -1
      test/support/mrf_module_mock.ex

+ 1
- 1
docs/configuration/mrf.md View File

@@ -82,7 +82,7 @@ For example, here is a sample policy module which rewrites all messages to "new
```elixir
defmodule Pleroma.Web.ActivityPub.MRF.RewritePolicy do
@moduledoc "MRF policy which rewrites all Notes to have 'new message content'."
@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

# Catch messages which contain Note objects with actual data to filter.
# Capture the object as `object`, the message content as `content` and the


+ 0
- 11
lib/pleroma/web/activity_pub/mrf.ex View File

@@ -51,17 +51,6 @@ defmodule Pleroma.Web.ActivityPub.MRF do

@required_description_keys [:key, :related_policy]

@callback filter(Map.t()) :: {:ok | :reject, Map.t()}
@callback describe() :: {:ok | :error, Map.t()}
@callback config_description() :: %{
optional(:children) => [map()],
key: atom(),
related_policy: String.t(),
label: String.t(),
description: String.t()
}
@optional_callbacks config_description: 0

def filter(policies, %{} = message) do
policies
|> Enum.reduce({:ok, message}, fn


+ 1
- 1
lib/pleroma/web/activity_pub/mrf/activity_expiration_policy.ex View File

@@ -4,7 +4,7 @@

defmodule Pleroma.Web.ActivityPub.MRF.ActivityExpirationPolicy do
@moduledoc "Adds expiration to all local Create activities"
@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

@impl true
def filter(activity) do


+ 1
- 1
lib/pleroma/web/activity_pub/mrf/anti_followbot_policy.ex View File

@@ -7,7 +7,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.AntiFollowbotPolicy do

@moduledoc "Prevent followbots from following with a bit of heuristic"

@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

# XXX: this should become User.normalize_by_ap_id() or similar, really.
defp normalize_by_ap_id(%{"id" => id}), do: User.get_cached_by_ap_id(id)


+ 1
- 1
lib/pleroma/web/activity_pub/mrf/anti_link_spam_policy.ex View File

@@ -5,7 +5,7 @@
defmodule Pleroma.Web.ActivityPub.MRF.AntiLinkSpamPolicy do
alias Pleroma.User

@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

require Logger



+ 1
- 1
lib/pleroma/web/activity_pub/mrf/drop_policy.ex View File

@@ -5,7 +5,7 @@
defmodule Pleroma.Web.ActivityPub.MRF.DropPolicy do
require Logger
@moduledoc "Drop and log everything received"
@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

@impl true
def filter(object) do


+ 1
- 1
lib/pleroma/web/activity_pub/mrf/ensure_re_prepended.ex View File

@@ -6,7 +6,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.EnsureRePrepended do
alias Pleroma.Object

@moduledoc "Ensure a re: is prepended on replies to a post with a Subject"
@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

@reply_prefix Regex.compile!("^re:[[:space:]]*", [:caseless])



+ 1
- 1
lib/pleroma/web/activity_pub/mrf/follow_bot_policy.ex View File

@@ -1,5 +1,5 @@
defmodule Pleroma.Web.ActivityPub.MRF.FollowBotPolicy do
@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy
alias Pleroma.Config
alias Pleroma.User
alias Pleroma.Web.CommonAPI


+ 1
- 1
lib/pleroma/web/activity_pub/mrf/force_bot_unlisted_policy.ex View File

@@ -4,7 +4,7 @@

defmodule Pleroma.Web.ActivityPub.MRF.ForceBotUnlistedPolicy do
alias Pleroma.User
@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy
@moduledoc "Remove bot posts from federated timeline"

require Pleroma.Constants


+ 1
- 1
lib/pleroma/web/activity_pub/mrf/hashtag_policy.ex View File

@@ -14,7 +14,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.HashtagPolicy do
Note: This MRF Policy is always enabled, if you want to disable it you have to set empty lists.
"""

@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

defp check_reject(message, hashtags) do
if Enum.any?(Config.get([:mrf_hashtag, :reject]), fn match -> match in hashtags end) do


+ 1
- 1
lib/pleroma/web/activity_pub/mrf/hellthread_policy.ex View File

@@ -9,7 +9,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.HellthreadPolicy do

@moduledoc "Block messages with too much mentions (configurable)"

@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

defp delist_message(message, threshold) when threshold > 0 do
follower_collection = User.get_cached_by_ap_id(message["actor"]).follower_address


+ 1
- 1
lib/pleroma/web/activity_pub/mrf/keyword_policy.ex View File

@@ -7,7 +7,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.KeywordPolicy do

@moduledoc "Reject or Word-Replace messages with a keyword or regex"

@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy
defp string_matches?(string, _) when not is_binary(string) do
false
end


+ 1
- 1
lib/pleroma/web/activity_pub/mrf/media_proxy_warming_policy.ex View File

@@ -4,7 +4,7 @@

defmodule Pleroma.Web.ActivityPub.MRF.MediaProxyWarmingPolicy do
@moduledoc "Preloads any attachments in the MediaProxy cache by prefetching them"
@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

alias Pleroma.HTTP
alias Pleroma.Web.MediaProxy


+ 1
- 1
lib/pleroma/web/activity_pub/mrf/mention_policy.ex View File

@@ -5,7 +5,7 @@
defmodule Pleroma.Web.ActivityPub.MRF.MentionPolicy do
@moduledoc "Block messages which mention a user"

@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

@impl true
def filter(%{"type" => "Create"} = message) do


+ 1
- 1
lib/pleroma/web/activity_pub/mrf/no_empty_policy.ex View File

@@ -4,7 +4,7 @@

defmodule Pleroma.Web.ActivityPub.MRF.NoEmptyPolicy do
@moduledoc "Filter local activities which have no content"
@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

alias Pleroma.Web.Endpoint



+ 1
- 1
lib/pleroma/web/activity_pub/mrf/no_op_policy.ex View File

@@ -4,7 +4,7 @@

defmodule Pleroma.Web.ActivityPub.MRF.NoOpPolicy do
@moduledoc "Does nothing (lets the messages go through unmodified)"
@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

@impl true
def filter(object) do


+ 1
- 1
lib/pleroma/web/activity_pub/mrf/no_placeholder_text_policy.ex View File

@@ -4,7 +4,7 @@

defmodule Pleroma.Web.ActivityPub.MRF.NoPlaceholderTextPolicy do
@moduledoc "Ensure no content placeholder is present (such as the dot from mastodon)"
@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

@impl true
def filter(


+ 1
- 1
lib/pleroma/web/activity_pub/mrf/normalize_markup.ex View File

@@ -6,7 +6,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.NormalizeMarkup do
@moduledoc "Scrub configured hypertext markup"
alias Pleroma.HTML

@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

@impl true
def filter(%{"type" => "Create", "object" => child_object} = object) do


+ 1
- 1
lib/pleroma/web/activity_pub/mrf/object_age_policy.ex View File

@@ -9,7 +9,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ObjectAgePolicy do
require Pleroma.Constants

@moduledoc "Filter activities depending on their age"
@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

defp check_date(%{"object" => %{"published" => published}} = message) do
with %DateTime{} = now <- DateTime.utc_now(),


+ 16
- 0
lib/pleroma/web/activity_pub/mrf/policy.ex View File

@@ -0,0 +1,16 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only

defmodule Pleroma.Web.ActivityPub.MRF.Policy do
@callback filter(Map.t()) :: {:ok | :reject, Map.t()}
@callback describe() :: {:ok | :error, Map.t()}
@callback config_description() :: %{
optional(:children) => [map()],
key: atom(),
related_policy: String.t(),
label: String.t(),
description: String.t()
}
@optional_callbacks config_description: 0
end

+ 1
- 1
lib/pleroma/web/activity_pub/mrf/reject_non_public.ex View File

@@ -8,7 +8,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.RejectNonPublic do
alias Pleroma.Config
alias Pleroma.User

@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

require Pleroma.Constants



+ 1
- 1
lib/pleroma/web/activity_pub/mrf/simple_policy.ex View File

@@ -4,7 +4,7 @@

defmodule Pleroma.Web.ActivityPub.MRF.SimplePolicy do
@moduledoc "Filter activities depending on their origin instance"
@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

alias Pleroma.Config
alias Pleroma.FollowingRelationship


+ 1
- 1
lib/pleroma/web/activity_pub/mrf/steal_emoji_policy.ex View File

@@ -8,7 +8,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.StealEmojiPolicy do
alias Pleroma.Config

@moduledoc "Detect new emojis by their shortcode and steals them"
@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

defp accept_host?(host), do: host in Config.get([:mrf_steal_emoji, :hosts], [])



+ 1
- 1
lib/pleroma/web/activity_pub/mrf/subchain_policy.ex View File

@@ -8,7 +8,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.SubchainPolicy do

require Logger

@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

defp lookup_subchain(actor) do
with matches <- Config.get([:mrf_subchain, :match_actor]),


+ 1
- 1
lib/pleroma/web/activity_pub/mrf/tag_policy.ex View File

@@ -4,7 +4,7 @@

defmodule Pleroma.Web.ActivityPub.MRF.TagPolicy do
alias Pleroma.User
@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy
@moduledoc """
Apply policies based on user tags



+ 1
- 1
lib/pleroma/web/activity_pub/mrf/user_allow_list_policy.ex View File

@@ -6,7 +6,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.UserAllowListPolicy do
alias Pleroma.Config

@moduledoc "Accept-list of users from specified instances"
@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

defp filter_by_list(object, []), do: {:ok, object}



+ 1
- 1
lib/pleroma/web/activity_pub/mrf/vocabulary_policy.ex View File

@@ -5,7 +5,7 @@
defmodule Pleroma.Web.ActivityPub.MRF.VocabularyPolicy do
@moduledoc "Filter messages which belong to certain activity vocabularies"

@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

@impl true
def filter(%{"type" => "Undo", "object" => child_message} = message) do


+ 1
- 1
test/fixtures/modules/good_mrf.ex View File

@@ -1,5 +1,5 @@
defmodule Fixtures.Modules.GoodMRF do
@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

@impl true
def filter(a), do: {:ok, a}


+ 1
- 1
test/support/mrf_module_mock.ex View File

@@ -3,7 +3,7 @@
# SPDX-License-Identifier: AGPL-3.0-only

defmodule MRFModuleMock do
@behaviour Pleroma.Web.ActivityPub.MRF
@behaviour Pleroma.Web.ActivityPub.MRF.Policy

@impl true
def filter(message), do: {:ok, message}


Loading…
Cancel
Save