logo

pleroma

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

no_empty_policy.ex (1730B)


  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.NoEmptyPolicy do
  5. @moduledoc "Filter local activities which have no content"
  6. @behaviour Pleroma.Web.ActivityPub.MRF.Policy
  7. alias Pleroma.Web.Endpoint
  8. @impl true
  9. def filter(%{"actor" => actor} = object) do
  10. with true <- local?(actor),
  11. true <- eligible_type?(object),
  12. true <- note?(object),
  13. false <- has_attachment?(object),
  14. true <- only_mentions?(object) do
  15. {:reject, "[NoEmptyPolicy]"}
  16. else
  17. _ ->
  18. {:ok, object}
  19. end
  20. end
  21. def filter(object), do: {:ok, object}
  22. defp local?(actor) do
  23. if actor |> String.starts_with?("#{Endpoint.url()}") do
  24. true
  25. else
  26. false
  27. end
  28. end
  29. defp has_attachment?(%{
  30. "object" => %{"type" => "Note", "attachment" => attachments}
  31. })
  32. when length(attachments) > 0,
  33. do: true
  34. defp has_attachment?(_), do: false
  35. defp only_mentions?(%{"object" => %{"type" => "Note", "source" => source}}) do
  36. source =
  37. case source do
  38. %{"content" => text} -> text
  39. _ -> source
  40. end
  41. non_mentions =
  42. source |> String.split() |> Enum.filter(&(not String.starts_with?(&1, "@"))) |> length
  43. if non_mentions > 0 do
  44. false
  45. else
  46. true
  47. end
  48. end
  49. defp only_mentions?(_), do: false
  50. defp note?(%{"object" => %{"type" => "Note"}}), do: true
  51. defp note?(_), do: false
  52. defp eligible_type?(%{"type" => type}) when type in ["Create", "Update"], do: true
  53. defp eligible_type?(_), do: false
  54. @impl true
  55. def describe, do: {:ok, %{}}
  56. end