From 0604b0dd091682727d26567e6166fb89db842a9c Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Tue, 25 Jan 2022 12:33:26 -0600 Subject: [PATCH] ForceMentionsInContent: don't mention self --- .../mrf/force_mentions_in_content.ex | 14 +++++++++++ .../mrf/force_mentions_in_content_test.exs | 24 +++++++++++++++++++ 2 files changed, 38 insertions(+) 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 c9b022cf8..71c240727 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 @@ -3,6 +3,8 @@ # SPDX-License-Identifier: AGPL-3.0-only defmodule Pleroma.Web.ActivityPub.MRF.ForceMentionsInContent do + require Pleroma.Constants + alias Pleroma.Formatter alias Pleroma.Object alias Pleroma.User @@ -58,6 +60,17 @@ defp sort_replied_user(users, %User{id: user_id} = user) do 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 def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = object) when is_list(to) do @@ -69,6 +82,7 @@ def filter(%{"type" => "Create", "object" => %{"type" => "Note", "to" => to}} = mention_users = to + |> clean_recipients(object) |> Enum.map(&User.get_cached_by_ap_id/1) |> Enum.reject(&is_nil/1) |> sort_replied_user(replied_to_user) 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 3bc07be94..589e8fdfb 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 @@ -85,4 +85,28 @@ test "the replied-to user is sorted to the left" do assert filtered == "@luigi @mario WHA-HA!" 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