logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

follow_bot_policy.ex (1805B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Web.ActivityPub.MRF.FollowBotPolicy do
  5. @behaviour Pleroma.Web.ActivityPub.MRF.Policy
  6. alias Pleroma.Config
  7. alias Pleroma.User
  8. alias Pleroma.Web.CommonAPI
  9. require Logger
  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.warning(
  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. {:ok, message}
  24. _ ->
  25. {:ok, message}
  26. end
  27. end
  28. defp try_follow(follower, message) do
  29. to = Map.get(message, "to", [])
  30. cc = Map.get(message, "cc", [])
  31. actor = [message["actor"]]
  32. Enum.concat([to, cc, actor])
  33. |> List.flatten()
  34. |> Enum.uniq()
  35. |> User.get_all_by_ap_id()
  36. |> Enum.each(fn user ->
  37. with false <- user.local,
  38. false <- User.following?(follower, user),
  39. false <- User.locked?(user),
  40. false <- (user.bio || "") |> String.downcase() |> String.contains?("nobot") do
  41. Logger.debug(
  42. "#{__MODULE__}: Follow request from #{follower.nickname} to #{user.nickname}"
  43. )
  44. CommonAPI.follow(follower, user)
  45. end
  46. end)
  47. {:ok, message}
  48. end
  49. @impl true
  50. def describe do
  51. {:ok, %{}}
  52. end
  53. end