From 0604b0dd091682727d26567e6166fb89db842a9c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 25 Jan 2022 12:33:26 -0600 Subject: ForceMentionsInContent: don't mention self --- .../activity_pub/mrf/force_mentions_in_content.ex | 14 +++++++++++++ .../mrf/force_mentions_in_content_test.exs | 24 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) 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 c9b022cf8..71c240727 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 @@ -3,6 +3,8 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do + require Pleroma.Constants + alias Pleroma.Formatter alias Pleroma.Object alias Pleroma.User @@ -58,6 +60,17 @@ defp sort_replied_user(users, %User{id: user_id} = user) do defp sort_replied_user(users, _), do: users + # Drop constants and the actor's own AP ID + defp clean_recipients(recipients, object) do + Enum.reject(recipients, fn ap_id -> + ap_id in [ + object["object"]["actor"], + Pleroma.Constants.as_public(), + Pleroma.Web.ActivityPub.Utils.as_local_public() + ] + end) + end + @impl true def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = object) when is_list(to) do @@ -69,6 +82,7 @@ def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = mention_users = to + |> clean_recipients(object) |> Enum.map(&User.get_cached_by_ap_id/1) |> Enum.reject(&is_nil/1) |> sort_replied_user(replied_to_user) 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 3bc07be94..589e8fdfb 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 @@ -85,4 +85,28 @@ test "the replied-to user is sorted to the left" do assert filtered == "@luigi @mario WHA-HA!" end + + test "don't mention self" do + mario = insert(:user, nickname: "mario") + + {:ok, post} = CommonAPI.post(mario, %{status: "Mama mia"}) + + activity = %{ + "type" => "Create", + "actor" => mario.ap_id, + "object" => %{ + "type" => "Note", + "actor" => mario.ap_id, + "content" => "I'ma tired...", + "to" => [ + mario.ap_id, + Constants.as_public() + ], + "inReplyTo" => Object.normalize(post).data["id"] + } + } + + {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) + assert filtered == "I'ma tired..." + end end -- cgit v1.2.3 From a4de79ced01d02cddeec525ee829792cd5dfefee Mon Sep 17 00:00:00 2001 From: bot <44222-bot@users.noreply.git.pleroma.social> Date: Wed, 26 Jan 2022 22:53:02 +0000 Subject: ForceBotUnlistedPolicy: fix to stop unlisting my posts >:( --- lib/pleroma/web/activity_pub/mrf/force_bot_unlisted_policy.ex | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/pleroma/web/activity_pub/mrf/force_bot_unlisted_policy.ex b/lib/pleroma/web/activity_pub/mrf/force_bot_unlisted_policy.ex index 11871375e..b10b27f06 100644 --- a/lib/pleroma/web/activity_pub/mrf/force_bot_unlisted_policy.ex +++ b/lib/pleroma/web/activity_pub/mrf/force_bot_unlisted_policy.ex @@ -10,7 +10,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceBotUnlistedPolicy do require Pleroma.Constants defp check_by_actor_type(user), do: user.actor_type in ["Application", "Service"] - defp check_by_nickname(user), do: Regex.match?(~r/bot@|ebooks@/i, user.nickname) + defp check_by_nickname(user), do: Regex.match?(~r/.bot@|ebooks@/i, user.nickname) defp check_if_bot(user), do: check_by_actor_type(user) or check_by_nickname(user) -- cgit v1.2.3 From 27cb3d62738f6ea174994f72688497db49cef754 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Wed, 26 Jan 2022 21:24:26 -0600 Subject: ForceMentionsInContent: don't apply it to top-level posts --- .../activity_pub/mrf/force_mentions_in_content.ex | 9 ++++++-- .../mrf/force_mentions_in_content_test.exs | 25 ++++++++++++++++++++++ 2 files changed, 32 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 71c240727..715771d9d 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 @@ -72,8 +72,13 @@ defp clean_recipients(recipients, object) do end @impl true - def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = object) - when is_list(to) do + def filter( + %{ + "type" => "Create", + "object" => %{"type" => "Note", "to" => to, "inReplyTo" => in_reply_to} + } = object + ) + when is_list(to) and is_binary(in_reply_to) do # image-only posts from pleroma apparently reach this MRF without the content field content = object["object"]["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 589e8fdfb..6bcf75a92 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 @@ -109,4 +109,29 @@ test "don't mention self" do {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) assert filtered == "I'ma tired..." end + + test "don't mention in top-level posts" do + mario = insert(:user, nickname: "mario") + luigi = insert(:user, nickname: "luigi") + + {:ok, post} = CommonAPI.post(mario, %{status: "Letsa go"}) + + activity = %{ + "type" => "Create", + "actor" => mario.ap_id, + "object" => %{ + "type" => "Note", + "actor" => mario.ap_id, + "content" => "Mama mia!", + "to" => [ + luigi.ap_id, + Constants.as_public() + ], + "quoteUrl" => Object.normalize(post).data["id"] + } + } + + {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) + assert filtered == "Mama mia!" + end end -- cgit v1.2.3 From 3bf257171f546ef84993b08851c01cf064309017 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 27 Jan 2022 14:15:06 -0600 Subject: ForceMentionsInContent: improve display of Markdown posts --- .../activity_pub/mrf/force_mentions_in_content.ex | 19 +++++++++++--- .../mrf/force_mentions_in_content_test.exs | 29 +++++++++++++++++++++- 2 files changed, 44 insertions(+), 4 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 715771d9d..255910b2f 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 @@ -103,10 +103,23 @@ def filter( end end) - content = + recipients_inline = if added_mentions != "", - do: "#{added_mentions}" <> content, - else: content + do: "#{added_mentions}", + else: "" + + content = + cond do + # For Markdown posts, insert the mentions inside the first

tag + recipients_inline != "" && String.starts_with?(content, "

") -> + "

" <> recipients_inline <> String.trim_leading(content, "

") + + recipients_inline != "" -> + recipients_inline <> content + + true -> + content + end {:ok, put_in(object["object"]["content"], content)} end 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 6bcf75a92..669ec5251 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 @@ -49,7 +49,7 @@ test "adds mentions to post content" do {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) assert filtered == - "@dielan @coolboymew @fence @hakui @lain

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 @@ -134,4 +134,31 @@ test "don't mention in top-level posts" do {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) assert filtered == "Mama mia!" end + + test "with markdown formatting" do + mario = insert(:user, nickname: "mario") + luigi = insert(:user, nickname: "luigi") + + {:ok, post} = CommonAPI.post(luigi, %{status: "Mama mia"}) + + activity = %{ + "type" => "Create", + "actor" => mario.ap_id, + "object" => %{ + "type" => "Note", + "actor" => mario.ap_id, + "content" => "

I'ma tired...

", + "to" => [ + luigi.ap_id, + Constants.as_public() + ], + "inReplyTo" => Object.normalize(post).data["id"] + } + } + + {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) + + assert filtered == + "

@luigi I'ma tired...

" + end end -- cgit v1.2.3