summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Gleason <alex@alexgleason.me>2022-01-25 10:42:34 -0600
committerAlex Gleason <alex@alexgleason.me>2022-01-25 10:42:34 -0600
commit65b4d2ce8450098ee2eaa2057968a4f47aa536c1 (patch)
treec10aa2d8234cdd2e9740585f173bb3eb4448acb7
parent267184b70e587a3e6301d3a01bd16fd465bc0f68 (diff)
ForceMentionsInContent: fix order of mentions
-rw-r--r--lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex38
-rw-r--r--test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs44
2 files changed, 75 insertions, 7 deletions
diff --git a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex
index 1d750d4c2..c9b022cf8 100644
--- a/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex
+++ b/lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex
@@ -4,6 +4,7 @@
defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do
alias Pleroma.Formatter
+ alias Pleroma.Object
alias Pleroma.User
@behaviour Pleroma.Web.ActivityPub.MRF.Policy
@@ -34,21 +35,48 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do
do_extract(tree, [])
end
+ defp get_replied_to_user(%{"inReplyTo" => in_reply_to}) do
+ case Object.normalize(in_reply_to, fetch: false) do
+ %Object{data: %{"actor" => actor}} -> User.get_cached_by_ap_id(actor)
+ _ -> nil
+ end
+ end
+
+ defp get_replied_to_user(_object), do: nil
+
+ # Ensure the replied-to user is sorted to the left
+ defp sort_replied_user([%User{id: user_id} | _] = users, %User{id: user_id}), do: users
+
+ defp sort_replied_user(users, %User{id: user_id} = user) do
+ if Enum.find(users, fn u -> u.id == user_id end) do
+ users = Enum.reject(users, fn u -> u.id == user_id end)
+ [user | users]
+ else
+ users
+ end
+ end
+
+ defp sort_replied_user(users, _), do: users
+
@impl true
- def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = object) do
+ def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = object)
+ when is_list(to) do
# image-only posts from pleroma apparently reach this MRF without the content field
content = object["object"]["content"] || ""
+ # Get the replied-to user for sorting
+ replied_to_user = get_replied_to_user(object["object"])
+
mention_users =
to
- |> Enum.map(&{&1, User.get_cached_by_ap_id(&1)})
- |> Enum.reject(fn {_, user} -> is_nil(user) end)
- |> Enum.into(%{})
+ |> Enum.map(&User.get_cached_by_ap_id/1)
+ |> Enum.reject(&is_nil/1)
+ |> sort_replied_user(replied_to_user)
explicitly_mentioned_uris = extract_mention_uris_from_content(content)
added_mentions =
- Enum.reduce(mention_users, "", fn {uri, user}, acc ->
+ Enum.reduce(mention_users, "", fn %User{ap_id: uri} = user, acc ->
unless uri in explicitly_mentioned_uris do
acc <> Formatter.mention_from_user(user, %{mentions_format: :compact}) <> " "
else
diff --git a/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs b/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs
index 3c40887c4..3bc07be94 100644
--- a/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs
+++ b/test/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs
@@ -3,9 +3,15 @@
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContentTest do
+ use Pleroma.DataCase
+ require Pleroma.Constants
+
+ alias Pleroma.Constants
+ alias Pleroma.Object
alias Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent
+ alias Pleroma.Web.CommonAPI
+
import Pleroma.Factory
- use Pleroma.DataCase
test "adds mentions to post content" do
[lain, coolboymew, dielan, hakui, fence] = [
@@ -43,6 +49,40 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContentTest do
{:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity)
assert filtered ==
- "<span class=\"recipients-inline\"><span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{lain.id}\" href=\"https://lain.com/users/lain\" rel=\"ugc\">@<span>lain</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{coolboymew.id}\" href=\"https://shitposter.club/users/coolboymew\" rel=\"ugc\">@<span>coolboymew</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{dielan.id}\" href=\"https://shitposter.club/users/dielan\" rel=\"ugc\">@<span>dielan</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{hakui.id}\" href=\"https://tuusin.misono-ya.info/users/hakui\" rel=\"ugc\">@<span>hakui</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{fence.id}\" href=\"https://xyzzy.link/users/fence\" rel=\"ugc\">@<span>fence</span></a></span> </span><p>Haha yeah, you can control who you reply to.</p>"
+ "<span class=\"recipients-inline\"><span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{dielan.id}\" href=\"https://shitposter.club/users/dielan\" rel=\"ugc\">@<span>dielan</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{coolboymew.id}\" href=\"https://shitposter.club/users/coolboymew\" rel=\"ugc\">@<span>coolboymew</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{fence.id}\" href=\"https://xyzzy.link/users/fence\" rel=\"ugc\">@<span>fence</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{hakui.id}\" href=\"https://tuusin.misono-ya.info/users/hakui\" rel=\"ugc\">@<span>hakui</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{lain.id}\" href=\"https://lain.com/users/lain\" rel=\"ugc\">@<span>lain</span></a></span> </span><p>Haha yeah, you can control who you reply to.</p>"
+ end
+
+ test "the replied-to user is sorted to the left" do
+ [mario, luigi, wario] = [
+ insert(:user, nickname: "mario"),
+ insert(:user, nickname: "luigi"),
+ insert(:user, nickname: "wario")
+ ]
+
+ {:ok, post1} = CommonAPI.post(mario, %{status: "Letsa go!"})
+
+ {:ok, post2} =
+ CommonAPI.post(luigi, %{status: "Oh yaah", in_reply_to_id: post1.id, to: [mario.ap_id]})
+
+ activity = %{
+ "type" => "Create",
+ "actor" => wario.ap_id,
+ "object" => %{
+ "type" => "Note",
+ "actor" => wario.ap_id,
+ "content" => "WHA-HA!",
+ "to" => [
+ mario.ap_id,
+ luigi.ap_id,
+ Constants.as_public()
+ ],
+ "inReplyTo" => Object.normalize(post2).data["id"]
+ }
+ }
+
+ {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity)
+
+ assert filtered ==
+ "<span class=\"recipients-inline\"><span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{luigi.id}\" href=\"#{luigi.ap_id}\" rel=\"ugc\">@<span>luigi</span></a></span> <span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{mario.id}\" href=\"#{mario.ap_id}\" rel=\"ugc\">@<span>mario</span></a></span> </span>WHA-HA!"
end
end