1 # Pleroma: A lightweight social networking server
2 # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
3 # SPDX-License-Identifier: AGPL-3.0-only
5 defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do
6 require Pleroma.Constants
8 alias Pleroma.Formatter
12 @behaviour Pleroma.Web.ActivityPub.MRF.Policy
15 def history_awareness, do: :auto
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"]
21 href = Enum.find(attrs, fn {name, _} -> name == "href" end) |> elem(1)
28 defp do_extract({_, _, children}, acc) do
29 do_extract(children, acc)
32 defp do_extract(nodes, acc) when is_list(nodes) do
33 Enum.reduce(nodes, acc, fn node, acc -> do_extract(node, acc) end)
36 defp do_extract(_, acc), do: acc
38 defp extract_mention_uris_from_content(content) do
39 {:ok, tree} = :fast_html.decode(content, format: [:html_atoms])
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)
50 defp get_replied_to_user(_object), do: nil
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
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)
64 defp sort_replied_user(users, _), do: users
66 # Drop constants and the actor's own AP ID
67 defp clean_recipients(recipients, object) do
68 Enum.reject(recipients, fn ap_id ->
70 object["object"]["actor"],
71 Pleroma.Constants.as_public(),
72 Pleroma.Web.ActivityPub.Utils.as_local_public()
81 "object" => %{"type" => "Note", "to" => to, "inReplyTo" => in_reply_to}
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"] || ""
88 # Get the replied-to user for sorting
89 replied_to_user = get_replied_to_user(object["object"])
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)
98 explicitly_mentioned_uris = extract_mention_uris_from_content(content)
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}) <> " "
110 if added_mentions != "",
111 do: "<span class=\"recipients-inline\">#{added_mentions}</span>",
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>")
120 recipients_inline != "" ->
121 recipients_inline <> content
127 {:ok, put_in(object["object"]["content"], content)}
131 def filter(object), do: {:ok, object}
134 def describe, do: {:ok, %{}}