Do not try to follow local users. Their posts are already available locally on the...
[akkoma] / lib / pleroma / web / activity_pub / mrf / followbot_policy.ex
1 defmodule Pleroma.Web.ActivityPub.MRF.FollowbotPolicy do
2 @behaviour Pleroma.Web.ActivityPub.MRF
3 alias Pleroma.Activity
4 alias Pleroma.Config
5 alias Pleroma.User
6 alias Pleroma.Web.CommonAPI
7
8 require Logger
9
10 @impl true
11 def filter(message) do
12 with follower_nickname <- Config.get([:mrf_follow_bot, :follower_nickname]),
13 %User{actor_type: "Service"} = follower <-
14 User.get_cached_by_nickname(follower_nickname),
15 %{"type" => "Create", "object" => %{"type" => "Note"}} <- message do
16 try_follow(follower, message)
17 else
18 nil ->
19 Logger.warn(
20 "#{__MODULE__} skipped because of missing `:mrf_follow_bot, :follower_nickname` configuration, the :follower_nickname
21 account does not exist, or the account is not correctly configured as a bot."
22 )
23
24 {:ok, message}
25
26 _ ->
27 {:ok, message}
28 end
29 end
30
31 defp try_follow(follower, message) do
32 Task.start(fn ->
33 to = Map.get(message, "to", [])
34 cc = Map.get(message, "cc", [])
35 actor = [message["actor"]]
36
37 Enum.concat([to, cc, actor])
38 |> List.flatten()
39 |> Enum.uniq()
40 |> User.get_all_by_ap_id()
41 |> Enum.each(fn user ->
42 since_thirty_days_ago = NaiveDateTime.utc_now() |> NaiveDateTime.add(-(86_400 * 30))
43
44 with false <- user.local,
45 false <- User.following?(follower, user),
46 false <- User.locked?(user),
47 false <- (user.bio || "") |> String.downcase() |> String.contains?("nobot"),
48 false <-
49 Activity.follow_requests_outstanding_since?(follower, user, since_thirty_days_ago) do
50 Logger.info(
51 "#{__MODULE__}: Follow request from #{follower.nickname} to #{user.nickname}"
52 )
53
54 CommonAPI.follow(follower, user)
55 end
56 end)
57 end)
58
59 {:ok, message}
60 end
61
62 @impl true
63 def describe do
64 {:ok, %{}}
65 end
66 end