Try to handle misconfiguration scenarios gracefully
[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.Config
4 alias Pleroma.User
5 alias Pleroma.Web.CommonAPI
6 require Logger
7
8 @impl true
9 def filter(message) do
10 with follower_nickname <- Config.get([:mrf_follow_bot, :follower_nickname]),
11 %User{} = follower <- User.get_cached_by_nickname(follower_nickname),
12 %{"type" => "Create", "object" => %{"type" => "Note"}} <- message do
13 try_follow(follower, message)
14 else
15 nil ->
16 Logger.warn(
17 "#{__MODULE__} skipped because of missing :mrf_follow_bot, :follower_nickname configuration or the account
18 does not exist."
19 )
20
21 {:ok, message}
22
23 _ ->
24 {:ok, message}
25 end
26 end
27
28 defp try_follow(follower, message) do
29 Task.start(fn ->
30 to = Map.get(message, "to", [])
31 cc = Map.get(message, "cc", [])
32 actor = [message["actor"]]
33
34 Enum.concat([to, cc, actor])
35 |> List.flatten()
36 |> User.get_all_by_ap_id()
37 |> Enum.each(fn user ->
38 Logger.info("Checking if #{user.nickname} can be followed")
39
40 with false <- User.following?(follower, user),
41 false <- user.locked,
42 false <- (user.bio || "") |> String.downcase() |> String.contains?("nobot") do
43 Logger.info("Following #{user.nickname}")
44 CommonAPI.follow(follower, user)
45 end
46 end)
47 end)
48
49 {:ok, message}
50 end
51
52 @impl true
53 def describe do
54 {:ok, %{}}
55 end
56 end