logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

subchain_policy.ex (1849B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.ActivityPub.MRF.SubchainPolicy do
  5. alias Pleroma.Config
  6. alias Pleroma.Web.ActivityPub.MRF
  7. require Logger
  8. @behaviour Pleroma.Web.ActivityPub.MRF.Policy
  9. defp lookup_subchain(actor) do
  10. with matches <- Config.get([:mrf_subchain, :match_actor]),
  11. {match, subchain} <- Enum.find(matches, fn {k, _v} -> String.match?(actor, k) end) do
  12. {:ok, match, subchain}
  13. else
  14. _e -> {:error, :notfound}
  15. end
  16. end
  17. @impl true
  18. def filter(%{"actor" => actor} = message) do
  19. with {:ok, match, subchain} <- lookup_subchain(actor) do
  20. Logger.debug(
  21. "[SubchainPolicy] Matched #{actor} against #{inspect(match)} with subchain #{inspect(subchain)}"
  22. )
  23. MRF.filter(subchain, message)
  24. else
  25. _e -> {:ok, message}
  26. end
  27. end
  28. @impl true
  29. def filter(message), do: {:ok, message}
  30. @impl true
  31. def describe, do: {:ok, %{}}
  32. @impl true
  33. def config_description do
  34. %{
  35. key: :mrf_subchain,
  36. related_policy: "Pleroma.Web.ActivityPub.MRF.SubchainPolicy",
  37. label: "MRF Subchain",
  38. description:
  39. "This policy processes messages through an alternate pipeline when a given message matches certain criteria." <>
  40. " All criteria are configured as a map of regular expressions to lists of policy modules.",
  41. children: [
  42. %{
  43. key: :match_actor,
  44. type: {:map, {:list, :string}},
  45. description: "Matches a series of regular expressions against the actor field",
  46. suggestions: [
  47. %{
  48. ~r/https:\/\/example.com/s => [Pleroma.Web.ActivityPub.MRF.DropPolicy]
  49. }
  50. ]
  51. }
  52. ]
  53. }
  54. end
  55. end