From 1bbc701a3ababc4c6e856ad24f8826da0a0ba1a3 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 24 Jan 2022 19:11:14 -0600 Subject: [PATCH 1/5] ForceMentionsInContent: use `to` instead of `tag` --- .../web/activity_pub/mrf/force_mentions_in_content.ex | 7 ++----- 1 file changed, 2 insertions(+), 5 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 522ae2f60..8d3885d7b 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 @@ -35,15 +35,12 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do end @impl true - def filter(%{"type" => "Create", "object" => %{"type" => "Note", "tag" => tag}} = object) do + def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = object) do # image-only posts from pleroma apparently reach this MRF without the content field content = object["object"]["content"] || "" mention_users = - tag - |> Enum.filter(fn tag -> tag["type"] == "Mention" end) - |> Enum.map(& &1["href"]) - |> Enum.reject(&is_nil/1) + to |> Enum.map(fn ap_id_or_uri -> case User.get_or_fetch_by_ap_id(ap_id_or_uri) do {:ok, user} -> {ap_id_or_uri, user} From d5644a52aa4f031c69a2938b333636660156c703 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 24 Jan 2022 19:11:45 -0600 Subject: [PATCH 2/5] ForceMentionsInContent: wrap inline mentions with span tag --- lib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex | 2 +- priv/scrubbers/default.ex | 2 +- 2 files changed, 2 insertions(+), 2 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 8d3885d7b..97ea67002 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 @@ -63,7 +63,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do content = if added_mentions != "", - do: added_mentions <> " " <> content, + do: "#{added_mentions} " <> content, else: content {:ok, put_in(object["object"]["content"], content)} diff --git a/priv/scrubbers/default.ex b/priv/scrubbers/default.ex index 4694a92a5..f33a721a8 100644 --- a/priv/scrubbers/default.ex +++ b/priv/scrubbers/default.ex @@ -56,7 +56,7 @@ defmodule Pleroma.HTML.Scrubber.Default do Meta.allow_tag_with_these_attributes(:u, []) Meta.allow_tag_with_these_attributes(:ul, []) - Meta.allow_tag_with_this_attribute_values(:span, "class", ["h-card"]) + Meta.allow_tag_with_this_attribute_values(:span, "class", ["h-card", "recipients-inline"]) Meta.allow_tag_with_these_attributes(:span, []) Meta.allow_tag_with_this_attribute_values(:code, "class", ["inline"]) From c5a20c80c4ed8fdd4e7de18caf85efb56be1db6e Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 24 Jan 2022 19:44:44 -0600 Subject: [PATCH 3/5] ForceMentionsInContent: simplify finding users --- .../web/activity_pub/mrf/force_mentions_in_content.ex | 9 ++------- 1 file changed, 2 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 97ea67002..a458bed31 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 @@ -41,13 +41,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do mention_users = to - |> Enum.map(fn ap_id_or_uri -> - case User.get_or_fetch_by_ap_id(ap_id_or_uri) do - {:ok, user} -> {ap_id_or_uri, user} - _ -> {ap_id_or_uri, User.get_by_uri(ap_id_or_uri)} - end - end) - |> Enum.reject(fn {_, user} -> user == nil end) + |> Enum.map(& {&1, User.get_cached_by_ap_id(&1)}) + |> Enum.reject(fn {_, user} -> is_nil(user) end) |> Enum.into(%{}) explicitly_mentioned_uris = extract_mention_uris_from_content(content) From 267184b70e587a3e6301d3a01bd16fd465bc0f68 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Mon, 24 Jan 2022 20:03:43 -0600 Subject: [PATCH 4/5] ForceMentionsInContentTest: return mentions in a not terrible format --- .../mrf/force_mentions_in_content.ex | 6 +-- .../mrf/force_mentions_in_content_test.exs | 38 +++++++++++++------ 2 files changed, 29 insertions(+), 15 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 a458bed31..1d750d4c2 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 @@ -41,7 +41,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do mention_users = to - |> Enum.map(& {&1, User.get_cached_by_ap_id(&1)}) + |> Enum.map(&{&1, User.get_cached_by_ap_id(&1)}) |> Enum.reject(fn {_, user} -> is_nil(user) end) |> Enum.into(%{}) @@ -50,7 +50,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do added_mentions = Enum.reduce(mention_users, "", fn {uri, user}, acc -> unless uri in explicitly_mentioned_uris do - acc <> Formatter.mention_from_user(user) + acc <> Formatter.mention_from_user(user, %{mentions_format: :compact}) <> " " else acc end @@ -58,7 +58,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do content = if added_mentions != "", - do: "#{added_mentions} " <> content, + do: "#{added_mentions}" <> content, else: content {:ok, put_in(object["object"]["content"], content)} 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 090bdc35e..3c40887c4 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 @@ -8,17 +8,29 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContentTest do use Pleroma.DataCase test "adds mentions to post content" do - users = %{ - "lain@lain.com" => "https://lain.com/users/lain", - "coolboymew@shitposter.club" => "https://shitposter.club/users/coolboymew", - "dielan@shitposter.club" => "https://shitposter.club/users/dielan", - "hakui@tuusin.misono-ya.info" => "https://tuusin.misono-ya.info/users/hakui", - "fence@xyzzy.link" => "https://xyzzy.link/users/fence" - } - - Enum.each(users, fn {nickname, ap_id} -> - insert(:user, ap_id: ap_id, nickname: nickname, local: false) - end) + [lain, coolboymew, dielan, hakui, fence] = [ + insert(:user, ap_id: "https://lain.com/users/lain", nickname: "lain@lain.com", local: false), + insert(:user, + ap_id: "https://shitposter.club/users/coolboymew", + nickname: "coolboymew@shitposter.club", + local: false + ), + insert(:user, + ap_id: "https://shitposter.club/users/dielan", + nickname: "dielan@shitposter.club", + local: false + ), + insert(:user, + ap_id: "https://tuusin.misono-ya.info/users/hakui", + nickname: "hakui@tuusin.misono-ya.info", + local: false + ), + insert(:user, + ap_id: "https://xyzzy.link/users/fence", + nickname: "fence@xyzzy.link", + local: false + ) + ] object = File.read!("test/fixtures/soapbox_no_mentions_in_content.json") |> Jason.decode!() @@ -29,6 +41,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContentTest do } {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) - Enum.each(users, fn {nickname, _} -> assert filtered =~ nickname end) + + assert filtered == + "@lain @coolboymew @dielan @hakui @fence

Haha yeah, you can control who you reply to.

" end end From 65b4d2ce8450098ee2eaa2057968a4f47aa536c1 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 25 Jan 2022 10:42:34 -0600 Subject: [PATCH 5/5] ForceMentionsInContent: fix order of mentions --- .../mrf/force_mentions_in_content.ex | 38 +++++++++++++-- .../mrf/force_mentions_in_content_test.exs | 46 +++++++++++++++++-- 2 files changed, 76 insertions(+), 8 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 - alias Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent - import Pleroma.Factory 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 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 == - "@lain @coolboymew @dielan @hakui @fence

Haha yeah, you can control who you reply to.

" + "@dielan @coolboymew @fence @hakui @lain

Haha yeah, you can control who you reply to.

" + 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 == + "@luigi @mario WHA-HA!" end end