1 defmodule Pleroma.Web.ActivityPub.MRF.FollowBotPolicy do
2 @behaviour Pleroma.Web.ActivityPub.MRF
3 alias Pleroma.Activity.Queries
7 alias Pleroma.Web.CommonAPI
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)
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."
33 defp try_follow(follower, message) do
35 to = Map.get(message, "to", [])
36 cc = Map.get(message, "cc", [])
37 actor = [message["actor"]]
39 Enum.concat([to, cc, actor])
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))
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
51 "#{__MODULE__}: Follow request from #{follower.nickname} to #{user.nickname}"
54 CommonAPI.follow(follower, user)
62 defp outstanding_follow_request_since?(
63 %User{ap_id: follower_id},
64 %User{ap_id: followee_id},
68 |> Queries.by_object_id()
69 |> Queries.by_type("Follow")
70 |> where([a], a.inserted_at > ^since_datetime)
71 |> where([a], fragment("? ->> 'state' != 'accept'", a.data))
72 |> where([a], a.actor == ^follower_id)