fb123dbd3c4bb1a815c878d4baf717ce75a1ebda
[akkoma] / lib / pleroma / web / activity_pub / mrf / follow_bot_policy.ex
1 defmodule Pleroma.Web.ActivityPub.MRF.FollowBotPolicy do
2 @behaviour Pleroma.Web.ActivityPub.MRF
3 alias Pleroma.User
4 alias Pleroma.Web.CommonAPI
5 require Logger
6
7 @impl true
8 def filter(message) do
9 Task.start(fn ->
10 follower_nickname = Pleroma.Config.get([:mrf_follow_bot, :follower_nickname])
11
12 with %User{} = follower <- User.get_cached_by_nickname(follower_nickname),
13 %{"type" => "Create", "object" => %{"type" => "Note"}} <- message do
14 to = Map.get(message, "to", [])
15 cc = Map.get(message, "cc", [])
16 actor = [message["actor"]]
17
18 Enum.concat([to, cc, actor])
19 |> List.flatten()
20 |> User.get_all_by_ap_id()
21 |> Enum.each(fn user ->
22 Logger.info("Checking if #{user.nickname} can be followed")
23
24 with false <- User.following?(follower, user),
25 false <- user.locked,
26 false <- (user.bio || "") |> String.downcase() |> String.contains?("nobot") do
27 Logger.info("Following #{user.nickname}")
28 CommonAPI.follow(follower, user)
29 end
30 end)
31 end
32 end)
33
34 {:ok, message}
35 end
36
37 @impl true
38 def describe do
39 {:ok, %{}}
40 end
41 end