From 80ce6482f6eb85f4c1d172811affc4a17d24985a Mon Sep 17 00:00:00 2001 From: tusooa Date: Tue, 28 Feb 2023 10:51:56 -0500 Subject: [PATCH] EmojiPolicy: implement remove by shortcode --- lib/pleroma/web/activity_pub/emoji_policy.ex | 48 ++++++++++++++++--- .../activity_pub/mrf/emoji_policy_test.exs | 33 +++++++++++++ 2 files changed, 75 insertions(+), 6 deletions(-) diff --git a/lib/pleroma/web/activity_pub/emoji_policy.ex b/lib/pleroma/web/activity_pub/emoji_policy.ex index 747e720b6..346b64699 100644 --- a/lib/pleroma/web/activity_pub/emoji_policy.ex +++ b/lib/pleroma/web/activity_pub/emoji_policy.ex @@ -13,17 +13,23 @@ defp config_remove_url do Pleroma.Config.get([:mrf_emoji, :remove_url], []) end + defp config_remove_shortcode do + Pleroma.Config.get([:mrf_emoji, :remove_shortcode], []) + end + @impl Pleroma.Web.ActivityPub.MRF.Policy def filter(%{"type" => type, "object" => %{} = object} = message) when type in ["Create", "Update"] do - with object <- process_remove(object, :url, config_remove_url()) do + with object <- process_remove(object, :url, config_remove_url()), + object <- process_remove(object, :shortcode, config_remove_shortcode()) do {:ok, Map.put(message, "object", object)} end end @impl Pleroma.Web.ActivityPub.MRF.Policy def filter(%{"type" => type} = object) when type in Pleroma.Constants.actor_types() do - with object <- process_remove(object, :url, config_remove_url()) do + with object <- process_remove(object, :url, config_remove_url()), + object <- process_remove(object, :shortcode, config_remove_shortcode()) do {:ok, object} end end @@ -46,12 +52,42 @@ defp match_any?(string, patterns) do end defp process_remove(object, :url, patterns) do + process_remove_impl( + object, + fn + %{"icon" => %{"url" => url}} -> url + _ -> nil + end, + fn {_name, url} -> url end, + patterns + ) + end + + defp process_remove(object, :shortcode, patterns) do + process_remove_impl( + object, + fn + %{"name" => name} when is_binary(name) -> String.trim(name, ":") + _ -> nil + end, + fn {name, _url} -> name end, + patterns + ) + end + + defp process_remove_impl(object, extract_from_tag, extract_from_emoji, patterns) do processed_tag = Enum.filter( object["tag"], fn - %{"type" => "Emoji", "icon" => %{"url" => url}} when is_binary(url) -> - not match_any?(url, patterns) + %{"type" => "Emoji"} = tag -> + str = extract_from_tag.(tag) + + if is_binary(str) do + not match_any?(str, patterns) + else + true + end _ -> true @@ -61,8 +97,8 @@ defp process_remove(object, :url, patterns) do processed_emoji = if object["emoji"] do object["emoji"] - |> Enum.reduce(%{}, fn {name, url}, acc -> - if not match_any?(url, patterns) do + |> Enum.reduce(%{}, fn {name, url} = emoji, acc -> + if not match_any?(extract_from_emoji.(emoji), patterns) do Map.put(acc, name, url) else acc diff --git a/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs b/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs index fe1fb338e..6511f6532 100644 --- a/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs +++ b/test/pleroma/web/activity_pub/mrf/emoji_policy_test.exs @@ -120,4 +120,37 @@ test "processes status" do assert %{"object" => %{"tag" => ^expected_tags, "emoji" => ^expected_emoji}} = filtered end end + + describe "remove_shortcode" do + setup do + clear_config([:mrf_emoji, :remove_shortcode], [ + "test", + ~r{mikoto_s}, + "nekomimi_girl_emoji" + ]) + + :ok + end + + test "processes user" do + {:ok, filtered} = MRF.filter_one(EmojiPolicy, @user_data) + + expected_tags = [@emoji_tags |> Enum.at(2)] ++ @misc_tags + + assert %{"tag" => ^expected_tags} = filtered + end + + test "processes status" do + {:ok, filtered} = MRF.filter_one(EmojiPolicy, @status_data) + + expected_tags = [@emoji_tags |> Enum.at(2)] ++ @misc_tags + + expected_emoji = %{ + "nekomimi_girl_emoji_007" => + "https://example.org/emoji/nekomimi_girl_emoji/nekomimi_girl_emoji_007.png" + } + + assert %{"object" => %{"tag" => ^expected_tags, "emoji" => ^expected_emoji}} = filtered + end + end end