logo

pleroma

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

force_bot_unlisted_policy.ex (1533B)


  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.ForceBotUnlistedPolicy do
  5. alias Pleroma.User
  6. @behaviour Pleroma.Web.ActivityPub.MRF.Policy
  7. @moduledoc "Remove bot posts from federated timeline"
  8. require Pleroma.Constants
  9. defp check_by_actor_type(user), do: user.actor_type in ["Application", "Service"]
  10. defp check_by_nickname(user), do: Regex.match?(~r/.bot@|ebooks@/i, user.nickname)
  11. defp check_if_bot(user), do: check_by_actor_type(user) or check_by_nickname(user)
  12. @impl true
  13. def filter(
  14. %{
  15. "type" => "Create",
  16. "to" => to,
  17. "cc" => cc,
  18. "actor" => actor,
  19. "object" => object
  20. } = message
  21. ) do
  22. user = User.get_cached_by_ap_id(actor)
  23. isbot = check_if_bot(user)
  24. if isbot and Enum.member?(to, Pleroma.Constants.as_public()) do
  25. to = List.delete(to, Pleroma.Constants.as_public()) ++ [user.follower_address]
  26. cc = List.delete(cc, user.follower_address) ++ [Pleroma.Constants.as_public()]
  27. object =
  28. object
  29. |> Map.put("to", to)
  30. |> Map.put("cc", cc)
  31. message =
  32. message
  33. |> Map.put("to", to)
  34. |> Map.put("cc", cc)
  35. |> Map.put("object", object)
  36. {:ok, message}
  37. else
  38. {:ok, message}
  39. end
  40. end
  41. @impl true
  42. def filter(message), do: {:ok, message}
  43. @impl true
  44. def describe, do: {:ok, %{}}
  45. end