logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git
commit: 64944c164f8730c59c3a55e6c2e4b9e444254a9d
parent a146f6acab5127e9dd7a70ac9dea9bf35a57721e
Author: Alex Gleason <alex@alexgleason.me>
Date:   Fri, 28 Jan 2022 16:53:40 +0000

Merge branch 'mention-mrf-md' into 'develop'

ForceMentionsInContent: improve display of Markdown posts

See merge request pleroma/pleroma!3625

Diffstat:

Mlib/pleroma/web/activity_pub/mrf/force_mentions_in_content.ex19++++++++++++++++---
Mtest/pleroma/web/activity_pub/mrf/force_mentions_in_content_test.exs29++++++++++++++++++++++++++++-
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 @@ -103,10 +103,23 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do end end) - content = + recipients_inline = if added_mentions != "", - do: "<span class=\"recipients-inline\">#{added_mentions}</span>" <> content, - else: content + do: "<span class=\"recipients-inline\">#{added_mentions}</span>", + else: "" + + content = + cond do + # For Markdown posts, insert the mentions inside the first <p> tag + recipients_inline != "" && String.starts_with?(content, "<p>") -> + "<p>" <> recipients_inline <> String.trim_leading(content, "<p>") + + 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 @@ -49,7 +49,7 @@ 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=\"#{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>" + "<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>Haha yeah, you can control who you reply to.</p>" end test "the replied-to user is sorted to the left" do @@ -134,4 +134,31 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContentTest 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" => "<p>I'ma tired...</p>", + "to" => [ + luigi.ap_id, + Constants.as_public() + ], + "inReplyTo" => Object.normalize(post).data["id"] + } + } + + {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) + + assert filtered == + "<p><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>I'ma tired...</p>" + end end