EmojiPolicy: implement remove by shortcode

This commit is contained in:
tusooa 2023-02-28 10:51:56 -05:00
parent 28ff828caa
commit 80ce6482f6
No known key found for this signature in database
GPG key ID: 42AEC43D48433C51
2 changed files with 75 additions and 6 deletions

View file

@ -13,17 +13,23 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
Pleroma.Config.get([:mrf_emoji, :remove_url], []) Pleroma.Config.get([:mrf_emoji, :remove_url], [])
end end
defp config_remove_shortcode do
Pleroma.Config.get([:mrf_emoji, :remove_shortcode], [])
end
@impl Pleroma.Web.ActivityPub.MRF.Policy @impl Pleroma.Web.ActivityPub.MRF.Policy
def filter(%{"type" => type, "object" => %{} = object} = message) def filter(%{"type" => type, "object" => %{} = object} = message)
when type in ["Create", "Update"] do 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)} {:ok, Map.put(message, "object", object)}
end end
end end
@impl Pleroma.Web.ActivityPub.MRF.Policy @impl Pleroma.Web.ActivityPub.MRF.Policy
def filter(%{"type" => type} = object) when type in Pleroma.Constants.actor_types() do 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} {:ok, object}
end end
end end
@ -46,12 +52,42 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
end end
defp process_remove(object, :url, patterns) do 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 = processed_tag =
Enum.filter( Enum.filter(
object["tag"], object["tag"],
fn fn
%{"type" => "Emoji", "icon" => %{"url" => url}} when is_binary(url) -> %{"type" => "Emoji"} = tag ->
not match_any?(url, patterns) str = extract_from_tag.(tag)
if is_binary(str) do
not match_any?(str, patterns)
else
true
end
_ -> _ ->
true true
@ -61,8 +97,8 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicy do
processed_emoji = processed_emoji =
if object["emoji"] do if object["emoji"] do
object["emoji"] object["emoji"]
|> Enum.reduce(%{}, fn {name, url}, acc -> |> Enum.reduce(%{}, fn {name, url} = emoji, acc ->
if not match_any?(url, patterns) do if not match_any?(extract_from_emoji.(emoji), patterns) do
Map.put(acc, name, url) Map.put(acc, name, url)
else else
acc acc

View file

@ -120,4 +120,37 @@ defmodule Pleroma.Web.ActivityPub.MRF.EmojiPolicyTest do
assert %{"object" => %{"tag" => ^expected_tags, "emoji" => ^expected_emoji}} = filtered assert %{"object" => %{"tag" => ^expected_tags, "emoji" => ^expected_emoji}} = filtered
end end
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 end