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])
41 |> User.get_all_by_ap_id()
42 |> Enum.each(fn user ->
43 since_thirty_days_ago = NaiveDateTime.utc_now() |> NaiveDateTime.add(-(86_400 * 30))
45 with false <- User.following?(follower, user),
46 false <- User.locked?(user),
47 false <- (user.bio || "") |> String.downcase() |> String.contains?("nobot"),
48 false <- outstanding_follow_request_since?(follower, user, since_thirty_days_ago) do
49 Logger.info("#{__MODULE__}: Follow request from #{follower.nickname} to #{user.nickname}")
50 CommonAPI.follow(follower, user)
58 defp outstanding_follow_request_since?(
59 %User{ap_id: follower_id},
60 %User{ap_id: followee_id},
64 |> Queries.by_object_id()
65 |> Queries.by_type("Follow")
66 |> where([a], a.inserted_at > ^since_datetime)
67 |> where([a], fragment("? ->> 'state' = 'pending'", a.data))
68 |> where([a], a.actor == ^follower_id)