Fix tagpolicy to also work with Update
[akkoma] / lib / pleroma / web / activity_pub / mrf / follow_bot_policy.ex
index d10b7b480190005b18c86c428673b8d3ce09732a..7cf7de06834d015b3471e744c07d225edd544729 100644 (file)
@@ -1,8 +1,9 @@
 defmodule Pleroma.Web.ActivityPub.MRF.FollowBotPolicy do
-  @behaviour Pleroma.Web.ActivityPub.MRF
+  @behaviour Pleroma.Web.ActivityPub.MRF.Policy
   alias Pleroma.Config
   alias Pleroma.User
   alias Pleroma.Web.CommonAPI
+
   require Logger
 
   @impl true
@@ -27,24 +28,25 @@ defmodule Pleroma.Web.ActivityPub.MRF.FollowBotPolicy do
   end
 
   defp try_follow(follower, message) do
-    Task.start(fn ->
-      to = Map.get(message, "to", [])
-      cc = Map.get(message, "cc", [])
-      actor = [message["actor"]]
-
-      Enum.concat([to, cc, actor])
-      |> List.flatten()
-      |> User.get_all_by_ap_id()
-      |> Enum.each(fn user ->
-        Logger.info("Checking if #{user.nickname} can be followed")
-
-        with false <- User.following?(follower, user),
-             false <- user.locked,
-             false <- (user.bio || "") |> String.downcase() |> String.contains?("nobot") do
-          Logger.info("Following #{user.nickname}")
-          CommonAPI.follow(follower, user)
-        end
-      end)
+    to = Map.get(message, "to", [])
+    cc = Map.get(message, "cc", [])
+    actor = [message["actor"]]
+
+    Enum.concat([to, cc, actor])
+    |> List.flatten()
+    |> Enum.uniq()
+    |> User.get_all_by_ap_id()
+    |> Enum.each(fn user ->
+      with false <- user.local,
+           false <- User.following?(follower, user),
+           false <- User.locked?(user),
+           false <- (user.bio || "") |> String.downcase() |> String.contains?("nobot") do
+        Logger.debug(
+          "#{__MODULE__}: Follow request from #{follower.nickname} to #{user.nickname}"
+        )
+
+        CommonAPI.follow(follower, user)
+      end
     end)
 
     {:ok, message}