From 3bf257171f546ef84993b08851c01cf064309017 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 27 Jan 2022 14:15:06 -0600 Subject: [PATCH] ForceMentionsInContent: improve display of Markdown posts --- .../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