Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

60 lines
1.6KB

  1. defmodule Pleroma.Web.ActivityPub.MRF.FollowBotPolicy do
  2. @behaviour Pleroma.Web.ActivityPub.MRF.Policy
  3. alias Pleroma.Config
  4. alias Pleroma.User
  5. alias Pleroma.Web.CommonAPI
  6. require Logger
  7. @impl true
  8. def filter(message) do
  9. with follower_nickname <- Config.get([:mrf_follow_bot, :follower_nickname]),
  10. %User{actor_type: "Service"} = follower <-
  11. User.get_cached_by_nickname(follower_nickname),
  12. %{"type" => "Create", "object" => %{"type" => "Note"}} <- message do
  13. try_follow(follower, message)
  14. else
  15. nil ->
  16. Logger.warn(
  17. "#{__MODULE__} skipped because of missing `:mrf_follow_bot, :follower_nickname` configuration, the :follower_nickname
  18. account does not exist, or the account is not correctly configured as a bot."
  19. )
  20. {:ok, message}
  21. _ ->
  22. {:ok, message}
  23. end
  24. end
  25. defp try_follow(follower, message) do
  26. to = Map.get(message, "to", [])
  27. cc = Map.get(message, "cc", [])
  28. actor = [message["actor"]]
  29. Enum.concat([to, cc, actor])
  30. |> List.flatten()
  31. |> Enum.uniq()
  32. |> User.get_all_by_ap_id()
  33. |> Enum.each(fn user ->
  34. with false <- user.local,
  35. false <- User.following?(follower, user),
  36. false <- User.locked?(user),
  37. false <- (user.bio || "") |> String.downcase() |> String.contains?("nobot") do
  38. Logger.debug(
  39. "#{__MODULE__}: Follow request from #{follower.nickname} to #{user.nickname}"
  40. )
  41. CommonAPI.follow(follower, user)
  42. end
  43. end)
  44. {:ok, message}
  45. end
  46. @impl true
  47. def describe do
  48. {:ok, %{}}
  49. end
  50. end