total rebase
[anni] / lib / pleroma / web / activity_pub / mrf / force_mention.ex
1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2024 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
4
5 defmodule Pleroma.Web.ActivityPub.MRF.ForceMention do
6   require Pleroma.Constants
7
8   alias Pleroma.Config
9   alias Pleroma.Object
10   alias Pleroma.User
11
12   @behaviour Pleroma.Web.ActivityPub.MRF.Policy
13
14   defp get_author(url) do
15     with %Object{data: %{"actor" => actor}} <- Object.normalize(url, fetch: false),
16          %User{ap_id: ap_id, nickname: nickname} <- User.get_cached_by_ap_id(actor) do
17       %{"type" => "Mention", "href" => ap_id, "name" => "@#{nickname}"}
18     else
19       _ -> nil
20     end
21   end
22
23   defp prepend_author(tags, _, false), do: tags
24
25   defp prepend_author(tags, nil, _), do: tags
26
27   defp prepend_author(tags, url, _) do
28     actor = get_author(url)
29
30     if not is_nil(actor) do
31       [actor | tags]
32     else
33       tags
34     end
35   end
36
37   @impl true
38   def filter(%{"type" => "Create", "object" => %{"tag" => tag} = object} = activity) do
39     tag =
40       tag
41       |> prepend_author(
42         object["inReplyTo"],
43         Config.get([:mrf_force_mention, :mention_parent, true])
44       )
45       |> prepend_author(
46         object["quoteUrl"],
47         Config.get([:mrf_force_mention, :mention_quoted, true])
48       )
49       |> Enum.uniq()
50
51     {:ok, put_in(activity["object"]["tag"], tag)}
52   end
53
54   @impl true
55   def filter(object), do: {:ok, object}
56
57   @impl true
58   def describe, do: {:ok, %{}}
59 end