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.

73 lines
2.1KB

  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.AntiFollowbotPolicy do
  5. alias Pleroma.User
  6. @moduledoc "Prevent followbots from following with a bit of heuristic"
  7. @behaviour Pleroma.Web.ActivityPub.MRF.Policy
  8. # XXX: this should become User.normalize_by_ap_id() or similar, really.
  9. defp normalize_by_ap_id(%{"id" => id}), do: User.get_cached_by_ap_id(id)
  10. defp normalize_by_ap_id(uri) when is_binary(uri), do: User.get_cached_by_ap_id(uri)
  11. defp normalize_by_ap_id(_), do: nil
  12. defp score_nickname("followbot@" <> _), do: 1.0
  13. defp score_nickname("federationbot@" <> _), do: 1.0
  14. defp score_nickname("federation_bot@" <> _), do: 1.0
  15. defp score_nickname(_), do: 0.0
  16. defp score_displayname("federation bot"), do: 1.0
  17. defp score_displayname("federationbot"), do: 1.0
  18. defp score_displayname("fedibot"), do: 1.0
  19. defp score_displayname(_), do: 0.0
  20. defp determine_if_followbot(%User{nickname: nickname, name: displayname}) do
  21. # nickname will be a binary string except when following a relay
  22. nick_score =
  23. if is_binary(nickname) do
  24. nickname
  25. |> String.downcase()
  26. |> score_nickname()
  27. else
  28. 0.0
  29. end
  30. # displayname will either be a binary string or nil, if a displayname isn't set.
  31. name_score =
  32. if is_binary(displayname) do
  33. displayname
  34. |> String.downcase()
  35. |> score_displayname()
  36. else
  37. 0.0
  38. end
  39. nick_score + name_score
  40. end
  41. defp determine_if_followbot(_), do: 0.0
  42. @impl true
  43. def filter(%{"type" => "Follow", "actor" => actor_id} = message) do
  44. %User{} = actor = normalize_by_ap_id(actor_id)
  45. score = determine_if_followbot(actor)
  46. # TODO: scan biography data for keywords and score it somehow.
  47. if score < 0.8 do
  48. {:ok, message}
  49. else
  50. {:reject, "[AntiFollowbotPolicy] Scored #{actor_id} as #{score}"}
  51. end
  52. end
  53. @impl true
  54. def filter(message), do: {:ok, message}
  55. @impl true
  56. def describe, do: {:ok, %{}}
  57. end