8cec8eabe97e2437e90c2e6c88329b9020a651cc
[anni] / lib / pleroma / web / activity_pub / mrf / force_bot_unlisted_policy.ex
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
5 defmodule Pleroma.Web.ActivityPub.MRF.ForceBotUnlistedPolicy do
6   alias Pleroma.User
7   @behaviour Pleroma.Web.ActivityPub.MRF.Policy
8   @moduledoc "Remove bot posts from federated timeline"
9
10   require Pleroma.Constants
11
12   defp check_by_actor_type(user), do: user.actor_type in ["Application", "Service"]
13   defp check_by_nickname(user), do: Regex.match?(~r/.bot@|ebooks@/i, user.nickname)
14
15   defp check_if_bot(user), do: check_by_actor_type(user) or check_by_nickname(user)
16
17   @impl true
18   def filter(
19         %{
20           "type" => "Create",
21           "to" => to,
22           "cc" => cc,
23           "actor" => actor,
24           "object" => object
25         } = message
26       ) do
27     user = User.get_cached_by_ap_id(actor)
28     isbot = check_if_bot(user)
29
30     if isbot and Enum.member?(to, Pleroma.Constants.as_public()) do
31       to = List.delete(to, Pleroma.Constants.as_public()) ++ [user.follower_address]
32       cc = List.delete(cc, user.follower_address) ++ [Pleroma.Constants.as_public()]
33
34       object =
35         object
36         |> Map.put("to", to)
37         |> Map.put("cc", cc)
38
39       message =
40         message
41         |> Map.put("to", to)
42         |> Map.put("cc", cc)
43         |> Map.put("object", object)
44
45       {:ok, message}
46     else
47       {:ok, message}
48     end
49   end
50
51   @impl true
52   def filter(message), do: {:ok, message}
53
54   @impl true
55   def describe, do: {:ok, %{}}
56 end