Merge remote-tracking branch 'pleroma/develop' into cycles-streaming
[akkoma] / lib / pleroma / web / activity_pub / mrf / anti_followbot_policy.ex
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
5 defmodule Pleroma.Web.ActivityPub.MRF.AntiFollowbotPolicy do
6 alias Pleroma.User
7
8 @moduledoc "Prevent followbots from following with a bit of heuristic"
9
10 @behaviour Pleroma.Web.ActivityPub.MRF
11
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
16
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
21
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
26
27 defp determine_if_followbot(%User{nickname: nickname, name: displayname}) do
28 # nickname will be a binary string except when following a relay
29 nick_score =
30 if is_binary(nickname) do
31 nickname
32 |> String.downcase()
33 |> score_nickname()
34 else
35 0.0
36 end
37
38 # displayname will either be a binary string or nil, if a displayname isn't set.
39 name_score =
40 if is_binary(displayname) do
41 displayname
42 |> String.downcase()
43 |> score_displayname()
44 else
45 0.0
46 end
47
48 nick_score + name_score
49 end
50
51 defp determine_if_followbot(_), do: 0.0
52
53 @impl true
54 def filter(%{"type" => "Follow", "actor" => actor_id} = message) do
55 %User{} = actor = normalize_by_ap_id(actor_id)
56
57 score = determine_if_followbot(actor)
58
59 # TODO: scan biography data for keywords and score it somehow.
60 if score < 0.8 do
61 {:ok, message}
62 else
63 {:reject, "[AntiFollowbotPolicy] Scored #{actor_id} as #{score}"}
64 end
65 end
66
67 @impl true
68 def filter(message), do: {:ok, message}
69
70 @impl true
71 def describe, do: {:ok, %{}}
72 end