Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.5KB

  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2021 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
  7. alias Pleroma.Web.Endpoint
  8. @impl true
  9. def filter(%{"actor" => actor} = object) do
  10. with true <- is_local?(actor),
  11. true <- is_note?(object),
  12. false <- has_attachment?(object),
  13. true <- only_mentions?(object) do
  14. {:reject, "[NoEmptyPolicy]"}
  15. else
  16. _ ->
  17. {:ok, object}
  18. end
  19. end
  20. def filter(object), do: {:ok, object}
  21. defp is_local?(actor) do
  22. if actor |> String.starts_with?("#{Endpoint.url()}") do
  23. true
  24. else
  25. false
  26. end
  27. end
  28. defp has_attachment?(%{
  29. "type" => "Create",
  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?(%{"type" => "Create", "object" => %{"type" => "Note", "source" => source}}) do
  36. non_mentions =
  37. source |> String.split() |> Enum.filter(&(not String.starts_with?(&1, "@"))) |> length
  38. if non_mentions > 0 do
  39. false
  40. else
  41. true
  42. end
  43. end
  44. defp only_mentions?(_), do: false
  45. defp is_note?(%{"type" => "Create", "object" => %{"type" => "Note"}}), do: true
  46. defp is_note?(_), do: false
  47. @impl true
  48. def describe, do: {:ok, %{}}
  49. end