ForceMentionsInContent: don't mention self
This commit is contained in:
parent
0f4e0e667e
commit
0604b0dd09
@ -3,6 +3,8 @@
|
|||||||
# SPDX-License-Identifier: AGPL-3.0-only
|
# SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
|
||||||
defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do
|
defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do
|
||||||
|
require Pleroma.Constants
|
||||||
|
|
||||||
alias Pleroma.Formatter
|
alias Pleroma.Formatter
|
||||||
alias Pleroma.Object
|
alias Pleroma.Object
|
||||||
alias Pleroma.User
|
alias Pleroma.User
|
||||||
@ -58,6 +60,17 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do
|
|||||||
|
|
||||||
defp sort_replied_user(users, _), do: users
|
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
|
@impl true
|
||||||
def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = object)
|
def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = object)
|
||||||
when is_list(to) do
|
when is_list(to) do
|
||||||
@ -69,6 +82,7 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do
|
|||||||
|
|
||||||
mention_users =
|
mention_users =
|
||||||
to
|
to
|
||||||
|
|> clean_recipients(object)
|
||||||
|> Enum.map(&User.get_cached_by_ap_id/1)
|
|> Enum.map(&User.get_cached_by_ap_id/1)
|
||||||
|> Enum.reject(&is_nil/1)
|
|> Enum.reject(&is_nil/1)
|
||||||
|> sort_replied_user(replied_to_user)
|
|> sort_replied_user(replied_to_user)
|
||||||
|
@ -85,4 +85,28 @@ defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContentTest do
|
|||||||
assert filtered ==
|
assert filtered ==
|
||||||
"<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 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!"
|
"<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 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!"
|
||||||
end
|
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
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user