Make ForceMentionsInContent history-aware

This commit is contained in:
Tusooa Zhu 2022-07-23 22:23:57 -04:00
parent cd19537f39
commit 0a337063e1
No known key found for this signature in database
GPG Key ID: 7B467EDE43A08224
2 changed files with 100 additions and 2 deletions

View File

@ -11,6 +11,9 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do
@behaviour Pleroma.Web.ActivityPub.MRF.Policy
@impl true
def history_awareness, do: :auto
defp do_extract({:a, attrs, _}, acc) do
if Enum.find(attrs, fn {name, value} ->
name == "class" && value in ["mention", "u-url mention", "mention u-url"]
@ -74,11 +77,11 @@ defp clean_recipients(recipients, object) do
@impl true
def filter(
%{
"type" => "Create",
"type" => type,
"object" => %{"type" => "Note", "to" => to, "inReplyTo" => in_reply_to}
} = object
)
when is_list(to) and is_binary(in_reply_to) do
when type in ["Create", "Update"] and 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"] || ""

View File

@ -8,6 +8,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContentTest do
alias Pleroma.Constants
alias Pleroma.Object
alias Pleroma.Web.ActivityPub.MRF
alias Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent
alias Pleroma.Web.CommonAPI
@ -161,4 +162,98 @@ test "with markdown formatting" do
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
test "aware of history" do
mario = insert(:user, nickname: "mario")
wario = insert(:user, nickname: "wario")
{:ok, post1} = CommonAPI.post(mario, %{status: "Letsa go!"})
activity = %{
"type" => "Create",
"actor" => wario.ap_id,
"object" => %{
"type" => "Note",
"actor" => wario.ap_id,
"content" => "WHA-HA!",
"to" => [
mario.ap_id,
Constants.as_public()
],
"inReplyTo" => post1.object.data["id"],
"formerRepresentations" => %{
"orderedItems" => [
%{
"type" => "Note",
"actor" => wario.ap_id,
"content" => "WHA-HA!",
"to" => [
mario.ap_id,
Constants.as_public()
],
"inReplyTo" => post1.object.data["id"]
}
]
}
}
}
expected =
"<span class=\"recipients-inline\"><span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{mario.id}\" href=\"#{mario.ap_id}\" rel=\"ugc\">@<span>mario</span></a></span> </span>WHA-HA!"
assert {:ok,
%{
"object" => %{
"content" => ^expected,
"formerRepresentations" => %{"orderedItems" => [%{"content" => ^expected}]}
}
}} = MRF.filter_one(ForceMentionsInContent, activity)
end
test "works with Updates" do
mario = insert(:user, nickname: "mario")
wario = insert(:user, nickname: "wario")
{:ok, post1} = CommonAPI.post(mario, %{status: "Letsa go!"})
activity = %{
"type" => "Update",
"actor" => wario.ap_id,
"object" => %{
"type" => "Note",
"actor" => wario.ap_id,
"content" => "WHA-HA!",
"to" => [
mario.ap_id,
Constants.as_public()
],
"inReplyTo" => post1.object.data["id"],
"formerRepresentations" => %{
"orderedItems" => [
%{
"type" => "Note",
"actor" => wario.ap_id,
"content" => "WHA-HA!",
"to" => [
mario.ap_id,
Constants.as_public()
],
"inReplyTo" => post1.object.data["id"]
}
]
}
}
}
expected =
"<span class=\"recipients-inline\"><span class=\"h-card\"><a class=\"u-url mention\" data-user=\"#{mario.id}\" href=\"#{mario.ap_id}\" rel=\"ugc\">@<span>mario</span></a></span> </span>WHA-HA!"
assert {:ok,
%{
"object" => %{
"content" => ^expected,
"formerRepresentations" => %{"orderedItems" => [%{"content" => ^expected}]}
}
}} = MRF.filter_one(ForceMentionsInContent, activity)
end
end