move to 2.5.5
[anni] / lib / pleroma / web / activity_pub / mrf / force_mentions_in_content.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.ForceMentionsInContent do
6   require Pleroma.Constants
7
8   alias Pleroma.Formatter
9   alias Pleroma.Object
10   alias Pleroma.User
11
12   @behaviour Pleroma.Web.ActivityPub.MRF.Policy
13
14   @impl true
15   def history_awareness, do: :auto
16
17   defp do_extract({:a, attrs, _}, acc) do
18     if Enum.find(attrs, fn {name, value} ->
19          name == "class" && value in ["mention", "u-url mention", "mention u-url"]
20        end) do
21       href = Enum.find(attrs, fn {name, _} -> name == "href" end) |> elem(1)
22       acc ++ [href]
23     else
24       acc
25     end
26   end
27
28   defp do_extract({_, _, children}, acc) do
29     do_extract(children, acc)
30   end
31
32   defp do_extract(nodes, acc) when is_list(nodes) do
33     Enum.reduce(nodes, acc, fn node, acc -> do_extract(node, acc) end)
34   end
35
36   defp do_extract(_, acc), do: acc
37
38   defp extract_mention_uris_from_content(content) do
39     {:ok, tree} = :fast_html.decode(content, format: [:html_atoms])
40     do_extract(tree, [])
41   end
42
43   defp get_replied_to_user(%{"inReplyTo" => in_reply_to}) do
44     case Object.normalize(in_reply_to, fetch: false) do
45       %Object{data: %{"actor" => actor}} -> User.get_cached_by_ap_id(actor)
46       _ -> nil
47     end
48   end
49
50   defp get_replied_to_user(_object), do: nil
51
52   # Ensure the replied-to user is sorted to the left
53   defp sort_replied_user([%User{id: user_id} | _] = users, %User{id: user_id}), do: users
54
55   defp sort_replied_user(users, %User{id: user_id} = user) do
56     if Enum.find(users, fn u -> u.id == user_id end) do
57       users = Enum.reject(users, fn u -> u.id == user_id end)
58       [user | users]
59     else
60       users
61     end
62   end
63
64   defp sort_replied_user(users, _), do: users
65
66   # Drop constants and the actor's own AP ID
67   defp clean_recipients(recipients, object) do
68     Enum.reject(recipients, fn ap_id ->
69       ap_id in [
70         object["object"]["actor"],
71         Pleroma.Constants.as_public(),
72         Pleroma.Web.ActivityPub.Utils.as_local_public()
73       ]
74     end)
75   end
76
77   @impl true
78   def filter(
79         %{
80           "type" => type,
81           "object" => %{"type" => "Note", "to" => to, "inReplyTo" => in_reply_to}
82         } = object
83       )
84       when type in ["Create", "Update"] and is_list(to) and is_binary(in_reply_to) do
85     # image-only posts from pleroma apparently reach this MRF without the content field
86     content = object["object"]["content"] || ""
87
88     # Get the replied-to user for sorting
89     replied_to_user = get_replied_to_user(object["object"])
90
91     mention_users =
92       to
93       |> clean_recipients(object)
94       |> Enum.map(&User.get_cached_by_ap_id/1)
95       |> Enum.reject(&is_nil/1)
96       |> sort_replied_user(replied_to_user)
97
98     explicitly_mentioned_uris = extract_mention_uris_from_content(content)
99
100     added_mentions =
101       Enum.reduce(mention_users, "", fn %User{ap_id: uri} = user, acc ->
102         unless uri in explicitly_mentioned_uris do
103           acc <> Formatter.mention_from_user(user, %{mentions_format: :compact}) <> " "
104         else
105           acc
106         end
107       end)
108
109     recipients_inline =
110       if added_mentions != "",
111         do: "<span class=\"recipients-inline\">#{added_mentions}</span>",
112         else: ""
113
114     content =
115       cond do
116         # For Markdown posts, insert the mentions inside the first <p> tag
117         recipients_inline != "" && String.starts_with?(content, "<p>") ->
118           "<p>" <> recipients_inline <> String.trim_leading(content, "<p>")
119
120         recipients_inline != "" ->
121           recipients_inline <> content
122
123         true ->
124           content
125       end
126
127     {:ok, put_in(object["object"]["content"], content)}
128   end
129
130   @impl true
131   def filter(object), do: {:ok, object}
132
133   @impl true
134   def describe, do: {:ok, %{}}
135 end