1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.AntiFollowbotPolicy do
8 @moduledoc "Prevent followbots from following with a bit of heuristic"
10 @behaviour Pleroma.Web.ActivityPub.MRF
12 # XXX: this should become User.normalize_by_ap_id() or similar, really.
13 defp normalize_by_ap_id(%{"id" => id}), do: User.get_cached_by_ap_id(id)
14 defp normalize_by_ap_id(uri) when is_binary(uri), do: User.get_cached_by_ap_id(uri)
15 defp normalize_by_ap_id(_), do: nil
17 defp score_nickname("followbot@" <> _), do: 1.0
18 defp score_nickname("federationbot@" <> _), do: 1.0
19 defp score_nickname("federation_bot@" <> _), do: 1.0
20 defp score_nickname(_), do: 0.0
22 defp score_displayname("federation bot"), do: 1.0
23 defp score_displayname("federationbot"), do: 1.0
24 defp score_displayname("fedibot"), do: 1.0
25 defp score_displayname(_), do: 0.0
27 defp determine_if_followbot(%User{nickname: nickname, name: displayname}) do
28 # nickname will be a binary string except when following a relay
30 if is_binary(nickname) do
38 # displayname will either be a binary string or nil, if a displayname isn't set.
40 if is_binary(displayname) do
43 |> score_displayname()
48 nick_score + name_score
51 defp determine_if_followbot(_), do: 0.0
54 def filter(%{"type" => "Follow", "actor" => actor_id} = message) do
55 %User{} = actor = normalize_by_ap_id(actor_id)
57 score = determine_if_followbot(actor)
59 # TODO: scan biography data for keywords and score it somehow.
63 {:reject, "[AntiFollowbotPolicy] Scored #{actor_id} as #{score}"}
68 def filter(message), do: {:ok, message}
71 def describe, do: {:ok, %{}}