logo

pleroma

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

anti_link_spam_policy.ex (1688B)


  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.AntiLinkSpamPolicy do
  5. alias Pleroma.User
  6. @behaviour Pleroma.Web.ActivityPub.MRF.Policy
  7. require Logger
  8. @impl true
  9. def history_awareness, do: :auto
  10. # has the user successfully posted before?
  11. defp old_user?(%User{} = u) do
  12. u.note_count > 0 || u.follower_count > 0
  13. end
  14. # does the post contain links?
  15. defp contains_links?(%{"content" => content} = _object) do
  16. content
  17. |> Floki.parse_fragment!()
  18. |> Floki.filter_out("a.mention,a.hashtag,a[rel~=\"tag\"],a.zrl")
  19. |> Floki.attribute("a", "href")
  20. |> length() > 0
  21. end
  22. defp contains_links?(_), do: false
  23. @impl true
  24. def filter(%{"type" => "Create", "actor" => actor, "object" => object} = message) do
  25. with {:ok, %User{local: false} = u} <- User.get_or_fetch_by_ap_id(actor),
  26. {:contains_links, true} <- {:contains_links, contains_links?(object)},
  27. {:old_user, true} <- {:old_user, old_user?(u)} do
  28. {:ok, message}
  29. else
  30. {:ok, %User{local: true}} ->
  31. {:ok, message}
  32. {:contains_links, false} ->
  33. {:ok, message}
  34. {:old_user, false} ->
  35. {:reject, "[AntiLinkSpamPolicy] User has no posts nor followers"}
  36. {:error, _} ->
  37. {:reject, "[AntiLinkSpamPolicy] Failed to get or fetch user by ap_id"}
  38. e ->
  39. {:reject, "[AntiLinkSpamPolicy] Unhandled error #{inspect(e)}"}
  40. end
  41. end
  42. # in all other cases, pass through
  43. def filter(message), do: {:ok, message}
  44. @impl true
  45. def describe, do: {:ok, %{}}
  46. end