MRF: create MRF.Policy behaviour separate from MRF module
[akkoma] / lib / pleroma / web / activity_pub / mrf / anti_followbot_policy.ex
index 34665a3a6999491b1ff3847bab127031e5f513c8..851e95d226e7f5b216c500e46a194e5333f621e1 100644 (file)
@@ -1,11 +1,13 @@
 # Pleroma: A lightweight social networking server
-# Copyright © 2019 Pleroma Authors <https://pleroma.social/>
+# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
 # SPDX-License-Identifier: AGPL-3.0-only
 
 defmodule Pleroma.Web.ActivityPub.MRF.AntiFollowbotPolicy do
   alias Pleroma.User
 
-  @behaviour Pleroma.Web.ActivityPub.MRF
+  @moduledoc "Prevent followbots from following with a bit of heuristic"
+
+  @behaviour Pleroma.Web.ActivityPub.MRF.Policy
 
   # XXX: this should become User.normalize_by_ap_id() or similar, really.
   defp normalize_by_ap_id(%{"id" => id}), do: User.get_cached_by_ap_id(id)
@@ -23,11 +25,15 @@ defmodule Pleroma.Web.ActivityPub.MRF.AntiFollowbotPolicy do
   defp score_displayname(_), do: 0.0
 
   defp determine_if_followbot(%User{nickname: nickname, name: displayname}) do
-    # nickname will always be a binary string because it's generated by Pleroma.
+    # nickname will be a binary string except when following a relay
     nick_score =
-      nickname
-      |> String.downcase()
-      |> score_nickname()
+      if is_binary(nickname) do
+        nickname
+        |> String.downcase()
+        |> score_nickname()
+      else
+        0.0
+      end
 
     # displayname will either be a binary string or nil, if a displayname isn't set.
     name_score =
@@ -54,10 +60,13 @@ defmodule Pleroma.Web.ActivityPub.MRF.AntiFollowbotPolicy do
     if score < 0.8 do
       {:ok, message}
     else
-      {:reject, nil}
+      {:reject, "[AntiFollowbotPolicy] Scored #{actor_id} as #{score}"}
     end
   end
 
   @impl true
   def filter(message), do: {:ok, message}
+
+  @impl true
+  def describe, do: {:ok, %{}}
 end