ForceMentionsInContent: improve display of Markdown posts

This commit is contained in:
Alex Gleason 2022-01-27 14:15:06 -06:00
parent a146f6acab
commit 3bf257171f
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
2 changed files with 44 additions and 4 deletions

View File

@ -103,10 +103,23 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do
end end
end) end)
content = recipients_inline =
if added_mentions != "", if added_mentions != "",
do: "<span class=\"recipients-inline\">#{added_mentions}</span>" <> content, do: "<span class=\"recipients-inline\">#{added_mentions}</span>",
else: content 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)} {:ok, put_in(object["object"]["content"], content)}
end end

View File

@ -49,7 +49,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContentTest do
{:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity) {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity)
assert filtered == 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 end
test "the replied-to user is sorted to the left" do 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) {:ok, %{"object" => %{"content" => filtered}}} = ForceMentionsInContent.filter(activity)
assert filtered == "Mama mia!" assert filtered == "Mama mia!"
end 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 end