Fix MRF policies to also work with Update
[akkoma] / lib / pleroma / web / activity_pub / mrf / force_bot_unlisted_policy.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2021 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" => type,
21 "to" => to,
22 "cc" => cc,
23 "actor" => actor,
24 "object" => object
25 } = message
26 )
27 when type in ["Create", "Update"] do
28 user = User.get_cached_by_ap_id(actor)
29 isbot = check_if_bot(user)
30
31 if isbot and Enum.member?(to, Pleroma.Constants.as_public()) do
32 to = List.delete(to, Pleroma.Constants.as_public()) ++ [user.follower_address]
33 cc = List.delete(cc, user.follower_address) ++ [Pleroma.Constants.as_public()]
34
35 object =
36 object
37 |> Map.put("to", to)
38 |> Map.put("cc", cc)
39
40 message =
41 message
42 |> Map.put("to", to)
43 |> Map.put("cc", cc)
44 |> Map.put("object", object)
45
46 {:ok, message}
47 else
48 {:ok, message}
49 end
50 end
51
52 @impl true
53 def filter(message), do: {:ok, message}
54
55 @impl true
56 def describe, do: {:ok, %{}}
57 end