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