2fd5d5612e8f3adf1fc4e3d5862aa49cc460bc85
[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.Activity.Queries
4 alias Pleroma.Config
5 alias Pleroma.Repo
6 alias Pleroma.User
7 alias Pleroma.Web.CommonAPI
8 require Logger
9
10 import Ecto.Query
11
12 @impl true
13 def filter(message) do
14 with follower_nickname <- Config.get([:mrf_follow_bot, :follower_nickname]),
15 %User{actor_type: "Service"} = follower <-
16 User.get_cached_by_nickname(follower_nickname),
17 %{"type" => "Create", "object" => %{"type" => "Note"}} <- message do
18 try_follow(follower, message)
19 else
20 nil ->
21 Logger.warn(
22 "#{__MODULE__} skipped because of missing `:mrf_follow_bot, :follower_nickname` configuration, the :follower_nickname
23 account does not exist, or the account is not correctly configured as a bot."
24 )
25
26 {:ok, message}
27
28 _ ->
29 {:ok, message}
30 end
31 end
32
33 defp try_follow(follower, message) do
34 Task.start(fn ->
35 to = Map.get(message, "to", [])
36 cc = Map.get(message, "cc", [])
37 actor = [message["actor"]]
38
39 Enum.concat([to, cc, actor])
40 |> List.flatten()
41 |> Enum.uniq()
42 |> User.get_all_by_ap_id()
43 |> Enum.each(fn user ->
44 since_thirty_days_ago = NaiveDateTime.utc_now() |> NaiveDateTime.add(-(86_400 * 30))
45
46 with false <- User.following?(follower, user),
47 false <- User.locked?(user),
48 false <- (user.bio || "") |> String.downcase() |> String.contains?("nobot"),
49 false <- outstanding_follow_request_since?(follower, user, since_thirty_days_ago) do
50 Logger.info("#{__MODULE__}: Follow request from #{follower.nickname} to #{user.nickname}")
51 CommonAPI.follow(follower, user)
52 end
53 end)
54 end)
55
56 {:ok, message}
57 end
58
59 defp outstanding_follow_request_since?(
60 %User{ap_id: follower_id},
61 %User{ap_id: followee_id},
62 since_datetime
63 ) do
64 followee_id
65 |> Queries.by_object_id()
66 |> Queries.by_type("Follow")
67 |> where([a], a.inserted_at > ^since_datetime)
68 |> where([a], fragment("? ->> 'state' = 'pending'", a.data))
69 |> where([a], a.actor == ^follower_id)
70 |> Repo.exists?()
71 end
72
73 @impl true
74 def describe do
75 {:ok, %{}}
76 end
77 end