ForceBotUnlistedPolicy: format
[akkoma] / lib / pleroma / web / activity_pub / mrf / force_bot_unlisted_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2020 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
8 @moduledoc "Remove bot posts from federated timeline"
9
10 require Pleroma.Constants
11
12 defp check_by_actor_type(user) do
13 if user.actor_type in ["Application", "Service"], do: 1.0, else: 0.0
14 end
15
16 defp check_by_nickname(user) do
17 if Regex.match?(~r/bot@|ebooks@/i, user.nickname), do: 1.0, else: 0.0
18 end
19
20 defp botness_score(user), do: check_by_actor_type(user) + check_by_nickname(user)
21
22 @impl true
23 def filter(
24 %{
25 "type" => "Create",
26 "to" => to,
27 "cc" => cc,
28 "actor" => actor,
29 "object" => object
30 } = message
31 ) do
32 user = User.get_cached_by_ap_id(actor)
33 isbot = 0.8 < botness_score(user)
34
35 if isbot and Enum.member?(to, Pleroma.Constants.as_public()) do
36 to = List.delete(to, Pleroma.Constants.as_public()) ++ [user.follower_address]
37 cc = List.delete(cc, user.follower_address) ++ [Pleroma.Constants.as_public()]
38
39 object =
40 object
41 |> Map.put("to", to)
42 |> Map.put("cc", cc)
43
44 message =
45 message
46 |> Map.put("to", to)
47 |> Map.put("cc", cc)
48 |> Map.put("object", object)
49
50 {:ok, message}
51 else
52 {:ok, message}
53 end
54 end
55
56 @impl true
57 def filter(message), do: {:ok, message}
58
59 @impl true
60 def describe, do: {:ok, %{}}
61 end